def start(bot, update): user_id = update.message.from_user.id user = update.message.from_user db = sqlitedb.get_instance() keyboard = [[InlineKeyboardButton("❌Stop Searching❌", callback_data='1')]] keyboard1 = [[InlineKeyboardButton("❌Left the Chat❌", callback_data='1')]] reply_markup = telegram.ReplyKeyboardMarkup(keyboard, resize_keyboard=True) reply_markup1 = telegram.ReplyKeyboardMarkup(keyboard1, resize_keyboard=True) bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING) if user_id in db.get_banned_users(): bot.send_message( user_id, "{} You have been banned from using this bot!".format(BOT_SENDS), parse_mode="Markdown") return db.add_user(user.id, "en", user.first_name, user.last_name, user.username) if (user_id not in searching_users) and (user_already_chatting(user_id) == -1): # search for another "searching" user in searching_users list if len(searching_users) > 0: # delete the other searching users from the list of searching_users logger.debug( "Another user is searching now. There are 2 users. Matching them now!" ) with lock: partner_id = searching_users[0] del searching_users[0] # add both users to the list of chatting users with the user_id of the other user. chatting_users.append([user_id, partner_id]) chatting_users.append([partner_id, user_id]) text = "❇️ You are connected to a stranger. Have fun and be nice! Skip stranger with /next. ❇️" # bot.send_message(user_id, "{} {}".format(BOT_SENDS, text), parse_mode="Markdown") # bot.send_message(partner_id, "{} {}".format(BOT_SENDS, text), parse_mode="Markdown") bot.send_message(user_id, text, reply_markup=reply_markup1) bot.send_message(partner_id, text, reply_markup=reply_markup1) else: # if no user is searching, add him to the list of searching users. # TODO later when you can search for specific gender, this condition must be changed searching_users.append(user_id) # bot.send_message(user_id, "{} {}".format(BOT_SENDS, "Searching for strangers!"), parse_mode="Markdown") bot.send_message(chat_id=update.message.chat_id, text="❇️ Searching for strangers! ❇️", reply_markup=reply_markup) elif user_id in searching_users: bot.send_message(user_id, "{} {}".format( BOT_SENDS, "You are already searching. Please wait!"), parse_mode="Markdown")
def broadcast(bot, update, args): """Sends a broadcast message to all known users""" if len(args) == 0: return text = " ".join(args) db = sqlitedb.get_instance() users = db.get_all_users() print(users) for user_id in users: bot.send_message(user_id, "{} {}".format(BOT_BROADCAST, text), parse_mode="Markdown")
def stop(bot, update): user_id = update.message.from_user.id db = sqlitedb.get_instance() keyboard = [[ InlineKeyboardButton("💥New Chat💥", callback_data='1'), InlineKeyboardButton("🍴Settings🍴", callback_data='2') ], [InlineKeyboardButton("🚀Help & Info🚀", callback_data='3')]] bot.send_chat_action(chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING) reply_markup = telegram.ReplyKeyboardMarkup(keyboard, resize_keyboard=True) if user_id in db.get_banned_users(): bot.send_message( user_id, "{} You have been banned from using this bot!".format(BOT_SENDS), parse_mode="Markdown") return if (user_id in searching_users) or (user_already_chatting(user_id) >= 0): if user_id in searching_users: # remove user from searching users index = user_already_searching(user_id) del searching_users[index] bot.send_message(chat_id=update.message.chat_id, text="📛 At this moment you are not searching 📛", reply_markup=reply_markup) elif user_already_chatting(user_id) >= 0: # remove both users from chatting users partner_id = get_partner_id(user_id) index = user_already_chatting(user_id) del chatting_users[index] partner_index = user_already_chatting(partner_id) del chatting_users[partner_index] # send message that other user left the chat # bot.send_message(partner_id, "{} {}".format(BOT_SENDS, "Your partner left the chat"), parse_mode="Markdown") # bot.send_message(user_id, "{} {}".format(BOT_SENDS, "You left the chat!"), parse_mode="Markdown") bot.send_message(partner_id, text="🎈 Your partner left the chat 🎈", reply_markup=reply_markup, parse_mode="Markdown") bot.send_message(user_id, text="🎈 You left the chat! 🎈", reply_markup=reply_markup)
def unban(bot, update, args): """Unbans a user from using this bot""" if len(args) == 0: return db = sqlitedb.get_instance() banned_user_id = args[0] logger.info("Unbanning user {}".format(banned_user_id)) if not re.match("[0-9]+", banned_user_id): update.message.reply_text( "{} UserID is in invalid format!".format(BOT_SENDS), parse_mode="Markdown") return db.unban(banned_user_id) update.message.reply_text("{} Unbanned user {}".format( BOT_SENDS, banned_user_id), parse_mode="Markdown")