Stack Overflow на русском Asked on December 26, 2020
Пишу бота для группы в контакте.
Начал добавлять кнопки, но они не появляются в сообщениях с ботом. При этом ошибки программа не выдает.
import vk_api
from vk_api import VkUpload
from vk_api.longpoll import VkLongPoll, VkEventType
from vk_api.utils import get_random_id
from vk_api.keyboard import VkKeyboard, VkKeyboardColor
keyboard = VkKeyboard(one_time=False)
keyboard.add_button(label="Кнопка 1", color=VkKeyboardColor.POSITIVE)
token = 'token'
image = ''
vk_session = vk_api.VkApi(token = token)
session_api = vk_session.get_api()
longpull = VkLongPoll(vk_session)
upload = VkUpload(vk_session)
def send_msg(id, msg):
vk_session.method('messages.send', {'user_id': id, 'message': msg, 'random_id': get_random_id()})
def send_loud_photo(id):
vk_session.method('messages.send', {'user_id': id, 'random_id': get_random_id(), 'attachment':
','.join(attachments)})
def send_photo(id, url):
vk_session.method('messages.send', {'user_id': id, 'attachment': url, 'random_id':
get_random_id()})
for event in longpull.listen():
if event.type == VkEventType.MESSAGE_NEW and event.to_me and event.text:
msg = event.text.lower()
id = event.user_id
attachments = []
upload_image = upload.photo_messages(photos=image)[0]
attachments.append('photo{}_{}'.format(upload_image['owner_id'], upload_image['id']))
if msg == 'привет':
send_msg(id, 'Привет')
elif msg == 'фото1':
send_photo(id, 'url')
elif msg == 'фото2':
send_loud_photo(id)
elif msg == 'ссылка':
send_msg(id, r'https://vk.com/id'+str(id))
else:
send_msg(id, 'неизвестное собщение')
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP