Bonjour, chers habitants de Habr.
Mon premier message. Il sera bref, succinct et, espérons-le, pertinent.
Le 9 juillet, VK a déployé des boutons de rappel pour les robots de discussion. Télégrammetendu fait ça à 199 ...longue. La bibliothèque la plus populaire pour développer des robots de discussion pour VK en Python est vk_api (du développeurpython273). Du fait que de nouvelles modifications se font lentement, j'ai pris la liberté de faire une fourchette, de la compléter et de décrire un petit exemple d'utilisation.
Qu'est-ce qui a été fait concernant la version originale v11.80?
( 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-: , — .
.
Espérons que cet aperçu rapide aidera quelqu'un à commencer à utiliser les boutons de rappel dans ses robots. Pull-request a été envoyé à l'auteur de la bibliothèque.
ps change en injectant le code principal;