vk-bot
vk-bot
is a Py3 VK bot builder built with
vk-client
.
Install
Stable from PyPI:
pip install vk-bot
Bleeding edge from Github:
pip install git+https://github.com/Suenweek/vk-bot#egg=vk-bot
Usage
Create bot
import vk_bot
bot = vk_bot.VkBot('YOUR_ACCESS_TOKEN')
Register event handlers
from vk_client import GroupEventType
@bot.on([GroupEventType.GROUP_JOIN])
def group_join_handler(obj):
bot.vk.Message.send(
peer=obj.user,
message=f'Welcome, {obj.user.first_name}!'
)
Create event handler for commands
from vk_bot.ext import CmdHandler
command = CmdHandler(bot)
Register commands
@command(pass_msg=True)
def whoami(msg):
"""Print effective user name."""
return f'{msg.sender.first_name} {msg.sender.last_name}'
More involved command example
import operator
OPS = {
'add': operator.add,
'sub': operator.sub,
'mul': operator.mul,
'div': operator.truediv
}
@command(args={
'op': {'choices': list(OPS)},
'a': {'type': int},
'b': {'type': int}
})
def calc(op, a, b):
"""Simple calculator."""
try:
return f'Result: {OPS[op](a, b)}'
except ArithmeticError as e:
return f'Error: {e}'
Run bot
bot.run()