def __init__(self): self.config = Config() self.bot = aiotg.Bot(api_token=self.config.token) self.bot.add_command(r"^/start", self.handle_start) self.bot.add_command(r"^/status", self.handle_status) self.bot.add_command(r"^/guide", self.handle_guide) self.bot.add_command(r"^/register", self.handle_register) self.bot.add_command(r"^/unregister", self.handle_unregister) self.app = aiohttp.web.Application() self.app.router.add_post("/{uuid}", self.handle_webhook)
def create_bot(redis): bot = aiotg.Bot(BotConfig.TOKEN, name=BotConfig.NAME) restricted_deco = restricted(redis) commands = [ (set_boss, r'^/setboss'), (botta, r'^/botta'), (lista_botta, r'^/listabotta'), (stats_button_reply_phase1, '^/stats'), (set_dungeon, r'^Sei stato aggiunto alla Lista Avventurieri del dungeon (.*)!'), (close_dungeon, r'^/quitdg'), (get_map, r'^/mappa'), (log_user_position, r'Stanza (\d+)/(\d+)'), (log_user_direction, rf"({Emoji.ARROW_UP}|{Emoji.ARROW_LEFT}|{Emoji.ARROW_RIGHT})"), (next_room, r'^/next'), (get_current_dungeon, r'^/dungeon'), (trade_dungeon, r'^/scambio'), (expire_dungeon, r'^/cancelladg'), (map_todo, r'^/todo'), (set_custom_emojis, r'^#emojis mappa'), (get_custom_emojis, r'/getemojis') # (set_expire_date, r'') ] dungeon_commands = [(log_user_action, '^' + string) for string in Dungeon.RE] commands += dungeon_commands callbacks = [(stats_choice_phase1, 'stats1click-(.+)'), (stats_choice_phase2, 'stats2click-(.+)'), (map_next, 'mapclick-(.+)'), (dungeon_exchange, 'tradedgclick-(.+)'), (confirm_trade, 'confirmtradeclick-(.+)')] for fn, re in commands: bot.add_command(re, restricted_deco(fn)) for fn, re in callbacks: bot.add_callback(re, restricted_deco(fn)) return bot
import os import sys import logging import asyncio import aiotg import redis api_token = os.environ.get('API_TOKEN') bot_name = os.environ.get('BOT_NAME') bot = aiotg.Bot(api_token=api_token, name=bot_name) channel = bot.channel(os.environ.get('CHANNEL_NAME', '@BozorboyNews')) redis_url = os.environ.get('REDIS_URL', 'redis://localhost:6379/0') logger = logging.getLogger('bot') logging.basicConfig(level=logging.DEBUG) async def main(): r = redis.from_url(redis_url) post = r.spop('unpublished_posts') if not post: sys.exit(0) text = post.decode('utf-8') if not r.sismember('published_posts', text): logger.info('Publishing post {text} to channel...'.format(text=text)) await channel.send_text(text, disable_web_page_preview=True) r.sadd('published_posts', text) loop = asyncio.get_event_loop()
import asyncio import os import re from datetime import datetime, timedelta from enum import IntEnum from functools import partial from typing import Union import aiotg from sqlalchemy.sql.functions import now from tasksbot.models import Task from tasksbot.models.chat import Chat bot = aiotg.Bot( api_token=os.environ.get('TG_BOT_TOKEN', ''), default_in_groups=True, ) class ChatState(IntEnum): NORMAL = 0 EXPECT_TASK = 1 EXPECT_PERIOD = 2 EXPECT_TIME_WHEN_SEND_NOTIFY = 3 EXPECT_TASK_TIME_WHEN_SEND_NOTIFY = 4 GREETING_BY_STATE = [ '', 'Вводи новое дело', "Вводи период",
from copy import copy import aiotg import matplotlib matplotlib.use('Agg') # noqa from airmon import const, forecast, storage, chart, date from airmon.storage.models import bind_db BOT_TOKEN = os.environ.get('TG_BOT_TOKEN') BOT_NAME = os.environ.get('TG_BOT_NAME') assert all([BOT_TOKEN, BOT_NAME]) _LAST_ALERT = 0 bot = aiotg.Bot(api_token=BOT_TOKEN, name=BOT_NAME) async def help_msg(chat): msg = ('/help - display help message\n' '/subscribe - be alerted if forecast goes bad\n' '/unsubscribe - stop receiving alerts\n' '/fire - emit a test alert\n' '/stats3 - renders chart for last 3 hours\n' '/stats6 - renders chart for last 6 hours\n' '/stats12 - renders chart for last 12 hours\n' '/stats24 - renders chart for last 24 hours\n') return await chat.send_text(msg) @bot.command(r'/help')
import asyncio import os import textwrap from collections import defaultdict, deque from concurrent.futures import ThreadPoolExecutor from datetime import datetime from functools import partial import aiotg import validators from .screenshot import do_screenshot bot = aiotg.Bot(api_token=os.getenv('TG_BOT_TOKEN')) queue = defaultdict(partial(deque, maxlen=10)) pool = ThreadPoolExecutor(max_workers=2) @bot.command('^/(start|help)$') async def echo(chat: aiotg.Chat, match): await chat.send_text( textwrap.dedent(""" bleep bloop I'm a bot 🤖 Throw me an URL and I'll screenshot it for you. """)) @bot.command('(.+)') async def screenshot(chat: aiotg.Chat, match): loop = asyncio.get_event_loop()
def create_bot(): telegram_bot = aiotg.Bot(os.environ["API_TOKEN"]) telegram_bot.clients = {} return telegram_bot