-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
120 lines (105 loc) · 3.71 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
import json
from http.server import BaseHTTPRequestHandler, HTTPServer
from telegram import telegram_api
from commands import run_command
from botconfig import *
class RequestHandler(BaseHTTPRequestHandler):
def do_POST(self):
# Get the post data
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length) # read post body
post_data = bytes.decode(post_data) # convert to text
post_data = json.loads(post_data) # convert to dictionary
# Default message to send back
message = {'ok': True}
# Process the response
if self.path == config_path_telegram:
if ('edited_message' in post_data):
post_data['message'] = post_data['edited_message']
if ('message' in post_data) and ('text' in post_data['message']):
message = post_data['message']
chat_id = message['chat']['id']
text = message['text']
# extract sender information
username = '?'
first_name = '?'
last_name = '?'
full_name = '?'
if 'from' in message:
message_from = message['from']
if 'first_name' in message_from:
first_name = message_from['first_name']
full_name = first_name
if 'last_name' in message_from:
last_name = message_from['last_name']
full_name += ' '+last_name
if 'username' in message_from:
username = message_from['username']
else:
username = first_name
# extract information from the text
command = text[1:]
arg = ''
find_space = text.find(' ')
find_newline = text.find('\n')
if find_space >= 0 or find_newline >= 0:
split_index = min(find_space, find_newline)
if find_space == -1:
split_index = find_newline
if find_newline == -1:
split_index = find_space
command = text[1:split_index]
arg = text[split_index+1:]
# trim /command@bot down to /command
find_at = command.find('@')
if find_at >= 0:
command = command[0:find_at]
# run the command
params = {'arg': arg, 'username': username, 'first_name': first_name, 'last_name': last_name, 'full_name': full_name}
result = run_command(command, params)
if result != None:
message = {'method': 'sendMessage', 'disable_web_page_preview': True, 'chat_id': chat_id, 'text': result['text']}
elif self.path == config_path_generic:
if ('command' in post_data) and ('arg' in post_data):
first_name = '?'
last_name = '?'
username = '?'
full_name = '?'
if 'first_name' in post_data:
first_name = post_data['first_name']
full_name = first_name
if 'last_name' in post_data:
last_name = post_data['last_name']
full_name += ' '+last_name
if 'username' in post_data:
username = post_data['username']
# run the command
params = {'arg': post_data['arg'], 'username': username, 'first_name': first_name, 'last_name': last_name, 'full_name': full_name}
result = run_command(post_data['command'], params)
if result != None:
message = {'text': result['text']}
else:
print("Unrecognized path: "+self.path)
# Set response code and headers
self.send_response(200)
self.send_header('Content-type','application/json')
self.end_headers()
# Send the response
self.wfile.write(bytes(json.dumps(message), "utf8"))
return
def do_GET(self):
# Set response code and headers
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
message = "Hello World!"
# Send the response
self.wfile.write(bytes(message, "utf8"))
return
def run():
print('starting server...')
httpd = HTTPServer(('', config_port), RequestHandler)
print('running server...')
httpd.serve_forever()
telegram_api('setWebHook', {'url': config_base_url+config_path_telegram', 'max_connections': 10})
run()