class ShoppingBot(object): instance = None def __init__(self, verbose=False): assert ShoppingBot.instance is None ShoppingBot.instance = self self.verbose = verbose self.bot = None self.loop = aio.get_event_loop() self.msg_loop = None self.callbacks = {} def start(self, token): self.bot = telepot.aio.DelegatorBot(token, [ include_callback_query_chat_id(pave_event_space())( per_chat_id(), create_open, ShoppingUser, timeout=10), ]) self.msg_loop = MessageLoop(self.bot) self.loop.create_task(self.msg_loop.run_forever()) self.loop.run_forever() def stop(self): ShoppingBot.instance = None if self.msg_loop: self.msg_loop.cancel() self.msg_loop = None @classmethod def is_verbose(cls): return cls.instance.verbose @classmethod def add_callback(cls, event, cb): assert event in ('cmd', 'add-product', 'flag-product', 'end') cls.instance.callbacks[event] = cls.instance.callbacks.get(event, []) cls.instance.callbacks[event].append(cb) @classmethod async def trigger(cls, event, user, *args, **kwargs): assert event in ('cmd', 'add-product', 'flag-product', 'end') result = True for cb in cls.instance.callbacks.get(event, []): cur = await cb(cls.instance.bot, user, *args, **kwargs) result = result & (cur is None or cur) if result is False: return False return result def __enter__(self): return self def __exit__(self, type, value, tb): self.stop()
def _initialise(bot): if not _telesync_config(bot): return if not bot.memory.exists(['telesync']): bot.memory.set_by_path(['telesync'], {'ho2tg': {}, 'tg2ho': {}}) bot.memory.save() if not bot.memory.exists(['profilesync']): bot.memory.set_by_path(['profilesync'], {'ho2tg': {}, 'tg2ho': {}}) bot.memory.save() global client_session global tg_bot global tg_loop client_session = aiohttp.ClientSession() tg_bot = TelegramBot(bot) tg_bot.set_on_message_callback(tg_on_message) tg_bot.set_on_photo_callback(tg_on_photo) tg_bot.set_on_sticker_callback(tg_on_sticker) tg_bot.set_on_user_join_callback(tg_on_user_join) tg_bot.set_on_user_leave_callback(tg_on_user_leave) tg_bot.set_on_location_share_callback(tg_on_location_share) tg_bot.set_on_supergroup_upgrade_callback(tg_on_supergroup_upgrade) tg_bot.add_command("/whoami", tg_command_whoami) tg_bot.add_command("/whereami", tg_command_whereami) tg_bot.add_command("/setsyncho", tg_command_set_sync_ho) tg_bot.add_command("/clearsyncho", tg_command_clear_sync_ho) tg_bot.add_command("/addadmin", tg_command_add_bot_admin) tg_bot.add_command("/removeadmin", tg_command_remove_bot_admin) tg_bot.add_command("/syncprofile", tg_command_sync_profile) tg_bot.add_command("/unsyncprofile", tg_command_unsync_profile) tg_bot.add_command("/getme", tg_command_get_me) tg_loop = MessageLoop(tg_bot) plugins.start_asyncio_task(tg_loop.run_forever()) plugins.start_asyncio_task(tg_bot.setup_bot_info()) plugins.register_admin_command(["telesync"]) plugins.register_user_command(["syncprofile"]) plugins.register_handler(_on_membership_change, type="membership")
elif vreveal >= vexpand: message = "Avete deciso di rivelare i nomi utenti!\n" for user in waffle.users: message += "- {user.icon} era {user}\n" message = "Il Waffle è stato sciolto.\n" \ "Se vi siete divertiti, create un gruppo normale!" await waffle.message(b, message) session.delete(waffle) session.commit() else: waffle.status = WaffleStatus.MATCHMAKING session.commit() await waffle.message( b, "Avete deciso di espandere il Waffle!\n" "Ricerca di altri giocatori in corso...\n" "Attendete, per piacere!") ml = MessageLoop(b, on_message) l.create_task(ml.run_forever()) l.create_task(matchmaking(900)) reloading = session.query(Waffle).filter( Waffle.status == WaffleStatus.CHATTING).all() for waffle in reloading: l.create_task(votes(21600, waffle.id)) l.create_task( waffle.message( b, "Oops! Il bot è stato riavviato e il timer della votazione è ricominciato.\n" "La votazione finirà tra 6 ore.")) l.run_forever()