def get_email(update: Update, context: CallbackContext) -> None: if not is_owner(update): return index = context.args[0] logger.info("received get command.") with EmailClient(email_addr, email_passwd) as client: mail = client.get_mail_by_index(index) content = mail.__repr__() for text in handle_large_text(content): context.bot.send_message(update.message.chat_id, text=text)
def periodic_task(bot, job): global inbox_num logger.info("entering periodic task.") with EmailClient(email_addr, email_passwd) as client: new_inbox_num = client.get_mails_count() if new_inbox_num > inbox_num: mail = client.get_mail_by_index(new_inbox_num) content = mail.__repr__() for text in handle_large_text(content): bot.send_message(job.context, text=text) inbox_num = new_inbox_num
def get_email(bot, update, args): count = 3 if (len(args)): count = int(args[0]) logger.info("received get command.") with EmailClient(email_addr, email_passwd) as client: for i in range(1, count + 1): mail = client.get_mail_by_index(i) content = mail.__repr__() for text in handle_large_text(content): bot.send_message(update.message.chat_id, text=text)
def reply(update: Update, context: CallbackContext) -> None: # if empty return, send messages only if the message is a reply from the original if not update.message.reply_to_message: return # Read the original message num_original_msg = message_dict[update.message.reply_to_message.message_id] with EmailClient(email_addr, email_passwd) as client: mail = client.get_mail_by_index(num_original_msg) client.send_mail(mail.sender, mail.subject, update.message.text)
def setting_email(bot, update, args, job_queue, chat_data): global email_addr, email_passwd, inbox_num chat_id = update.message.chat_id email_addr = args[0] email_passwd = args[1] logger.info("received setting_email command.") update.message.reply_text("Configure email success!") with EmailClient(email_addr, email_passwd) as client: inbox_num = client.get_mails_count() job = job_queue.run_repeating(periodic_task, 120, context=chat_id) chat_data['job'] = job logger.info("periodic task scheduled.")
def inbox(bot, update): logger.info("received inbox command.") with EmailClient(email_addr, email_passwd) as client: global inbox_num new_num = client.get_mails_count() reply_text = "The index of newest mail is *%d*," \ " received *%d* new mails since last" \ " time you checked." % \ (new_num, new_num - inbox_num) inbox_num = new_num bot.send_message(update.message.chat_id, parse_mode=ParseMode.MARKDOWN, text=reply_text)
def replyto(bot, update, args): logger.info("recieved replyto command.") try: with EmailClient(email_addr, email_passwd) as client: to, subject = (args[0], "Reply") text_list = args[1:] text = " ".join(text_list) logger.info("to = {} subject = {} text = {}".format( to, subject, text)) client.send_mail(to, subject, text) bot.send_message(update.message.chat_id, text="Message sent") except: bot.send_message(update.message.chat_id, text="Reply failed")
def periodic_task(context: CallbackContext) -> None: global inbox_num logger.info("entering periodic task.") with EmailClient(email_addr, email_passwd) as client: new_inbox_num = client.get_mails_count() if new_inbox_num > inbox_num: mail = client.get_mail_by_index(new_inbox_num) content = mail.__repr__() for text in handle_large_text(content): message = context.bot.send_message(context.job.context, text=text) message_dict[message.message_id] = new_inbox_num inbox_num = new_inbox_num
def reply(bot, update, args): logger.info("recieved reply command.") try: with EmailClient(email_addr, email_passwd) as client: last_mail = client.get_mail_by_index(1) to, subject = last_mail.get_reply_data() text = " ".join(args) logger.info("to = {} subject = {} text = {}".format( to, subject, text)) client.send_mail(to, subject, text) bot.send_message(update.message.chat_id, text="Message sent") except: bot.send_message(update.message.chat_id, text="Reply failed")
def setting_email(update: Update, context: CallbackContext) -> None: if not is_owner(update): return global email_addr, email_passwd, inbox_num email_addr = context.args[0] email_passwd = context.args[1] logger.info("received setting_email command.") update.message.reply_text("Configure email success!") with EmailClient(email_addr, email_passwd) as client: inbox_num = client.get_mails_count() context.job_queue.run_repeating(periodic_task, interval=60, context=update.message.chat_id) # chat_data['job'] = job logger.info("periodic task scheduled.")
def inbox(update: Update, context: CallbackContext) -> None: if not is_owner(update): return logger.info("received inbox command.") with EmailClient(email_addr, email_passwd) as client: global inbox_num new_num = client.get_mails_count() reply_text = "The index of newest mail is *%d*," \ " received *%d* new mails since last" \ " time you checked." % \ (new_num, new_num - inbox_num) inbox_num = new_num message = context.bot.send_message(update.message.chat_id, parse_mode=ParseMode.MARKDOWN, text=reply_text)