
مرحبا هبر!
أريد اليوم أن أتحدث عن مكتبة رائعة لتطوير برامج VK باستخدام لغة برمجة Python.
VKWave
VKWave هو إطار عمل لتطوير VK bot مكتوب باستخدام asyncio. تتمثل الأهداف الرئيسية للمشروع في تمكين المطور من تكوين الإطار قدر الإمكان لنفسه ، مع ضمان سرعة تطوير مناسبة في نفس الوقت.
الحد الأدنى المطلوب لإصدار Python هو 3.7
VKWave, . , , .
:
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.