Coder Social home page Coder Social logo

async-vk-bot's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.