Skip to content
This repository has been archived by the owner on Oct 24, 2019. It is now read-only.
/ async-vk-bot Public archive

Async VK bot builder built with trio and async-vk-api.

Notifications You must be signed in to change notification settings

rnovatorov/async-vk-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status codecov

async-vk-bot

Async VK bot builder built with trio and async-vk-api.

Installation

Stable from PyPi

pip install async-vk-bot

Latest from Github

pip install git+https://github.com/Suenweek/async-vk-bot#egg=async-vk-bot

Usage

import os

import trio
import async_vk_api
import async_vk_bot


def new_message(event):
    return event['type'] == 'message_new'


async def echo_once(bot):
    """
    Waits for a new message and sends the received text back exactly once.
    """
    event = await bot.wait(new_message)
    await bot.api.messages.send(
        peer_id=event['object']['peer_id'],
        message=event['object']['text']
    )


async def echo(bot):
    """
    Waits for new messages and sends the received text back.
    """
    async with bot.sub(new_message) as events:
        async for event in events:
            await bot.api.messages.send(
                peer_id=event['object']['peer_id'],
                message=event['object']['text']
            )


async def main():
    """
    Starts the bot and event handlers.
    """
    access_token = os.getenv('VK_API_ACCESS_TOKEN')
    api = async_vk_api.make_api(access_token, version='5.89')
    bot = async_vk_bot.make_bot(api)

    async with trio.open_nursery() as nursery:
        nursery.start_soon(bot)
        nursery.start_soon(echo, bot)
        nursery.start_soon(echo_once, bot)


if __name__ == '__main__':
    trio.run(main)

For the list of event types and objects see https://vk.com/dev/groups_events.

For more usage see examples.

Bots built with async-vk-bot

About

Async VK bot builder built with trio and async-vk-api.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages