forked from hashiba-k-jp/hakodate-a05
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.py
40 lines (34 loc) · 1.11 KB
/
funcs.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
# funcs stands for FUNCtionS
import os, sys
import psycopg2
from linebot.models import TextSendMessage
from linebot import(
LineBotApi
)
import linebot
#各種定数を環境変数から取得
ACCESS_TOKEN = os.environ.get('ACCESS_TOKEN')
DATABASE_URL = os.environ.get('DATABASE_URL')
#LINEユーザにメッセージを送信する関数
def send_msg_with_line(user_id,msgs):
send_msg = TextSendMessage(text='')
try:
line_bot_api = LineBotApi(ACCESS_TOKEN)
for msg in msgs:
send_msg = TextSendMessage(text=msg)
line_bot_api.push_message(user_id,send_msg)
except linebot.exceptions.LineBotApiError as e:
print(e.error.message)
print(e.error.details)
#DB接続用の関数
def db_connect():
print('Connecting:DataBase')
#環境変数からデータベースの情報を取得
DATABASE_URL = os.environ.get('DATABASE_URL')
conn = ''
try:
conn = psycopg2.connect(DATABASE_URL)
except psycopg2.Error:
print('Database connection failed!!') #DBとの接続に失敗した場合は終了する
sys.exit()
return conn