-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
138 lines (102 loc) · 3.24 KB
/
app.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
from flask import Flask, request, abort
import requests
import json
import logging
#初始化紀錄器
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
#載入config
from config import Config
#豆芽工具包
import beanstool.weather
import beanstool.fule
import beanstool.prosperity.prosperity_light as bean_prosperity
import beanstool.bitcoin
import beanstool.csmunews
#import beanstool.gold_price
import beanstool.poem
#豆芽開發包
import beansdev.Tester
# LINE bot 必要套件
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage, ImageMessage, ImageSendMessage
)
#--------------------------------------------------------------------------
app = Flask(__name__)
#載入組態檔
conf = Config()
conf = conf.load()
# Channel Access Token
line_bot_api = LineBotApi(conf["LineBotApi"])
# Channel Secret
handler = WebhookHandler(conf["Webhook"])
#------------------------- Functions --------------------------
@app.route("/callback", methods=['POST'])
def callback():
# get X-Line-Signature header value
signature = request.headers['X-Line-Signature']
# get request body as text
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
logging.info(event.message.text)
cmd = event.message.text.split(" ")
ReplyMsg = ""
if (cmd[0] == "抽"):
poem = beanstool.poem.Poem()
ReplyMsg = poem.getMsg()
if (cmd[0] == "天氣"):
station = cmd[1]
weather = beanstool.weather.Weather(station)
WeatherMsg = weather.getMsg(station)
if not WeatherMsg:
WeatherMsg = "沒這個氣象站啦"
ReplyMsg = WeatherMsg
if (cmd[0] == "雨量"):
station = cmd[1]
rain = beanstool.weather.Rainfall(station)
ReplyMsg = rain.getMsg(station)
if cmd[0] == "豆芽":
if cmd[1] == "消息":
csmunews = beanstool.csmunews()
ReplyMsg = csmunews.getMsg()
if(ReplyMsg == False):
ReplyMsg = "目前無訊息"
else:
return
if cmd[0] == "油價":
fuel = beanstool.fule()
ReplyMsg = fuel.getMsg()
if cmd[0].lower() == "bitcoin":
bitcoin = beanstool.bitcoin(cmd[1].upper())
ReplyMsg = bitcoin.getMsg()
#if cmd[0] == "黃金":
# gold = beanstool.gold_price()
# price = gold.getMsg()
if cmd[0] == "景氣指標":
PL = bean_prosperity.Prosperity()
ReplyMsg = PL.getMsg()
if cmd[0] == "dev-test-cmd-001":
tester = beansdev.Tester.URLTester()
ReplyMsg = tester.runTest()
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=ReplyMsg))
if __name__ == "__main__":
port = int(os.environ.get('PORT', conf["port"]))
app.run(host='0.0.0.0', port=port)