Ejemplo n.º 1
0
from vk.utils import TaskManager
from vk.bot_framework import Dispatcher
from vk.bot_framework import get_group_id
from utils import get_vk
from utils import PRODUCTION
import logging
from middlewares import MessageCheckMiddleware
from blueprints import generate_bp, info_bp, remember_bp, clear_bp, invite_bp
import os

logging.basicConfig(level=logging.DEBUG)

vk = get_vk()
dp = Dispatcher(vk)


async def run():
    dp.setup_blueprint(invite_bp)
    dp.setup_blueprint(generate_bp)
    dp.setup_blueprint(info_bp)
    dp.setup_blueprint(clear_bp)
    dp.setup_blueprint(remember_bp)
    dp.setup_middleware(MessageCheckMiddleware())
    if PRODUCTION:
        try:
            import starlette
            import aio_pika
            import uvicorn
        except ImportError:
            print("Для использования PRODUCTION мода вам нужно установить"
                  " дополнительные библиотеки: starlette, aio_pika, uvicorn")
Ejemplo n.º 2
0
import logging

from vk import types
from vk import VK
from vk.bot_framework import Dispatcher
from vk.utils import TaskManager

logging.basicConfig(level="INFO")

bot_token = "123"
vk = VK(bot_token)
gid = 123
task_manager = TaskManager(vk.loop)
api = vk.get_api()

dp = Dispatcher(vk, gid)


@dp.message_handler(with_fwd_messages=True, count_fwd_messages=3)
async def handle(message: types.Message, data: dict):
    await message.reply("Test!")


@dp.message_handler(with_reply_message=True)
async def handle_event(message: types.Message, data: dict):
    await message.reply("Hello!")


async def run():
    dp.run_polling()
Ejemplo n.º 3
0
from vk import VK
from vk.utils import TaskManager
from vk.bot_framework import Dispatcher
from vk.constants import JSON_LIBRARY
import asyncio
import logging
from io import BytesIO
from aiogtts import aiogTTS
import aiohttp

with open("config.json", "r") as config:
    config = JSON_LIBRARY.load(config)

logging.basicConfig(level=config["logging_level"],
                    format="%(asctime)s - %(levelname)s:: %(message)s",
                    handlers=[
                        logging.FileHandler("{0}/{1}".format(
                            config["logging_path"], config["logging_file"])),
                        logging.StreamHandler()
                    ])

vk = VK(config["token"])
task_manager = TaskManager(vk.loop)
dp = Dispatcher(vk, config["group_id"])
api = vk.get_api()
aiogtts = aiogTTS()
Ejemplo n.º 4
0
from vk.bot_framework import Dispatcher, get_group_id
from vk.bot_framework.dispatcher.rule import NamedRule

from vk import types

import logging

logging.basicConfig(level="INFO")

bot_token = "123"
vk = VK(bot_token)
VK.set_current(vk)
task_manager = TaskManager(vk.loop)
api = vk.get_api()

dp = Dispatcher(vk)


class Commands(NamedRule):
    key = "commands"
    """
    Own implementaion of commands rule.
    """
    def __init__(self, commands):
        self.commands = commands
        self.prefix = "!"

    async def check(self, message: types.Message, data: dict):
        text = message.text.lower()
        _accepted = False
        for command in self.commands:
Ejemplo n.º 5
0
from vk import types
from vk import VK
from vk.bot_framework import Dispatcher, get_group_id
from vk.bot_framework import Storage
from vk.bot_framework.addons.caching import cached_handler
from vk.bot_framework.storages import RedisStorage
from vk.utils import TaskManager

logging.basicConfig(level="INFO")

bot_token = "123"
vk = VK(bot_token)
task_manager = TaskManager(vk.loop)
api = vk.get_api()

dp = Dispatcher(vk)
redis_storage: RedisStorage = RedisStorage("redis://localhost",
                                           vk.loop)  # create redis
storage = Storage()  # create base storage for place any

dp.storage = storage


@dp.message_handler(text="text")
@cached_handler(redis_storage, expire=20, for_specify_user=True)
async def test(msg: types.Message, data):
    resp = await redis_storage.get("hello", 0)
    await redis_storage.update("hello", int(resp) + 1)
    return await msg.cached_answer(f"{resp}")

Ejemplo n.º 6
0
from vk import VK
from vk.utils import TaskManager
from vk.bot_framework import Dispatcher, get_group_id
from vk.bot_framework import BaseMiddleware, SkipHandler
from vk import types

import logging

logging.basicConfig(level="INFO")

bot_token = "token"
vk = VK(bot_token)
task_manager = TaskManager(vk.loop)
api = vk.get_api()

dp = Dispatcher(vk)


class MyMiddleware(BaseMiddleware):
    async def pre_process_event(self, event, data: dict):
        print("Called before handlers!")
        if event.type != "message_new":
            raise SkipHandler
        data["my_message"] = "hello, handler!"
        return data

    async def post_process_event(self):
        print("Called after handlers!")


@dp.message_handler(text="hello!", data_check={"my_message": "hello, handler!"})
Ejemplo n.º 7
0
from vk.utils import TaskManager
from vk.bot_framework import Dispatcher, rules
from vk.bot_framework import BaseRule, BaseMiddleware
from vk import types

import logging

logging.basicConfig(level="INFO")

bot_token = "token"
vk = VK(bot_token)
gid = 123
task_manager = TaskManager(vk.loop)
api = vk.get_api()

dp = Dispatcher(vk, gid)

USERS = {}  # schema - id: status


class RegistrationMiddleware(BaseMiddleware):
    """
    Register users in bot.
    """
    async def pre_process_event(self, event, data: dict):
        if event["type"] == "message_new":
            from_id = event["object"]["from_id"]
            if from_id not in USERS:
                USERS[from_id] = "user"
        return data
Ejemplo n.º 8
0
import logging

from vk import VK
from vk.bot_framework import Dispatcher
from vk.utils import TaskManager

from shuecm.config import VK_GROUP_ID
from shuecm.config import VK_TOKEN
from shuecm.models.prestart import pre_start as pre_start_db

logging.basicConfig(level="INFO")
vk = VK(VK_TOKEN)
dp = Dispatcher(vk, VK_GROUP_ID)


async def run():
    from shuecm.blueprints import info_bp

    dp.setup_blueprint(info_bp)

    from shuecm.middlewares import RegistrationMiddleware

    dp.setup_middleware(RegistrationMiddleware())

    pre_start_db(vk.loop)
    dp.run_polling()


if __name__ == "__main__":
    manager = TaskManager(vk.loop)
    manager.add_task(run)
Ejemplo n.º 9
0
from vk import types
from vk import VK
from vk.bot_framework import Dispatcher
from vk.bot_framework import Storage
from vk.bot_framework.storages import RedisStorage
from vk.utils import TaskManager

logging.basicConfig(level="INFO")

bot_token = "123"
vk = VK(bot_token)
gid = 123
task_manager = TaskManager(vk.loop)
api = vk.get_api()

dp = Dispatcher(vk, gid)
redis_storage: RedisStorage = RedisStorage("redis://localhost",
                                           vk.loop)  # create redis
storage = Storage()  # create base storage for place any

dp.storage = storage


@dp.message_handler(text="hello")
async def handle_event(message: types.Message, data: dict):
    redis: RedisStorage = await dp.storage.get(
        "redis")  # you have access to redis storage
    c = await redis.get("really_needed_counter", 0)  # equal API
    await message.answer(f"Hello! {c}")
    await redis.update("really_needed_counter", int(c) + 1)