VKWave-开发VK机器人的框架



哈Ha!



今天,我想谈论一个很棒的库,用于使用Python编程语言开发VK机器人。



VKWave



VKWave是使用asyncio编写的VK机器人开发框架。该项目的主要目标是使开发人员能够自己配置尽可能多的框架,同时确保良好的开发速度。



最低要求的Python版本是3.7



VKWave, . , , .



Telegram





:



pip install vkwave


!



Echo-



. , .



#   .
# SimpleLongPollBot:       
# SimpleBotEvent:  ,   SimpleLongPollBot
from vkwave.bots import SimpleLongPollBot, SimpleBotEvent

#   (   ,  vkwave    )
bot = SimpleLongPollBot(tokens=TOKEN, group_id=GROUP_ID)

#    .
#    ,         
@bot.message_handler()
def echo(event: SimpleBotEvent) -> str:
    #     , . vkwave ,     ,        .      ,       (          )
    return event.object.object.message.text

#      (    )
bot.run_forever()


.



- . , , /echo. — /echo .



Echo-



#     .         `/< >`.    ,     
@bot.message_handler(bot.command_filter("echo"))
def echo(event: SimpleBotEvent) -> str:
    #    
    args = event.object.object.message.text.split()
    # ,      
    #    - ,     - 
    if len(args) < 2:
        return " - !"
    #    (    )
    return " ".join(args[1:])


. , VKWave . : VKWave , .



Echo-



, .



#       
from vkwave.bots.core.dispatching.filters.base import BaseFilter, BaseEvent, FilterResult

#   ,    
class EchoFilter(BaseFilter):
    #    `__init__`    

    #    `check`,       
    async def check(self, event: BaseEvent) -> FilterResult:
        #     
        text = event.object.object.message.text
        #    
        all_args = text.split()
        #     -   
        if len(all_args) < 2:
            #  False.
            #   , , -  
            #    `event.api_ctx`,       
            return FilterResult(False)
        #    ( )  "/echo"  False
        if all_args[0] != "/echo":
            return FilterResult(False)
        #       
        event["echo_answer"] = " ".join(all_args[1:])
        return FilterResult(True)

#  
@bot.message_handler(EchoFilter())
def echo(event: SimpleBotEvent) -> str:
    #  ,    ""  
    return event["echo_answer"]




, , , VKWave. , middlewares, , , , HTTP !



, vk_api vk.



GitHub

Telegram




All Articles