-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenable_hooks.py
More file actions
53 lines (41 loc) · 1.31 KB
/
enable_hooks.py
File metadata and controls
53 lines (41 loc) · 1.31 KB
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
import argparse
import json
import sys
import urllib.request
API_ENDPOINT_URL_TEMPLATE = 'https://api.telegram.org/bot{tg_token}/setWebhook'
FUNCTION_URL_TEMPLATE = 'https://functions.yandexcloud.net/{function_id}'
def parse_args(raw_args):
parser = argparse.ArgumentParser()
parser.add_argument(
'--telegram-token',
help='Token for target Telegram bot to set webhook',
required=True,
type=str,
)
parser.add_argument(
'--function-id',
help='ID of YC Function to set as receiver of webhook',
required=True,
type=str,
)
args = parser.parse_args(raw_args)
return args
def set_webhook(tg_token, function_id):
data = {
'url': FUNCTION_URL_TEMPLATE.format(function_id=function_id),
}
raw_data = json.dumps(data).encode('utf8')
request = urllib.request.Request(
API_ENDPOINT_URL_TEMPLATE.format(tg_token=tg_token),
method='POST',
data=raw_data,
headers={'content-type': 'application/json'}
)
response = urllib.request.urlopen(request)
return response.read().decode('utf8')
def main(raw_args):
args = parse_args(raw_args)
tg_api_response = set_webhook(args.telegram_token, args.function_id)
print(tg_api_response)
if __name__ == '__main__':
main(sys.argv[1:])