Buen día, queridos vecinos de Habr.
Mi primera publicación. Será breve, conciso y, con suerte, relevante.
El 9 de julio, VK lanzó botones de devolución de llamada para chat bots. Telegramatenso hice esto en 199 ...largo. La biblioteca más popular para desarrollar bots de chat para VK en Python es vk_api (del desarrolladorpython273). Debido al hecho de que las nuevas ediciones se están realizando lentamente, me tomé la libertad de hacer una bifurcación, complementarla y describir un pequeño caso de uso.
¿Qué se ha hecho con respecto a la v11.80 original?
( API):
- 5 ( 4);
- 10 6 inline ( 10 , );
- Default Secondary.
callback-:
- "message_event" ( );
- " callback ";
- example ( ).
callback
allback 3 (+ ):
- show_snackbar — ( 10 );
- open_link — URL ;
- open_app — ;
- , + .
?
pip install git+https://github.com/chebotarevmichael/vk_api
, . .
. , .
from vk_api import VkApi
from vk_api.utils import get_random_id
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
from vk_api.keyboard import VkKeyboard, VkKeyboardColor
import json
. longpoll- .
" " .
#
GROUP_ID = '100...500'
GROUP_TOKEN = 'df2148cc7c664..._....df2148cc7c6642242531fad399'
API_VERSION = '5.120'
# callback- " "
APP_ID = 100500 # id IFrame
OWNER_ID = 123456 # id
# callback-
CALLBACK_TYPES = ('show_snackbar', 'open_link', 'open_app')
#
vk_session = VkApi(token=GROUP_TOKEN, api_version=API_VERSION)
vk = vk_session.get_api()
longpoll = VkBotLongPoll(vk_session, group_id=GROUP_ID)
— 4 .
— "", .
.
#
settings = dict(one_time=False, inline=True)
# №1. 3 : " ", " URL" ( )
keyboard_1 = VkKeyboard(**settings)
# pop-up
keyboard_1.add_callback_button(label=' pop-up ', color=VkKeyboardColor.SECONDARY, payload={"type": "show_snackbar", "text": " "})
keyboard_1.add_line()
# URL
keyboard_1.add_callback_button(label=' Url', color=VkKeyboardColor.POSITIVE, payload={"type": "open_link", "link": "https://vk.com/dev/bots_docs_5"})
keyboard_1.add_line()
# -
keyboard_1.add_callback_button(label=' ', color=VkKeyboardColor.NEGATIVE, payload={"type": "open_app", "app_id": APP_ID, "owner_id": OWNER_ID, "hash": "anything_data_100500"})
keyboard_1.add_line()
# 2
keyboard_1.add_callback_button(label=' ', color=VkKeyboardColor.PRIMARY, payload={"type": "my_own_100500_type_edit"})
# №2. callback-. .
keyboard_2 = VkKeyboard(**settings)
# , 1 .
keyboard_2.add_callback_button('', color=VkKeyboardColor.NEGATIVE, payload={"type": "my_own_100500_type_edit"})
long poll
. — . " callback " — ( 3+1 , ).
f_toggle: bool = False
for event in longpoll.listen():
# 1
if event.type == VkBotEventType.MESSAGE_NEW:
if event.obj.message['text'] != '':
if event.from_user:
# callback-,
#
# . .. inline .
if 'callback' not in event.obj.client_info['button_actions']:
print(f' {event.obj.message["from_id"]} . callback')
vk.messages.send(
user_id=event.obj.message['from_id'],
random_id=get_random_id(),
peer_id=event.obj.message['from_id'],
keyboard=keyboard_1.get_keyboard(),
message=event.obj.message['text'])
# callback
elif event.type == VkBotEventType.MESSAGE_EVENT:
# 3 :
if event.object.payload.get('type') in CALLBACK_TYPES:
# .
# payload callback- .
# : payload
# ,
# . .
r = vk.messages.sendMessageEventAnswer(
event_id=event.object.event_id,
user_id=event.object.user_id,
peer_id=event.object.peer_id,
event_data=json.dumps(event.object.payload))
# "" (.. ) ,
# edit .
# / pop-up. (. )
elif event.object.payload.get('type') == 'my_own_100500_type_edit':
last_id = vk.messages.edit(
peer_id=event.obj.peer_id,
message='ola',
conversation_message_id=event.obj.conversation_message_id,
keyboard=(keyboard_1 if f_toggle else keyboard_2).get_keyboard())
f_toggle = not f_toggle
if __name__ == '__main__':
print()
.
, callback- - — . , — . inline- . - — , -.
- -, , . . , , . callback-: , — .
.
Con suerte, esta descripción general rápida ayudará a alguien a comenzar a usar los botones de devolución de llamada en sus bots. Se ha enviado una solicitud de extracción al autor de la biblioteca.
ps cambia agregando el código principal;