Selamat siang, warga Habr yang terkasih.
Posting pertama saya. Ini akan singkat, ringkas dan mudah-mudahan relevan.
Pada 9 Juli, VK meluncurkan tombol panggilan balik untuk bot obrolan. Telegramtegang melakukan ini pada 199 ...panjang. Perpustakaan paling populer untuk mengembangkan bot obrolan untuk VK dengan Python adalah vk_api (dari pengembangpython273). Karena fakta bahwa pengeditan baru diperkenalkan perlahan, saya mengambil kebebasan untuk membuat garpu, melengkapinya dan menjelaskan contoh kecil penggunaan.
Apa yang telah dilakukan terkait v11.80 asli?
( 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-: , — .
.
Semoga ikhtisar singkat ini akan membantu seseorang mulai menggunakan tombol panggilan balik di bot mereka. Pull-request telah dikirim ke penulis perpustakaan.
perubahan ps menyuntikkan kode utama;