Bot的回叫按钮已添加到VK

使用回调


美好的一天,哈伯亲爱的居民。



我的第一篇文章。这将是简短,简洁且希望相关的。



7月9日,VK推出了聊天机器人的回调按钮。电报紧张 做过这个 在199 ...长。用于在Python中为VK开发聊天机器人的最受欢迎的库是vk_api(来自开发人员python273)。由于新编辑的执行速度很慢,因此我自由地制作了一个fork,对其进行了补充并描述了一个小用例。



原始v11.80做了什么?



  1. ( API):



    • 5 ( 4);
    • 10 6 inline ( 10 , );
    • Default Secondary.


  2. callback-:



    • "message_event" ( );
    • " callback ";
    • example ( ).




callback



allback 3 (+ ):



  1. show_snackbar — ( 10 );
  2. open_link — URL ;
  3. open_app — ;
  4. , + .


?



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-: , — .



.



我希望这个快速的概述可以帮助某人开始使用其漫游器中的回调按钮。请求请求已发送给该库的作者。



ps更改注入主代码;




All Articles