-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
40 lines (35 loc) · 1.35 KB
/
config.py
File metadata and controls
40 lines (35 loc) · 1.35 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 sys
import os
from linebot.v3 import (
WebhookHandler
)
from linebot.v3.messaging import (
Configuration
)
from api.azure import AzureService
from api.weather import WeatherService
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class Config(metaclass=Singleton):
def __init__(self):
self.CHANNEL_SECRET = os.getenv('CHANNEL_SECRET')
self.CHANNEL_ACCESS_TOKEN = os.getenv('CHANNEL_ACCESS_TOKEN')
self.CWA_API_KEY = os.getenv('CWA_API_KEY')
self.check_env()
self.line_bot_init()
def check_env(self):
if self.CHANNEL_SECRET is None or self.CHANNEL_ACCESS_TOKEN is None:
print("Please set LINE_CHANNEL_SECRET, LINE_CHANNEL_ACCESS_TOKEN environment variables.")
sys.exit(1)
if self.CWA_API_KEY is None:
print("Please set CWA_API_KEY environment variables.")
sys.exit(1)
def line_bot_init(self):
self.handler = WebhookHandler(self.CHANNEL_SECRET)
self.configuration = Configuration(access_token=self.CHANNEL_ACCESS_TOKEN)
self.azureService = AzureService()
self.weatherService = WeatherService(self.CWA_API_KEY)