-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
68 lines (53 loc) · 1.79 KB
/
main.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
import telegram
import os
from app.jobs.polls import Poll
from app.helpers import setup_logging, get_config
DEFAULT_CONFIG_NAME = "settings.ini"
bot = channel_id = config = None
def should_start_webhook(operation_mode):
""" Check if operation mode is webhook, and returns True or False """
if operation_mode == "webhook":
return True
return False
def should_start_jobs(operation_mode):
""" Check if operation mode is jobs, and returns True or False """
if operation_mode == "jobs":
return True
return False
def get_job_by_name(name):
""" Given a job name, instance the corresponding class and return it """
if name == "POLL":
job = Poll.setup(bot, channel_id, config)
elif name == "METRICS":
pass
else:
raise ValueError("Invalid job name. You must provide a valid job name inside \"JOB_NAME\" environment variable")
return job
def start_jobs():
""" Get job name from environment, and start the job """
job_name = os.environ.get("JOB_NAME")
job = get_job_by_name(job_name)
job.start_job()
def main():
# Security sensitive parameters got from environment..
telegram_token = os.environ.get("TELEGRAM_TOKEN")
# Other common parameters, get from config file
global config
config = get_config(DEFAULT_CONFIG_NAME)
global channel_id
channel_id = config["telegram"]["channel_id"]
operation_mode = config["app"]["operation_mode"]
# Setup common things
setup_logging()
global bot
bot = telegram.Bot(token=telegram_token)
# Jobs startup logic
if should_start_jobs(operation_mode):
start_jobs()
# Webhook startup logic
if should_start_webhook(operation_mode):
pass
def lambda_handler(event, context):
main()
if __name__ == "__main__":
main()