-
Notifications
You must be signed in to change notification settings - Fork 210
/
bot.py
executable file
·126 lines (101 loc) · 3.66 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
import argparse
import html
import json
import logging
import os
import traceback
from command_handlers import send_spotify_songs
from telegram import Update
from telegram.ext import CallbackContext, CommandHandler, Updater
from telegram.parsemode import ParseMode
logger = logging.getLogger(__name__)
DOWNLOAD_FROM_SPOTIFY_COMMAND_NAME = "spotify"
def setup_logging():
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
def start(update: Update, context: CallbackContext):
if update.effective_chat is None:
return
context.bot.send_message(
chat_id=update.effective_chat.id,
text=f"/{DOWNLOAD_FROM_SPOTIFY_COMMAND_NAME} [url]"
)
def error_handler(update: object, context: CallbackContext) -> None:
"""Log the error and send a telegram message to user"""
# Log the error before we do anything else, so we can see it even if something breaks.
logger.error(
msg="Exception while handling an update:", exc_info=context.error
)
# traceback.format_exception returns the usual python message about an exception, but as a
# list of strings rather than a single string, so we have to join them together.
tb_list = traceback.format_exception(
None, context.error, context.error.__traceback__
)
tb_string = ''.join(tb_list)
# Build the message with some markup and additional information about what happened.
# You might need to add some logic to deal with messages longer than the 4096 character limit.
update_str = update.to_dict() if isinstance(update,
Update) else str(update)
message = (
f'An exception was raised while handling an update\n'
f'<pre>update = {html.escape(json.dumps(update_str, indent=2, ensure_ascii=False))}'
'</pre>\n\n'
f'<pre>context.chat_data = {html.escape(str(context.chat_data))}</pre>\n\n'
f'<pre>context.user_data = {html.escape(str(context.user_data))}</pre>\n\n'
f'<pre>{html.escape(tb_string)}</pre>'
)
# Finally, send the message
context.bot.send_message(
chat_id=update.effective_chat.id,
text=message,
parse_mode=ParseMode.HTML
)
def main():
parser = argparse.ArgumentParser(
description="Start a telegram bot to serve requests"
)
parser.add_argument('--token', dest="telegram_token", default=None)
parser.add_argument('--url', dest="url", default=None)
parser.add_argument('--port', dest="port", type=int, default=None)
args, _ = parser.parse_known_args()
token = args.telegram_token or os.getenv("TOKEN")
url = args.url or os.getenv("URL")
port = args.port or int(os.environ.get('PORT', 8443))
if not token:
logger.error("Token most be set")
return
if not url:
logger.error("Url most be set")
return
if not port:
logger.error("port most be set")
return
updater = Updater(
token,
use_context=True,
)
dp = updater.dispatcher
dp.add_handler(CommandHandler('start', start))
dp.add_error_handler(error_handler)
dp.add_handler(
CommandHandler(
DOWNLOAD_FROM_SPOTIFY_COMMAND_NAME,
send_spotify_songs.send_spotify_songs,
pass_args=True,
pass_job_queue=True,
pass_chat_data=True
)
)
updater.start_webhook(
listen="0.0.0.0",
port=int(port),
url_path=token,
webhook_url=f"{url}/{token}"
)
logger.info(f"Started webhook on {url}/{token}")
updater.idle()
if __name__ == "__main__":
setup_logging()
main()