Good day, dear residents of Habr.
My first post. It will be brief, succinct and hopefully relevant.
On July 9th, VK rolled out callback buttons for chat bots. Telegramtensed did this at 199 ...long. The most popular library for developing chat bots for VK in Python is vk_api (from the developerpython273). Due to the fact that fresh edits are being made slowly, I took the liberty of making a fork, supplementing it and describing a small use case.
What has been done regarding the original v11.80?
Updated keyboard restrictions (in line with API changes):
- no more than 5 buttons per line (there were 4);
- no more than 10 and 6 lines for standard and inline views, respectively (there were 10 both there and there);
- The Default button has been renamed Secondary.
Added callback buttons:
- new type of event "message_event" (click on the button);
- new method "create callback button";
- added example (animation of work and code will be given below).
Types of callback buttons
Callback buttons have 3 built-in click actions (+ editing a message):
- show_snackbar - show a pop-up message (disappears after 10 seconds);
- open_link - open URL link;
- open_app - open VK application;
- you can configure editing of the current message, so that by clicking you can change the keyboard + text in the current message.
How do I install a modified library?
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-: , â .
.
I hope this quick overview will help someone get started using callback buttons in their bots. Pull-request was sent to the author of the library.
ps changes injecting the main code;