def __import_data__(chat_id, data): failures = [] for notename, notedata in data.get('extra', {}).items(): match = FILE_MATCHER.match(notedata) if match: failures.append(notename) notedata = notedata[match.end():].strip() if notedata: sql.add_note_to_db(chat_id, notename[1:], notedata) else: sql.add_note_to_db(chat_id, notename[1:], notedata) if failures: with BytesIO(str.encode("\n".join(failures))) as output: output.name = "failed_imports.txt" dispatcher.bot.send_document( chat_id, document=output, filename="failed_imports.txt", caption=tld( chat_id, "These files/photos failed to import due to originating " "from another bot. This is a telegram API restriction - each bot sees " "files with a different file_id, to avoid one bot accessing another's " "files. Sorry for the inconvenience!"))
def save_replied(bot: Bot, update: Update): chat_id = update.effective_chat.id text = update.effective_message.text args = text.split( None, 3) # use python's maxsplit to separate Cmd, note_name, and data if len(args) == 3 and args[1] == "from": notename = args[2] elif len(args) >= 2: notename = args[1] else: update.effective_message.reply_text( tld(chat_id, "You need to give me a notename to save this message!")) return msg = update.effective_message.reply_to_message # type: Optional[Message] if msg.from_user.is_bot: text = extract_text(msg) if text: sql.add_note_to_db(chat_id, notename, markdown_parser(text), is_reply=False) update.effective_message.reply_text( tld( chat_id, "Seems like you're trying to save a message from a bot. Unfortunately, " "bots can't forward bot messages, so I can't save the exact message. " "\nI'll save all the text I can, but if you want more, you'll have to " "forward the message yourself, and then save it.")) else: update.effective_message.reply_text( tld( chat_id, "Bots are kinda handicapped by telegram, making it hard for bots to " "interract with other bots, so I can't save this message " "like I usually would - do you mind forwarding it and " "then saving that new message? Thanks!")) return if MESSAGE_DUMP: msg = bot.forward_message(chat_id=MESSAGE_DUMP, from_chat_id=chat_id, message_id=msg.message_id) sql.add_note_to_db(chat_id, notename, msg.message_id, is_reply=True) update.effective_message.reply_text( tld(chat_id, "Yas! Added replied message {}").format(notename))
def save(bot: Bot, update: Update): chat_id = update.effective_chat.id msg = update.effective_message # type: Optional[Message] raw_text = msg.text args = raw_text.split( None, 2) # use python's maxsplit to separate Cmd, note_name, and data if len(args) >= 3: note_name = args[1] note = args[2] offset = len(note) - len( raw_text) # set correct offset relative to command + notename markdown_note, buttons = button_markdown_parser( note, entities=msg.parse_entities(), offset=offset) note_data = markdown_note.strip() if not note_data: msg.reply_text( tld( chat_id, "You can't save an empty message! If you added a button, you MUST " "have some text in the message too.")) return sql.add_note_to_db(chat_id, note_name, note_data, is_reply=False, buttons=buttons) msg.reply_text( tld( chat_id, "Yas! Added {note_name}.\nGet it with /get {note_name}, or #{note_name}" ).format(note_name=note_name)) else: msg.reply_text(tld(chat_id, "Dude, there's no note"))