def send_help(update, text, keyboard=None): logger.info("into send_help") if not keyboard: pass update.effective_message.reply_text(text=text, parse_mode=ParseMode.MARKDOWN, reply_markup=keyboard)
def get_help(update, context): logger.info("into get_help") chat = update.effective_chat threading.Thread(target=user_collect, args=(chat, ), daemon=True).start() threading.Thread(target=command_collect, args=("help", ), daemon=True).start() context.bot.send_chat_action(chat_id=chat.id, action=ChatAction.TYPING) # ONLY send help in PM if chat.type != chat.PRIVATE: if len(context.args) >= 1 and any(context.args[0].lower() == x for x in HELPER_SCRIPTS): module = context.args[0].lower() send_help( update, "Contact me in PM to get the list of possible commands.", InlineKeyboardMarkup([[ InlineKeyboardButton(text="Help for {}".format(module), url="t.me/{}?start={}".format( context.bot.username, module)) ]])) else: send_help( update, "Contact me in PM to get the list of possible commands.", InlineKeyboardMarkup([[ InlineKeyboardButton(text="Help", url="t.me/{}?start=help".format( context.bot.username)) ]])) return elif len(context.args) >= 1 and any(context.args[0].lower() == x for x in HELPER_SCRIPTS): module = context.args[0].lower() text = "Here is the available help for the *{}* module:\n".format( module) + HELPER_SCRIPTS[module] send_help( update, text, InlineKeyboardMarkup([[ InlineKeyboardButton(text="Back", callback_data="help_back") ]])) else: button_list = [] for module in HELPER_SCRIPTS: button_list.append( InlineKeyboardButton( text="/{}".format(module), callback_data="help_action={}".format(module), )) reply_markup_keyboard = InlineKeyboardMarkup( button_menu.build_menu(button_list, n_cols=2)) send_help(update=update, text=HELP_STRINGS, keyboard=reply_markup_keyboard)
def command_collect(command): db_commands = db().commands query = {"command": command} found = db_commands.find_one(query) if found: new = {"$set": {"command": command, "count": found["count"] + 1}} db_commands.update_one(query, new) else: db_commands.insert_one({"command": command, "count": 1}) logger.info("[+] Command collect {}".format(query))
def user_collect(chat): user = get_user_info(chat) db_users = db().users query = {"id": user['id']} new = {"$set": user, "$unset": {"blocked": True}} # if user exists update user object else create a new object db_users.update_many(query, new, upsert=True) logger.info("[+] USer collect {}".format(query))
def text_handler(update, context): logger.info("into get_stats") chat = update.effective_chat threading.Thread(target=user_collect, args=(chat,), daemon=True).start() threading.Thread(target=command_collect, args=("text",), daemon=True).start() # ONLY send in PM if chat.type != chat.PRIVATE: return else: context.args = [] get_help(update, context)
def run(self): if self.mode == "dev": logger.info("Starting Polling Method...") self.updater.start_polling(timeout=15, read_latency=4) elif self.mode == "prod": webhook_url = "https://{}.{}/{}".format(self.APP_NAME, self.domain, self.TOKEN) self.updater.start_webhook(listen="0.0.0.0", port=self.PORT, url_path=self.TOKEN) self.updater.bot.set_webhook(webhook_url) logger.info("Webhook set on {}".format(webhook_url)) else: logger.error("No MODE specified!") sys.exit(1) self.updater.idle()
def get_stats_from_db(): logger.info("[^] DB Stats") total_users = db().users.find().count() active_users = db().users.find({"blocked": {"$ne": True}}).count() total_queries = db().queries.find().count() successful_queries = db().queries.find({"available": True}).count() stats = { 'users': { 'total_users': total_users, 'active_users': active_users, }, 'queries': { 'total_queries': total_queries, 'successful_queries': successful_queries } } return stats
def feedback_collect(chat, feedback_str): user = get_user_info(chat) feedback = db().feedback feedback_obj = {'user': user, 'feedback': feedback_str} res = feedback.insert_one(feedback_obj) logger.info("[+] Feedback collect {}{}".format(res, feedback_str))
def query_collect(query_as_dict): queries = db().queries res = queries.insert_one(query_as_dict) logger.info("[+] Query collect {}{}".format(res, query_as_dict))