def has_list_id(command, update, lists): list_id = extract_list_id(command) if (str(list_id) in lists): return list_id else: ptbh.reply(update, msg.LIST_ID_UNKNOWN.format(gbc.ALL_LISTS["full"])) return False
def start(update, context): """Welcome users and notify bot-creator Calls register_new_user if the user is unknown.""" ptbh.reply(update, msg.START) chat_id = update.message.chat.id if not um.group_existent(chat_id): um.register_new_user(update, context.bot)
def list_reminders_summary(update, context): """Is called by /list_reminders or /reminders Gets all reminders and sends a list to users.""" chat_id = update.message.chat.id chat_name = ptbu.get_group_title(update) reminder_message = create_reminder_list_message(context.job_queue, chat_id, chat_name) ptbh.reply(update, reminder_message, quote=False)
def list_reminders(update, context, chat_id=0): """Is called by /reminders_long or /list_reminder_long""" chat_name = chat_id if not chat_id: chat_id = update.message.chat.id chat_name = ptbu.get_group_title(update) reminder_message = create_reminder_list_message(context.job_queue, chat_id, chat_name, False) ptbh.reply(update, reminder_message, quote=False)
def show_users(update, context): user_list = [] with shelve.open(c.SHELVE_MAIN) as db: user_list = list(db.values()) messages = ptbh.split_messages(user_list, 20, "<b>Users</b> - {}-{}\n", str) for m in messages: m = "\n\n".join(m) ptbh.reply(update, m)
def delete_list(update, context): chat_id = update.message.chat.id title = update.message.text[9:].strip() lists = get_lists_from_storage(chat_id) list_id = get_list_by_title(lists, title) if (not list_id): ptbh.reply(update, msg.LIST_ID_UNKNOWN.format(gbc.ALL_LISTS["full"])) return delete_list_from_storage(chat_id, list_id) ptbh.reply(update, msg.LIST_DELETED.format(title))
def add_single_reminder(update, context): """Prepare single reminder for storage and setup params: update: Update, context: Context""" name = context.args[0] # get time to be reminded try: td = extract_time(context.args[1]) message_id = get_reply_to_id(update) print(message_id) setup_reminder(context.bot, update, context.job_queue, update.message.chat.id, message_id, str(name), td) except AttributeError: ptbh.reply(update, msg.R_ERROR_ARG_REMINDER_TIME)
def close_list(update, context): command = update.message.text.strip() lists = get_lists_from_storage(update.message.chat.id) list_id = has_list_id(command, update, lists) if (not list_id): return current_list = lists[str(list_id)] completed_by = ptbu.getUsername(update, True) current_list.set_all_items_completed(completed_by) text = current_list.show_list(is_group(update)) edit_list_message(context, current_list, text) ptbh.reply(update, msg.ALL_SET_COMPLETED.format(completed_by)) add_list_to_storage(update.message.chat.id, current_list)
def show_list(update, context): chat_id = update.message.chat.id title = update.message.text[10:].strip() lists = get_lists_from_storage(chat_id) list_id = get_list_by_title(lists, title) if (not list_id): ptbh.reply(update, msg.LIST_ID_UNKNOWN.format(gbc.ALL_LISTS["full"])) return current_list = lists[str(list_id)] message = update.message.reply_html( current_list.show_list(is_group(update))) current_list.add_message_id(message.message_id) add_list_to_storage(chat_id, current_list) pin_message(update, context, message)
def add_trigger(update, context): """Is called by /add_trigger args Prepares all necessary variables for storage in shelve params: update: Update, context: Context (for args)""" chat_id = update.message.chat.id message = update.message.reply_to_message if not message: ptbh.reply(update, msg.TRIGGER_NO_REPLY) return if not context.args: ptbh.reply(update, msg.TRIGGER_NO_ARGS) return trigger_text = normalize_trigger_text_from_array(context.args) type, content = extract_type_content(message) last_use = datetime.datetime.utcnow() num_of_uses = 0 trigger = Trigger(trigger_text, type, content, last_use, num_of_uses) write_trigger_to_DB(update, trigger, trigger_text, chat_id)
def show_lists(update, context): name = ptbu.get_group_title(update) lists = get_lists_from_storage(update.message.chat.id) lists_list = lists.values() # if the list is emtpy: return a help-message if not lists_list: ptbh.reply(update, msg.LIST_EMPTY) return # initializing header text here, as group_name needs to be filled but two blanks are required header = "<b>Lists for " + name + " - {}-{}</b>\n" # getting an array of messages messages = ptbh.split_messages(list_to_split=lists_list, maximum=c.T_NUM_OF_ITEMS_ON_LISTS_LIST, header=header, callback=write_list_item) for m in messages: m = "\n".join(m) update.message.reply_html(m, quote=False)
def send_trigger_list(update, trigger_list, group_name): """Sends a couple of messages to the user params: update: Update, trigger_list: List of Trigger-Objects, group_name: str """ # if the list is emtpy: return a help-message if not trigger_list: ptbh.reply(update, msg.TRIGGER_EMPTY) return # initializing header text here, as group_name needs to be filled but two blanks are required header = "<b>Triggers for " + group_name + " - {}-{}</b>\n" # getting an array of messages messages = ptbh.split_messages(list_to_split=trigger_list, maximum=c.T_NUM_OF_ITEMS_ON_TRIGGER_LIST, header=header, callback=write_trigger_item) for m in messages: m = "\n".join(m) update.message.reply_html(m, quote=False)
def remove_item_from_list(update, context): # get ids given in the command lists = get_lists_from_storage(update.message.chat.id) command = update.message.text.strip() list_id = has_list_id(command, update, lists) if (not list_id): return current_list = lists[str(list_id)] item_id = int(extract_item_id(command)) try: item = current_list.items[item_id] except: ptbh.reply(update, msg.ITEM_ID_UNKNOWN) return if (item.completed): ptbh.reply(update, msg.ITEM_COMPLETED_ERROR) return # mod_message = modification of the list-message if the bot hasn't admin or delete rights. mod_message = "" if (not delete_message(update)): is_admin, _, can_delete = check_bot_rights(update) if (not is_admin): mod_message = "\n\n" + msg.NOT_ADMIN elif (is_admin and not can_delete): mod_message = "\n\n" + msg.NO_DELETE_RIGHTS else: raise RuntimeError( 'While processing the callback to delete a message this error occured: ' ) # finish completion and edit original message completed_by = ptbu.getUsername(update, True) current_list.set_item_completed(item_id, completed_by) text = current_list.show_list(is_group(update)) + mod_message edit_list_message(context, current_list, text) add_list_to_storage(update.message.chat.id, current_list)
def create_vote(update, context): id = update.message.chat.id args = update.message.text[6:] args = args.split(", ") len_args = len(args) if not len_args: ptbh.reply(update, text=msg.VOTE_NO_ARGS, quote=False) return elif len_args < 3: ptbh.reply(update, text=msg.VOTE_NOT_ENOUGH_ARGS, quote=False) return # preparing question and answers question = args[0] del args[0] answer_options = args keyboard = ptbh.get_inline_keyboard_markup(answer_options, CallbackOptions.VOTE.value, 1) message_text = ptbh.write_vote(question, answer_options) vote = Vote(datetime.datetime.utcnow(), question, answer_options, message_text) write_vote_to_DB(id, vote) message = update.message.reply_html(message_text, reply_markup=keyboard, quote=False) chat_type = update.message.chat.type if chat_type == "supergroup" or chat_type == "group": try: context.bot.pin_chat_message(message.chat.id, message.message_id, disable_notification=True) except BadRequest: ptbh.reply(update, msg.VOTE_NO_ADMIN, quote=False)
def add_item_to_list(update, context): lists = get_lists_from_storage(update.message.chat.id) command = update.message.text.strip() list_id = has_list_id(command, update, lists) if (not list_id): return current_list = lists[str(list_id)] # get new id item_id = current_list.get_next_id() command = command[(15 + len(list_id)):] lines = command.split("\n") # remove all empty strings lines = list(filter(None, lines)) # throw error, if no arguments are passed if (len(lines) == 0): ptbh.reply(update, msg.ADD_ITEM_NO_ARGS) return for i, ita in enumerate(lines): id = item_id + i item = Item(id, list_id, ita) current_list.add_item(item) text = current_list.show_list(is_group(update)) if (len(text) > c.MESSAGE_MAX_LENGTH * 0.8): ptbh.reply(update, msg.ADD_ITEM_LIST_TOO_LONG) return edit_list_message(context, current_list, text) add_list_to_storage(update.message.chat.id, current_list) ##### insert LINK to message if (is_supergroupgroup(update)): linkable_id = str(update.message.chat.id)[4:] if str( update.message.chat.id).startswith( "-100") else update.message.chat.id ptbh.reply( update, msg.ITEMS_ADDED_W_LINK.format(linkable_id, current_list.message_id, current_list.title)) else: ptbh.reply(update, msg.ITEMS_ADDED)
def handle_reminders(update, context): """Is called with /add_reminder Since /add_reminder can take multiple arguments it needs to send the message to an appropriate handler params: update: Update, context: Context""" id = update.message.chat.id # check if this name is taken already try: name = context.args[0] except IndexError: ptbh.reply(update, msg.R_ERROR_ARGS_REMINDER) return if check_reminder_existence(id, name): # notify user ptbh.reply(update, msg.R_ERROR_JOB_EXISTS) return # distribute based on no. of arguments num_of_args = len(context.args) if num_of_args == 2: add_single_reminder(update, context) elif num_of_args == 3: add_reapeting_reminder(update, context) else: ptbh.reply(update, msg.R_ERROR_ARGS_REMINDER) return
def add_list(update, context): # extract message text text = update.message.text[9:].strip() # split by newline to get the items - first item is title lines = text.split("\n") # remove all empty strings lines = list(filter(None, lines)) # throw error, if no arguments are passed if (len(lines) == 0): ptbh.reply(update, msg.LIST_NO_ARGS) return # set title and other data title = lines.pop(0) created_at = datetime.datetime.utcnow().isoformat() created_by = ptbu.getUsername(update, True) chat_id = update.message.chat.id lists = get_lists_from_storage(chat_id) list_id = len(get_lists_from_storage(chat_id)) while (str(list_id) in lists): list_id += 1 # create new list new_list = List(list_id, title, created_by, created_at, chat_id) # check for items on list if (len(lines) > 0): items = {} # add items to list for index, line in enumerate(lines, 1): new_item = Item(index, list_id, line) items[index] = new_item new_list.add_items(items) # check if the list is below the TELEGRAM limit for messages text = new_list.show_list() if (len(text) > c.MESSAGE_MAX_LENGTH * 0.8): ptbh.reply(update, msg.NEW_LIST_TOO_LONG) return # send list to user + retrieve the message_Id message_object = update.message.reply_html(text, quote=False) # finalize new list with last data new_list.add_message_id(message_object.message_id) # store list in storage add_list_to_storage(chat_id, new_list) if (not pin_message(update, context, message_object)): is_admin, can_pin, _ = check_bot_rights(update) if (not is_admin): mod_message = "\n\n" + msg.NOT_ADMIN elif (not can_pin): mod_message = "\n\n" + msg.NO_PIN_RIGHTS if (not is_admin or not can_pin): edit_list_message(context, new_list, new_list.show_list() + mod_message)
def help(update, context): """Send help-dialog to users params: update: Update, context: Context""" keyboard = create_help_inline_keyboard() text = msg.H_START ptbh.reply(update, text, reply_markup=keyboard)
def ping(update, context): """PING - fixed reply to see, whether the bot is still running params - update: Update, context: Context""" ptbh.reply(update, text=msg.S_PING, quote=False)