-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSwitcher.py
74 lines (66 loc) · 2.8 KB
/
Switcher.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
import http.client
import urllib
import json
from PrivateVariables import awsGatewayUrl, botToken
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
connection = http.client.HTTPSConnection('api.telegram.org')
options =['Enable AWS bot', 'Disable AWS bot', 'Get updates', 'Clean updates']
#EDIT THIS TO SELECT WHAT YOU WANT TO EXECUTE
optionsChosen = [options[1],options[2]]
##########################
for option in optionsChosen:
if option == 'Enable AWS bot':
print("Enabling AWS bot")
print('\n')
allowed_updates = json.dumps(["message", "callback_query"])
params = urllib.parse.urlencode({'url': awsGatewayUrl,
'allowed_updates': allowed_updates})
connection.request('POST', '/bot' + botToken + '/setWebhook', params, headers)
response = connection.getresponse()
string = response.read().decode('utf-8')
print(string)
print('\n')
# Test if everything is okay
connection.request('GET', '/bot' + botToken + '/getWebhookInfo', headers=headers)
response = connection.getresponse()
string = response.read().decode('utf-8')
print(string)
print('\n')
elif option == 'Disable AWS bot':
print("Disabling AWS bot")
print('\n')
connection.request('POST', '/bot' + botToken + '/deleteWebhook', headers=headers)
response = connection.getresponse()
string = response.read().decode('utf-8')
print(string)
print('\n')
elif option == 'Get updates':
print("Getting updates")
print('\n')
file = open("results.json", "w")
params = urllib.parse.urlencode({'offset': 0})
connection.request('GET', '/bot' + botToken + '/getUpdates', params, headers)
response = connection.getresponse()
string = response.read().decode('utf-8')
file.write(string)
elif option == 'Clean updates':
print("Cleaning updates")
print('\n')
file = open("results.json", "w")
params = urllib.parse.urlencode({'offset': 0})
connection.request('GET', '/bot' + botToken + '/getUpdates', params, headers)
response = connection.getresponse()
string = response.read().decode('utf-8')
responsejson = json.loads(string)
updates = responsejson["result"]
if len(updates) > 0:
offset = int(updates[len(updates) - 1]["update_id"]) + 1
else:
offset = 0
params = urllib.parse.urlencode({'offset': offset})
connection.request('GET', '/bot' + botToken + '/getUpdates', params, headers)
response = connection.getresponse()
string = response.read().decode('utf-8')
file.write(string)
else:
print("error: option not recognized")