async def duckduckgo(ctx: Context): """Produces simple wikipedia search.""" # This is little hack to avoid create own aiohttp session # don't do like this ;) session: aiohttp.ClientSession = ctx.bot.connector._session params = { 'action': 'opensearch', 'search': ctx.update.inline_query.query, } try: response = await session.get('https://en.wikipedia.org/w/api.php', params=params) result = json.loads(await response.text()) articles = [] for idx in range(len(result[2])): text = f"{result[2][idx]}\n\n{result[3][idx]}" hash = md5(text.encode()).hexdigest() article = InlineQueryResultArticle(id=hash, title=result[1][idx], input_message_content=InputTextMessageContent(message_text=text), description=result[2][idx]) articles.append(article) whr = AnswerInlineQuery(ctx.update.inline_query.query_id, articles) ctx.webhook_request(whr) except (json.decoder.JSONDecodeError, aiohttp.ClientConnectorError): pass
def help_command(ctx: Context): """Handler can also be simple function. But remember - in async environment you shouldn't use here hard synchronous code. This handler also demonstrates how to make webhook-request. If you use webhook executor this will be send as reply of received a webhook. Otherwise bot's router will fallback to send by regular call.""" whr = SendMessage(ctx.update.message.user.user_id, "🔹 Bot's help.\n" "\n" "/start - Print welcome message.\n" "/help - Show this message." "\n" "\n" "/keyboard - Shows keyboard.\n" "/keyboard_location - Shows keyboard with location button.\n" "/keyboard_contact - Shows keyboard with contact button.\n" "/cancel - Removes current keyboard.\n" "\n" "/simple_inline_keyboard - Shows simple inline keyboard.\n" "/arranged_inline_keyboard - Shows how to arrange inline keyboard.\n" "/arranged_scheme_inline_keyboard - Shows how to arrange inline keyboard by scheme.\n" "\n" "/echo - Waiting next request in same handler.\n" "\n" "/inline - Shows how to use inline mode.\n" "\n" "/enigma - Enigma cypher machine") ctx.webhook_request(whr)
def enigma_cmd(ctx: Context): while True: new_id = token_urlsafe(8) if new_id not in ctx.bot.globals.enigmas: break estat = Munch() ctx.bot.globals.enigmas[new_id] = estat estat.input = '' estat.output = '' estat.rotors = 4 estat.rotors_list = ['Beta', 'I', 'II', 'III'] estat.rings_list = ['A', 'A', 'A', 'A'] estat.reflector = 'C-Thin' estat.plugboard = [] estat.display = ['A', 'A', 'A', 'A'] m = MSG.setup.format(setting=MSG.setup_rotors, display=' '.join(estat.display), reflector=estat.reflector, rotors=' '.join(estat.rotors_list), rings=' '.join(estat.rings_list), plugboard=' '.join(estat.plugboard) if len( estat.plugboard) else MSG.plugboard_empty, idisplay=' '.join(estat.display)) whr = SendMessage(ctx.update.message.chat.chat_id, m, reply_markup=rotors_kb(new_id)) ctx.webhook_request(whr)
async def enigma_setup_rings(ctx: Context): estat_id, command, letter = ctx.update.callback_query.data.split()[1:] estat = ctx.bot.globals.enigmas.get(estat_id) if estat is None: await ctx.bot.edit_message_reply_markup(chat_id=ctx.update.callback_query.message.chat.chat_id, message_id=ctx.update.callback_query.message.message_id) whr = AnswerCallbackQuery(ctx.update.callback_query.query_id, MSG.expired) ctx.webhook_request(whr) return whr = AnswerCallbackQuery(ctx.update.callback_query.query_id) ctx.webhook_request(whr) if command == 'letter': if letter not in KEYBOARD_CHARS: return t = list(estat.rings_list) estat.rings_list.pop(0) estat.rings_list.append(letter) if t == estat.rings_list: return if command == 'done': m = MSG.setup.format(setting=MSG.setup_plugboard, display=' '.join(estat.display), reflector=estat.reflector, rotors=' '.join(estat.rotors_list), rings=' '.join(estat.rings_list), plugboard=' '.join(estat.plugboard) if len( estat.plugboard) else MSG.plugboard_empty, idisplay=' '.join(estat.display)) kb = enigma_kb(estat_id, 'enigma-plugboard') await ctx.bot.edit_message_text(chat_id=ctx.update.callback_query.message.chat.chat_id, message_id=ctx.update.callback_query.message.message_id, text=m, reply_markup=kb) return m = MSG.setup.format(setting=MSG.setup_rings, display=' '.join(estat.display), reflector=estat.reflector, rotors=' '.join(estat.rotors_list), rings=' '.join(estat.rings_list), plugboard=' '.join(estat.plugboard) if len( estat.plugboard) else MSG.plugboard_empty, idisplay=' '.join(estat.display)) kb = enigma_kb(estat_id, 'enigma-rings') await ctx.bot.edit_message_text(chat_id=ctx.update.callback_query.message.chat.chat_id, message_id=ctx.update.callback_query.message.message_id, text=m, reply_markup=kb)
def unknown(ctx: Context): """\ This is about how to use priority. This handler caches all messages to bot, but since we set priority to 2048 handler will be called if no other handlers was do. Default priority in Dispatcher is 1024, so for set the order of handlers you can use @priority decorator.""" whr = SendMessage(ctx.update.message.user.user_id, "🔹 I don't known what to do. May be /help?") ctx.webhook_request(whr)
def inline_photo(ctx: Context): """Shows how to send photo though inline.""" photos = [ InlineQueryResultPhoto('1', 'https://telegra.ph/file/a225c61d9354bb0fc1241.jpg', 'https://telegra.ph/file/f9023fd8e7fc222ab33f1.jpg'), InlineQueryResultPhoto('2', 'https://telegra.ph/file/64cf1343413cdc9f5256e.jpg', 'https://telegra.ph/file/581a6262d4a2513370d02.jpg'), InlineQueryResultPhoto('3', 'https://telegra.ph/file/46b85c82aa644ed3db1f5.jpg', 'https://telegra.ph/file/231a77c904870951f0d19.jpg'), InlineQueryResultPhoto('4', 'https://telegra.ph/file/bf6dd32105d795f4d8c84.jpg', 'https://telegra.ph/file/64696eb4513e9f6c1ca9f.jpg'), InlineQueryResultPhoto('5', 'https://telegra.ph/file/898227042e0021df169d4.jpg', 'https://telegra.ph/file/84bdc622437d376e718c5.jpg'), ] whr = AnswerInlineQuery(ctx.update.inline_query.query_id, photos) ctx.webhook_request(whr)
async def echo(ctx: Context): """Shows how to use waiter.""" msg = "🔹 Now i will send you back all messages in raw foramat like @ShowJsonBot.\n\n" \ "Hit /cancel to exit." whr = SendMessage(ctx.update.message.chat.chat_id, msg) ctx.webhook_request(whr) while True: # here waiting next request # this is python's async generator ctx: Context = (yield next_all()) if ctx.update.message.text == '/cancel': whr = SendMessage(ctx.update.message.chat.chat_id, "🔹 Ok! See you later!") ctx.webhook_request(whr) return # print reminder every five updates if not ctx.update.update_id % 5: await ctx.bot.send_message( ctx.update.message.chat.chat_id, "🔹 I am in <code>echo</code> mode. Hit /cancel to exit.") prepared = escape.html( json.dumps(ctx.update.raw, ensure_ascii=False, indent=1)) whr = SendMessage(ctx.update.message.chat.chat_id, f"<code>{prepared}</code>", reply_to_message_id=ctx.update.message.message_id) ctx.webhook_request(whr)
async def enigma_setup_rotors(ctx: Context): estat_id, command, letter = ctx.update.callback_query.data.split()[1:] estat = ctx.bot.globals.enigmas.get(estat_id) if estat is None: await ctx.bot.edit_message_reply_markup(chat_id=ctx.update.callback_query.message.chat.chat_id, message_id=ctx.update.callback_query.message.message_id) whr = AnswerCallbackQuery(ctx.update.callback_query.query_id, MSG.expired) ctx.webhook_request(whr) return whr = AnswerCallbackQuery(ctx.update.callback_query.query_id) ctx.webhook_request(whr) if command == 'rotor': if letter not in ROTORS.keys(): return t = list(estat.rotors_list) estat.rotors_list.pop(0) estat.rotors_list.append(letter) if t == estat.rotors_list: return if command == 'reflector': if letter not in REFLECTORS.keys(): return if letter == estat.reflector: return estat.reflector = letter if command == '3rotors': if estat.rotors == 3: return estat.rotors = 3 if len(estat.rotors_list) != 3: estat.rotors_list.pop(0) estat.rings_list = ['A', 'A', 'A'] estat.display = ['A', 'A', 'A'] if command == '4rotors': if estat.rotors == 4: return estat.rotors = 4 if len(estat.rotors_list) != 4: estat.rotors_list.insert(0, 'I') estat.rings_list = ['A', 'A', 'A', 'A'] estat.display = ['A', 'A', 'A', 'A'] if command == 'done': m = MSG.setup.format(setting=MSG.setup_rings, display=' '.join(estat.display), reflector=estat.reflector, rotors=' '.join(estat.rotors_list), rings=' '.join(estat.rings_list), plugboard=' '.join(estat.plugboard) if len( estat.plugboard) else MSG.plugboard_empty, idisplay=' '.join(estat.display)) kb = enigma_kb(estat_id, 'enigma-rings') await ctx.bot.edit_message_text(chat_id=ctx.update.callback_query.message.chat.chat_id, message_id=ctx.update.callback_query.message.message_id, text=m, reply_markup=kb) return m = MSG.setup.format(setting=MSG.setup_rotors, display=' '.join(estat.display), reflector=estat.reflector, rotors=' '.join(estat.rotors_list), rings=' '.join(estat.rings_list), plugboard=' '.join(estat.plugboard) if len( estat.plugboard) else MSG.plugboard_empty, idisplay=' '.join(estat.display)) await ctx.bot.edit_message_text(chat_id=ctx.update.callback_query.message.chat.chat_id, message_id=ctx.update.callback_query.message.message_id, text=m, reply_markup=rotors_kb(estat_id))
async def enigma_setup_plugboard(ctx: Context): estat_id, command, letter = ctx.update.callback_query.data.split()[1:] estat = ctx.bot.globals.enigmas.get(estat_id) if estat is None: await ctx.bot.edit_message_reply_markup(chat_id=ctx.update.callback_query.message.chat.chat_id, message_id=ctx.update.callback_query.message.message_id) whr = AnswerCallbackQuery(ctx.update.callback_query.query_id, MSG.expired) ctx.webhook_request(whr) return if command == 'letter': if letter not in KEYBOARD_CHARS: return if letter in estat.plugboard: whr = AnswerCallbackQuery(ctx.update.callback_query.query_id, MSG.plugboard_error) ctx.webhook_request(whr) return if len(estat.plugboard) < 1: estat.plugboard.append('') if len(estat.plugboard[-1]) == 2: estat.plugboard.append('') estat.plugboard[-1] += letter if len(estat.plugboard) > 10: estat.plugboard.pop(0) if command == 'del': if len(estat.plugboard) < 1: return estat.plugboard.pop(-1) whr = AnswerCallbackQuery(ctx.update.callback_query.query_id) ctx.webhook_request(whr) if command == 'done': if len(estat.plugboard) > 0 and len(estat.plugboard[-1]) < 2: whr = AnswerCallbackQuery(ctx.update.callback_query.query_id, MSG.plugboard_error) ctx.webhook_request(whr) return m = MSG.setup.format(setting=MSG.setup_display, display=' '.join(estat.display), reflector=estat.reflector, rotors=' '.join(estat.rotors_list), rings=' '.join(estat.rings_list), plugboard=' '.join(estat.plugboard) if len( estat.plugboard) else MSG.plugboard_empty, idisplay=' '.join(estat.display)) kb = enigma_kb(estat_id, 'enigma-display') await ctx.bot.edit_message_text(chat_id=ctx.update.callback_query.message.chat.chat_id, message_id=ctx.update.callback_query.message.message_id, text=m, reply_markup=kb) return m = MSG.setup.format(setting=MSG.setup_plugboard, display=' '.join(estat.display), reflector=estat.reflector, rotors=' '.join(estat.rotors_list), rings=' '.join(estat.rings_list), plugboard=' '.join(estat.plugboard) if len( estat.plugboard) else MSG.plugboard_empty, idisplay=' '.join(estat.display)) kb = enigma_kb(estat_id, 'enigma-plugboard', ''.join(estat.plugboard)) await ctx.bot.edit_message_text(chat_id=ctx.update.callback_query.message.chat.chat_id, message_id=ctx.update.callback_query.message.message_id, text=m, reply_markup=kb)
async def enigma_act(ctx: Context): estat_id, command, letter = ctx.update.callback_query.data.split()[1:] estat = ctx.bot.globals.enigmas.get(estat_id) if estat is None: await ctx.bot.edit_message_reply_markup(chat_id=ctx.update.callback_query.message.chat.chat_id, message_id=ctx.update.callback_query.message.message_id) whr = AnswerCallbackQuery(ctx.update.callback_query.query_id, MSG.expired) ctx.webhook_request(whr) return whr = AnswerCallbackQuery(ctx.update.callback_query.query_id) ctx.webhook_request(whr) if command == 'set': estat.input = '' m = MSG.setup.format(setting=MSG.setup_display, display=' '.join(estat.display), reflector=estat.reflector, rotors=' '.join(estat.rotors_list), rings=' '.join(estat.rings_list), plugboard=' '.join(estat.plugboard) if len( estat.plugboard) else MSG.plugboard_empty, idisplay=' '.join(estat.display)) kb = enigma_kb(estat_id, 'enigma-display') await ctx.bot.edit_message_text(chat_id=ctx.update.callback_query.message.chat.chat_id, message_id=ctx.update.callback_query.message.message_id, text=m, reply_markup=kb) return machine = EnigmaMachine.from_key_sheet( rotors=estat.rotors_list, reflector=estat.reflector, ring_settings=' '.join(estat.rings_list), plugboard_settings=' '.join(estat.plugboard)) machine.set_display(''.join(estat.display)) if command == 'bksp': if not len(estat.input): return estat.input = estat.input[:-1] else: estat.input += letter inp = ''.join([l if (n+1) % 5 else l+' ' for n, l in enumerate(estat.input)]) out = ''.join([l if (n+1) % 5 else l+' ' for n, l in enumerate(machine.process_text(estat.input))]) display = list(machine.get_display()) if len(display) < 4 and estat.rotors == 4: display.insert(0, estat.display[0]) display = ' '.join(display) m = MSG.enigma.format(display=display, input=inp, output=out, reflector=estat.reflector, rotors=' '.join(estat.rotors_list), rings=' '.join(estat.rings_list), plugboard=' '.join(estat.plugboard) if len(estat.plugboard) else MSG.plugboard_empty, idisplay=' '.join(estat.display)) await ctx.bot.edit_message_text(chat_id=ctx.update.callback_query.message.chat.chat_id, message_id=ctx.update.callback_query.message.message_id, text=m, reply_markup=enigma_kb(estat_id))