forked from hyfc/telegram-mail-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
203 lines (167 loc) · 6.58 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import logging
import os
import sys
from telegram import Update, Bot
from telegram.constants import MessageLimit, ParseMode
from telegram.ext import ApplicationBuilder, CommandHandler, CallbackContext
from utils.client import EmailClient
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s:%(lineno)d - %(message)s",
stream=sys.stdout,
level=logging.INFO,
)
logger = logging.getLogger(__name__)
bot_token = os.environ["TELEGRAM_TOKEN"]
owner_chat_id = int(os.environ["OWNER_CHAT_ID"])
def is_owner(update: Update) -> bool:
return update.message.chat_id == owner_chat_id
def handle_large_text(text):
while text:
if len(text) < MessageLimit.MAX_TEXT_LENGTH:
yield text
text = None
else:
out = text[: MessageLimit.MAX_TEXT_LENGTH]
yield out
text = text.lstrip(out)
def error(update: Update, context: CallbackContext) -> None:
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
async def start_callback(update: Update, context: CallbackContext) -> None:
if not is_owner(update):
return
msg = "Use /help to get help"
# print(update)
await update.message.reply_text(msg)
async def _help(update: Update, context: CallbackContext) -> None:
if not is_owner(update):
return
"""Send a message when the command /help is issued."""
help_str = """*Mailbox Settings*:
/setting [email protected] password Login (needed)
/inbox Get mails status in INBOX
/get $index Get the $index mail
/help get help
/list List all mails in INBOX"""
await context.bot.send_message(
update.message.chat_id,
parse_mode=ParseMode.MARKDOWN,
text=help_str,
)
async 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.")
await 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, chat_id=update.message.chat_id
)
# chat_data['job'] = job
logger.info("periodic task scheduled.")
async 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:
for i in range(inbox_num + 1, new_inbox_num + 1):
mail = client.get_mail_by_index(i)
content = mail.__repr__()
for text in handle_large_text(content):
await context.bot.send_message(context.job.chat_id, text=text)
inbox_num = new_inbox_num
async def inbox(update: Update, context: CallbackContext) -> None:
if not is_owner(update):
return
logger.info("received inbox command.")
try:
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
await context.bot.send_message(
update.message.chat_id,
parse_mode=ParseMode.MARKDOWN,
text=reply_text,
)
except NameError as e:
await context.bot.send_message(
update.message.chat_id,
parse_mode=ParseMode.MARKDOWN,
text="Email unset, please set valid email by the '/setting' command.",
)
logger.warning(e)
async def get_email(update: Update, context: CallbackContext) -> None:
if not is_owner(update):
return
index = context.args[0]
if not index:
await context.bot.send_message(
update.message.chat_id, text="$index should be a positive number!"
)
logger.info("received get command.")
try:
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):
await context.bot.send_message(update.message.chat_id, text=text)
except NameError as e:
await context.bot.send_message(
update.message.chat_id,
parse_mode=ParseMode.MARKDOWN,
text="Email unset, please set valid email by the '/setting' command.",
)
logger.warning(e)
async def list_email(update: Update, context: CallbackContext) -> None:
if not is_owner(update):
return
try:
with EmailClient(email_addr, email_passwd) as client:
mails = client.get_listed_mails()
text = ""
for i in mails:
text += f"*{mails.index(i)+1}*: Subject: {i[0]};\nSender: {i[1]};\nDate: {i[2]}\n\n"
await context.bot.send_message(
update.message.chat_id,
parse_mode=ParseMode.MARKDOWN,
text=text,
)
except NameError as e:
await context.bot.send_message(
update.message.chat_id,
parse_mode=ParseMode.MARKDOWN,
text="Email unset, please set valid email by the '/setting' command.",
)
logger.warning(e)
def main():
# Create the EventHandler and pass it your bot's token.
bot = Bot(token=bot_token)
print(bot_token)
application = ApplicationBuilder().bot(bot).build()
# simple start function
application.add_handler(CommandHandler("start", start_callback))
application.add_handler(CommandHandler("help", _help))
#
# Add command handler to set email address and account.
application.add_handler(CommandHandler("setting", setting_email))
application.add_handler(CommandHandler("inbox", inbox))
application.add_handler(CommandHandler("get", get_email))
application.add_handler(CommandHandler("list", list_email))
application.add_error_handler(error)
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
application.run_polling()
if __name__ == "__main__":
main()