-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
40 lines (35 loc) · 1.49 KB
/
main.py
File metadata and controls
40 lines (35 loc) · 1.49 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
import functions_framework
import google.cloud.secretmanager
from wwntgbotlib.keyboard_button_names import KeyboardButtonsNames as kbn
import bot_controller
# Test for gcp trigger
def run_bot(token: str, request_json):
data = request_json
if 'message' in data:
message = data['message']
bot_controller.BotController(token).handle_message(message)
return True
return False
###################################################################################
def get_secret(secret_id, project_id="worldwidenewstelegrambot", version_id="latest"):
# Створіть клієнт менеджера секретів.
client = google.cloud.secretmanager.SecretManagerServiceClient()
name = f"projects/{project_id}/secrets/{secret_id}/versions/{version_id}"
# Отримайте значення секрету.
response = client.access_secret_version(request={"name": name})
payload = response.payload.data.decode("UTF-8")
return payload
# Entry point
@functions_framework.http
def handle_request(request):
print("Start bot------------------------------")
print("Traversing button names:")
for bn in kbn:
print("Button name:", bn.value)
print("Stop bot------------------------------")
secret_id = "bot_token"
secret = get_secret(secret_id)
request_json = request.get_json(silent=True)
print(f"Request:{request_json}")
run_bot(secret, request_json)
return f'Request: {request}\nRequest proccessing is finished!'