-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
133 lines (92 loc) · 3.82 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
import base64
import logging
from pathlib import Path
import telepot
from flask import Flask, request
from telepot.loop import OrderedWebhook
import utils
from downloader import Mp3Downloader
config_path = Path('config.json')
c_channel_id = "channel_id"
c_telegram_api_key = "telegram_api_key"
c_webhook_url = "webhook_url"
c_webhook_port = "webhook_port"
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
config = utils.load_json(config_path, {})
def on_chat_message(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
if content_type == 'text':
text = msg['text']
if text.startswith('/get'):
try:
link = text[len('/get_'):]
if not link or len(link) > 150:
bot.sendMessage(chat_id, '🤨️')
return
link = link + '=' * (len(link) % 4)
link = base64.b64decode(link.encode('utf-8')).decode('utf-8')
print(f'get {link}')
mdl = Mp3Downloader()
dl_path = mdl.download_radio_javan(link)
print(f'sending {dl_path} to telegram ...')
bot.sendAudio(chat_id, open(dl_path, 'rb'))
except ValueError:
print('No payload, or more than one chunk of payload')
except KeyError:
print('Invalid key, no corresponding User ID')
else:
try:
query = text
if not query or len(query) > 50:
bot.sendMessage(chat_id, 'bad query 😕️')
return
print(f'search {query}')
mdl = Mp3Downloader()
results = mdl.search_rj(query)
if not results:
bot.sendMessage(chat_id, 'nothing found ☹️')
return
msg = ''
for name, link in results[:min(len(results), 10)]:
link = base64.b64encode(link.encode('utf-8')).decode('utf-8').replace('=', '')
msg += f'{name}:\n /get_{link}\n\n'
bot.sendMessage(chat_id, msg)
except ValueError as e:
print('No payload, or more than one chunk of payload')
def on_callback_query(msg):
query_id, from_id, data = telepot.glance(msg, flavor='callback_query')
print('Callback query:', query_id, from_id, data)
# need `/setinline`
def on_inline_query(msg):
query_id, from_id, query_string = telepot.glance(msg, flavor='inline_query')
print('Inline Query:', query_id, from_id, query_string)
# Compose your own answers
articles = [{'type': 'article',
'id': 'abc', 'title': 'ABC', 'message_text': 'Good morning'}]
bot.answerInlineQuery(query_id, articles)
# need `/setinlinefeedback`
def on_chosen_inline_result(msg):
result_id, from_id, query_string = telepot.glance(msg, flavor='chosen_inline_result')
print('Chosen Inline Result:', result_id, from_id, query_string)
TOKEN = config[c_telegram_api_key]
PORT = config[c_webhook_port]
URL = f'{config[c_webhook_url]}/{TOKEN}'
app = Flask(__name__)
bot = telepot.Bot(TOKEN)
webhook = OrderedWebhook(bot, {'chat': on_chat_message,
'callback_query': on_callback_query,
'inline_query': on_inline_query,
'chosen_inline_result': on_chosen_inline_result})
@app.route(f'/webhook/{TOKEN}', methods=['GET', 'POST'])
def pass_update():
webhook.feed(request.data)
return 'OK'
try:
bot.setWebhook(URL)
# Sometimes it would raise this error, but webhook still set successfully.
except telepot.exception.TooManyRequestsError:
pass
webhook.run_as_thread()