-
Notifications
You must be signed in to change notification settings - Fork 139
/
bot.py
234 lines (171 loc) · 6.25 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# -*- coding: utf-8 -*-
import logging
import os
import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, Dispatcher, CallbackQueryHandler
from telegram.ext.dispatcher import run_async
import aria
import time
from services import murror, muggnet
from handlers import button
import handlers
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# Resolve BOT API KEY from environment variable
updater = Updater(os.environ.get("BOT_API_KEY"), use_context=True)
# Resolve BOT LOG CHAT from environment variable
BOT_LOG_CHAT = os.environ.get("BOT_LOG_CHAT")
def id(update: telegram.Update, context):
"""Sends a message containing the Chat Id for the current chat
Args:
update:
A telegram incoming update
"""
chatid = update.message.chat.id
update.message.reply_text(chatid)
updater.bot.send_message(chat_id=chatid, text="Here is the id")
def cri(update: telegram.Update, context):
"""Sends a message containing an emoji of a duck crying
Args:
update:
Telegram incoming update
"""
chatid = update.message.chat.id
criemoji = u'\U0001F62D'
f = open('sticker/criduck.tgs', 'rb')
update.message.reply_text(criemoji + criemoji + criemoji)
updater.bot.send_sticker(chat_id=chatid, sticker=f)
def start(update: telegram.Update, context):
"""Sends a hello message with help commands
Args:
update:
Telegram incoming update
"""
update.message.reply_text(
f"Hello " + update.message.chat.first_name + "\nFor help send /help")
def help_func(update: telegram.Update, context):
"""Sends response for the "/help" command
Args:
update:
Telegram incoming update
"""
update.message.reply_text(
f"/mirror :for http(s),ftp and file downloads\n/magnet :for torrent magnet links\n/cancel :cancel all in progress downloads\n/list :get a list of downloads")
def listdownloads(update: telegram.Update, context):
"""Fetches downloads from the aria server
Args:
update:
Telegram incoming update
Returns:
A list of downloads from the aria server
"""
listofdownloads = [dwld for dwld in aria.aria2.get_downloads()]
update.message.reply_text(listofdownloads)
def cancel(update: telegram.Update, context):
"""Cancels all active downloads and removes the files from the server
Args:
update:
Telegram incoming update
"""
downloads = aria.aria2.get_downloads()
try:
aria.aria2.remove(downloads=downloads, files=True,
clean=True, force=True)
except:
print("No downloads to delete")
update.message.reply_text("Cancelling all downloads")
def error(update, context):
"""Log Errors caused by Updates."""
updater.bot.send_message(
chat_id=BOT_LOG_CHAT, text=f'Update {update} caused error {context.error}')
logger.warning('Update "%s" caused error "%s"', update, context.error)
def custom_error(custom_message: str):
"""Sends an error message to the BOT_LOG_CHAT group
Args:
custom_message:
The custom error message to be sent to the bot log chat
"""
updater.bot.send_message(chat_id=BOT_LOG_CHAT, text=custom_message)
@run_async
def mirror(update: telegram.Update, context):
"""Asynchronously starts a download and handles the "/mirror" command
Args:
update:
Telegram incoming update
"""
try:
murror(update=update, updater=updater, context=context)
except:
custom_error("Mirror function returned an error")
@run_async
def magnet(update, context):
"""Asynchronously starts a magnet download and handles "/magnet" command
Args:
update:
Telegram incoming update
"""
try:
muggnet(update=update, updater=updater, context=context)
except:
custom_error("Magnet function returned an error")
def restart(update: telegram.Update, context):
"""Restarts the bot
Args:
update:
Telegram incoming update
"""
try:
handlers.restart(updater, update, context)
except:
custom_error("Nahi maanna? Mat maan!")
def resturt(update: telegram.Update, context):
"""Pulls an update from github and restarts the bot
Args:
update:
Telegram incoming update
"""
try:
handlers.update_and_restart(updater, update, context)
except:
custom_error(
"Well, I guess I didn't get an update after all\nYou can feel good about yourself now")
def echo(update: telegram.Update, context):
"""Replies to a message with the message text. Basically serves as an echo command
Args:
update:
Telegram incoming update
"""
try:
update.message.reply_text(f"{update.message.text}")
except:
print("ERROR")
def main():
updater.bot.send_message(chat_id=BOT_LOG_CHAT, text="Bot Started")
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start))
# handles "/help" command
dp.add_handler(CommandHandler("help", help_func))
# handles "/mirror <url>" command
dp.add_handler(CommandHandler('mirror', mirror))
# handles "/magnet <magnet link>" command
dp.add_handler(CommandHandler('magnet', magnet))
# custom error handler to send error messages to bot log chat instead of console
dp.add_error_handler(error)
# handles "/list" command
dp.add_handler(CommandHandler("list", listdownloads))
dp.add_handler(CommandHandler("id", id)) # handles "/id" command
dp.add_handler(CommandHandler("cri", cri)) # handles "/cri" command
# handles "/cancel" command. Cancels all active downloads.
dp.add_handler(CommandHandler("cancel", cancel))
# handles button "cancel" and "pause" options
dp.add_handler(CallbackQueryHandler(button))
dp.add_handler(CommandHandler("restart", restart))
# git pulls and then restarts the bot
dp.add_handler(CommandHandler("resturt", resturt))
dp.add_handler(CommandHandler("echo", echo))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()