-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
115 lines (103 loc) · 4.34 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
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
from flask import Flask, request, abort
import dateutil.parser as dparser
from dateutil import relativedelta
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
)
import configparser
from datetime import datetime, date, timedelta
import eattelib
app = Flask(__name__)
config = configparser.ConfigParser()
config.read('../eatte.setting')
#環境変数取得
YOUR_CHANNEL_ACCESS_TOKEN = config.get("dev", "YOUR_CHANNEL_ACCESS_TOKEN")
YOUR_CHANNEL_SECRET = config.get("dev", "YOUR_CHANNEL_SECRET")
line_bot_api = LineBotApi(YOUR_CHANNEL_ACCESS_TOKEN)
handler = WebhookHandler(YOUR_CHANNEL_SECRET)
@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'
@app.route("/", methods=['GET'])
def top():
abort(404)
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
msgtext = event.message.text
profile = line_bot_api.get_profile(event.source.user_id)
dn = profile.display_name
print(msgtext)
global date
try:
rawdate = dparser.parse(msgtext,fuzzy=True)
date = rawdate.strftime('%m月%d日')
except dparser._parser.ParserError:
today = datetime.today()
tomorrow = today + timedelta(days=1)
if "明日" in msgtext:
#こんなにここらへんが汚いのはなぜかここでdateだけ定義しても動かなかったため
date = datetime.strftime(tomorrow, '%m月%d日')
print(date)
if "欠席" in msgtext or "休む" in msgtext or "行かない" in msgtext or "行けない" in msgtext and date is not None:
reptext = "" + date + " に欠席?おっけー。"
eattelib.marknotgoing(dn, date)
if "出席" in msgtext or "行く" in msgtext and date is not None:
reptext = "" + date + " に出席?了解。"
eattelib.markgoing(dn, date)
if "遅れて" in msgtext or "遅れる" in msgtext or "遅刻" in msgtext and date is not None:
reptext = "" + date + " に遅れる?了解です。詳しいことはとりあえずこのbotまだ対応できないから部長らへんに言ってね!"
eattelib.marklate(dn, date)
print("todd")
if "今日" in msgtext:
#こんなにここらへんが汚いのはなぜかここでdateだけ定義しても動かなかったため
date = datetime.strftime(today, '%m月%d日')
print(date)
if "欠席" in msgtext or "休む" in msgtext or "行かない" in msgtext or "行けない" in msgtext and date is not None:
reptext = "" + date + " に欠席?おっけー。"
eattelib.marknotgoing(dn, date)
if "出席" in msgtext or "行く" in msgtext and date is not None:
reptext = "" + date + " に出席?了解。"
eattelib.markgoing(dn, date)
if "遅れて" in msgtext or "遅れる" in msgtext or "遅刻" in msgtext and date is not None:
reptext = "" + date + " に遅れる?了解です。詳しいことはとりあえずこのbotまだ対応できないから部長らへんに言ってね!"
eattelib.marklate(dn, date)
else:
return ""
print("reptext")
print(reptext)
print("date")
print(date)
if "欠席" in msgtext or "休む" in msgtext or "行かない" in msgtext or "行けない" in msgtext and date is not None:
reptext = "" + date + " に欠席?おっけー。"
eattelib.marknotgoing(dn, date)
if "出席" in msgtext or "行く" in msgtext and date is not None:
reptext = "" + date + " に出席?了解。"
eattelib.markgoing(dn, date)
if "遅れて" in msgtext or "遅れる" in msgtext or "遅刻" in msgtext and date is not None:
reptext = "" + date + " に遅れる?了解です。詳しいことはとりあえずこのbotまだ対応できないから部長らへんに言ってね!"
eattelib.marklate(dn, date)
#elif
# reptext = "日付は認識しましたが何を言ってるのかを認識できませんでした。。日付と一緒に'遅刻'、'行く'、'休む'等のキーワードを入れてね!"
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=reptext))
if __name__ == "__main__":
# app.run()
port = 80
app.run(host="0.0.0.0", port=port)