Answer the question
In order to leave comments, you need to log in
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.
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
replies one time
bad code
vk_session.method('groups.isMember', {'group_id': 198542978, 'user_id': id})
ksend(id, '''Привет я Бот!''', 534923, keyb = keyboard)
ksend(id, 'Ну привет', 432342, keyv=keyboard)
if msg.lower() == 'назад':
messages = vk_session.method("messages.getConversations", {"offset": 0, "count": 20, "filter": "unanswered"})
if messages["count"] >= 1:
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question