def coreplug_help(self, bot, update, user, args): _ = lambda x: core.locale.get_localized(x, update.message.chat.id) if args: for plugin in self.plugins: for command in plugin["commands"]: if args[0].lower() == command["command"].lower(): info = { "command": args[0], "description": _(self.locales["not_available"]), "docs": _(self.locales["not_available"]) } info["description"] = command["description"] if command["function"].__doc__: info["docs"] = html.escape( textwrap.dedent(command["function"].__doc__)) return core.message(_(self.locales["help_format"]) % info, parse_mode="HTML") return core.locale.get_localized( self.locales["unknown_help_command"], update.message.chat.id) else: if update.message.chat.type == "private": return core.message(self.gen_help(update.message.chat.id), parse_mode="HTML") else: keyboard = InlineKeyboardMarkup([[ InlineKeyboardButton( text=_(self.locales["help_button_into_pm"]), url="http://t.me/%s?start=help" % bot.getMe().username) ]]) return core.message(_( self.locales["help_button_into_pm_text"]), inline_keyboard=keyboard)
def unlock(bot, update, user, args): if update.message.chat_id in locked: for admin in update.message.chat.get_administrators(): if admin.user.username == update.message.from_user.username: locked.remove(update.message.chat_id) return core.message("Chat unlocked") else: return core.message("This chat wasnt locked at all")
def command_handle(bot: Bot, update: Update): """ Handles commands """ _ = lambda x: core.locale.get_localized(x, update.message.chat.id) if update.message.reply_to_message and update.message.reply_to_message.photo: update.message.reply_to_message.text = update.message.reply_to_message.caption commanddata = update.message.text.split()[0].split('@') if (len(commanddata) >= 2 and commanddata[1] == bot.username) or (len(commanddata) == 1): pinkyresp = PINKY.handle_command(update) if pinkyresp: bot.send_chat_action(update.message.chat.id, "typing") user = update.message.from_user args = update.message.text.split(" ")[1:] if update.message.reply_to_message is None: message = update.message else: message = update.message.reply_to_message try: reply = pinkyresp(bot, update, user, args) except Exception as e: bot.sendMessage( settings.ADMIN, "Error occured in update:" + "\n<code>%s</code>\n" % html.escape(str(update)) + "Traceback:" + "\n<code>%s</code>" % html.escape(traceback.format_exc()), parse_mode='HTML') reply = core.message(_(PINKY.locales["error_occured"]), failed=True) send_message(bot, update, reply)
def command_handle(bot: Bot, update: Update): """ Handles commands """ _ = lambda x: core.locale.get_localized(x, update.message.chat.id) LOGGER.debug("Handling command") if update.message.reply_to_message and update.message.reply_to_message.photo: update.message.reply_to_message.text = update.message.reply_to_message.caption modloader_response = MODLOADER.handle_command(update) LOGGER.debug(modloader_response) if modloader_response: bot.send_chat_action(update.message.chat.id, "typing") user = update.message.from_user args = update.message.text.split(" ")[1:] try: reply = modloader_response(bot, update, user, args) except Exception: errmsg = _(MODLOADER.locales["error_occured_please_report"] ) % settings.CHAT_LINK if settings.USE_SENTRY: errmsg += _( MODLOADER.locales["sentry_code"] ) % sentry_support.catch_exc( extra_context=update.to_dict(), user_context=update.message.from_user.to_dict()) else: bot.sendMessage( settings.ADMIN, "Error occured in update:" + "\n<code>%s</code>\n" % html.escape(str(update)) + "Traceback:" + "\n<code>%s</code>" % html.escape(traceback.format_exc()), parse_mode='HTML') reply = core.message(errmsg, parse_mode="HTML", failed=True) return send_message(bot, update, reply)
def lock(bot, update, user, args): if update.message.chat_id in locked: return core.message("Chat is already locked") if update.message.chat.type != "private": for admin in update.message.chat.get_administrators(): if admin.user.username == update.message.from_user.username: for admin in update.message.chat.get_administrators(): if admin.user.username == bot.get_me().username: locked.append(update.message.chat_id) return core.message("Chat locked") return core.message("I am not admin of this chat...") return core.message( text="Hey! You are not admin of this chat!", photo="https://pbs.twimg.com/media/C_I2Xv1WAAAkpiv.jpg") else: return core.message("Why would you lock a private converstaion?")
def handler(bot, update, args): _ = lambda x: core.locale.get_localized(x, update.message.chat.id) if update.message.chat.id in self.disabled: return else: state_only_command = update.message.text == command or update.message.text.startswith( command + " ") state_word_swap = len(update.message.text.split( "/")) > 2 and update.message.text.startswith(command) state_mention_command = update.message.text.startswith( command + "@") if state_only_command or state_word_swap or state_mention_command: logging.getLogger("Chat-%s [%s]" % (update.message.chat.title, update.message.chat.id)).info( "User %s [%s] requested %s.", update.message.from_user.username, update.message.from_user.id, update.message.text) if not len(args) < minimal_args: try: reply = function(bot, update, update.message.from_user, args) except Exception as e: bot.sendMessage( settings.ADMIN, "Error occured in update:" + "\n<code>%s</code>\n" % html.escape(str(update)) + "Traceback:" + "\n<code>%s</code>" % html.escape(traceback.format_exc()), parse_mode='HTML') reply = core.message(_( self.locales["error_occured"]), failed=True) else: reply = core.message( _(self.locales["not_enough_arguments"]) % command, parse_mode="HTML") send_message(bot, update, reply)
def coreplug_start(self, bot, update, user, args): _ = lambda x: core.locale.get_localized(x, update.message.chat.id) if len(args) > 0: if args[0] == "help" and update.message.chat.type == "private": return core.message(self.gen_help(update.message.chat.id), parse_mode="HTML") kbd = InlineKeyboardMarkup([ [ InlineKeyboardButton(text=_(self.locales["help_button"]), url="http://t.me/%s?start=help" % bot.getMe().username) ], [ InlineKeyboardButton(text=_(self.locales["news_button"]), url=settings.NEWS_LINK) ], [ InlineKeyboardButton(text=_(self.locales["chat_button"]), url=settings.CHAT_LINK) ], ]) return core.message(_(self.locales["start"]) % bot.getMe().first_name, inline_keyboard=kbd)
def catalog_search(_: Bot, ___: Update, user, args): defnum = 1 term = " ".join(args) try: definition = get_definition(term, defnum) except IndexError: return core.message("Nothing found!", failed=True) else: kbd = InlineKeyboardMarkup([[ InlineKeyboardButton(text="⬅️", callback_data=cmdpref + ":bwd:" + str(defnum) + ":" + term + ":" + str(definition[1])), InlineKeyboardButton(text="1/" + str(definition[1]), callback_data="none"), InlineKeyboardButton(text="➡️", callback_data=cmdpref + ":fwd:" + str(defnum) + ":" + term + ":" + str(definition[1])) ]]) definition = definition[0][0] return core.message(str(definition), parse_mode="HTML", inline_keyboard=kbd)
def hi(bot, update, user, args): return core.message(text="Hi, @%s" % user.username)
def echo(bot, update): return core.message(text=update.message.text)
def inline_button_create(bot, update, user, args): keyboard = InlineKeyboardMarkup( [[InlineKeyboardButton("Test inline", callback_data="hello")]]) return core.message(text="Hi, @%s" % user.username, inline_keyboard=keyboard)