VKWave - a framework for developing VK bots



Hello, Habr!



Today I want to talk about a wonderful library for developing VK bots using the Python programming language.



VKWave



VKWave is a VK bot development framework written using asyncio. The main goals of the project are to enable the developer to configure the framework as much as possible for himself, while at the same time ensuring a decent development speed.



The minimum required Python version is 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