A
A
AnatolyBossman2020-09-11 15:49:16
Python
AnatolyBossman, 2020-09-11 15:49:16

Why does the bot work every other time?

Why does the bot respond once? I ask you not to pay attention to bad code, since refactoring has not yet been done.

The code
import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
import json


token = "...."

vk_session = vk_api.VkApi(token=token)
session_api = vk_session.get_api()

longpoll = VkLongPoll(vk_session)

def get_button(label, color):
    return {
        'action': {
            'type': 'text',
            'label': label,
        },
        'color': color
    }

keyboard_size = {
    'one_time': True,
    'buttons': [
        [get_button(label = 'Вперед', color = 'primary')],
        [get_button(label = 'Вправо', color = 'primary')],
        [get_button(label = 'Влево', color = 'primary')],
        [get_button(label = 'Назад', color = 'secondary')]
    ]
}

keyboard_size = json.dumps(keyboard_size, ensure_ascii = False).encode('utf-8')
keyboard_size = str(keyboard_size.decode('utf-8'))

keyboard = {
    'one_time': False,
    'buttons': [
        [get_button(label ='Привяо', color = 'primary')],
        [get_button(label = 'Пока', color = 'primary')]
    ] 
}

keyboard = json.dumps(keyboard, ensure_ascii = False).encode('utf-8')
keyboard = str(keyboard.decode('utf-8'))

def send(user_id, message, random_id):
    vk_session.method('messages.send', {'user_id': user_id, 'message': message, 'random_id': random_id})

def ksend(user_id, message, random_id, keyb):
    vk_session.method('messages.send', {'user_id': user_id, 'message': message, 'random_id': random_id, 'keyboard': keyb})

def psend(user_id, picture, random_id):
    vk_session.method('messages.send', {'user_id': user_id, 'attachment': picture, 'random_id': random_id})
    
def dsend(user_id, random_id, keybb):
     vk_session.method('messages.send', {'user_id': user_id, 'message': 'Вы успешно вернулись назад.', 'random_id': random_id, 'keyboard': keybb})

while True:
    for event in longpoll.listen():
        if event.type == VkEventType.MESSAGE_NEW:
            messages = vk_session.method("messages.getConversations", {"offset": 0, "count": 20, "filter": "unanswered"})
            if messages["count"] >= 1:

                msg = event.text.lower()
                id = event.user_id
                verify_user = vk_session.method('groups.isMember', {'group_id': 198542978, 'user_id': id})

                if verify_user == 1:

                    if msg == 'начать':
                        ksend(id, '''Привет я Бот!''', 534923, keyb = keyboard)

                    if msg == 'впреред':
                        ksend(id, 'Ну привет', 432342, keyv=keyboard)

                    if msg == 'вправо':
                        ksend(id, 'кк', 321342, keyb = keyboard)

                    if msg == 'влево':
                        ksend(id, 'adidas', 231932, keyb = keyboard)

                    if msg.lower() == 'назад':
                        dsend(id, 472932, keybb = keyboard)  
                else:
                    send(id, 'Для дальнейшей работы вы должны подписаться.', 348232)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2020-09-11
@SoreMix

replies one time

bad code

I doubt that
vk_session.method('groups.isMember', {'group_id': 198542978, 'user_id': id})

Returns 0 or 1
Decide which parameter is responsible for the keyboard
ksend(id, '''Привет я Бот!''', 534923, keyb = keyboard)
ksend(id, 'Ну привет', 432342, keyv=keyboard)

Send messages with the same random_id?
Lowercase for the second time to be sure Strange design
if msg.lower() == 'назад':
messages = vk_session.method("messages.getConversations", {"offset": 0, "count": 20, "filter": "unanswered"})
if messages["count"] >= 1:

1. unanswered only exists for community posts. But you don't pass the group ID.
2. I doubt that there are cases when the value will be < 1
I can be wrong, I don’t write like that

H
HemulGM, 2020-09-11
@HemulGM

Because that's how it was written

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question