forked from varietyshopletgo/linebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timetree.py
62 lines (52 loc) · 1.81 KB
/
timetree.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
import requests
import os
import json
from datetime import datetime, timezone, timedelta
import dateutil.parser
from linebot.models import (
TemplateSendMessage, ButtonsTemplate, URIAction, TextSendMessage
)
def get_timetree():
# トークンの設定
ACCESS_TOKEN = os.environ["TT_ACCESS_TOKEN"]
# ヘッダーの設定
headers = {
'Accept': 'application/vnd.timetree.v1+json',
'Authorization': 'Bearer ' + ACCESS_TOKEN
}
# エンドポイントの設定
URL = 'https://timetreeapis.com/calendars/EGSvfRNr2Kw9/upcoming_events?timezone=Asia/Tokyo&days=7'
# リクエスト
r = requests.get(URL, headers=headers)
data = r.json()
tt_msg =""
for event in data['data']:
tt_event = event['attributes']['title']
tt_time = event['attributes']['start_at']
# UTC→JSTに時間を変換
tt_time.replace('Z', '+00:00')
t = dateutil.parser.parse(tt_time)
JST = timezone(timedelta(hours=+9))
t_jst = t.astimezone(JST)
# 文章のパーツに必要な状態に整形
tt_month = t_jst.strftime("%-m")
tt_day = t_jst.strftime("%-d")
ret = tt_month + '/' + tt_day
tt_msg += ret + ' ' + tt_event + '\n'
tt_longmsg = tt_msg + 'があるらしい'
text_send_message = TextSendMessage(text=tt_longmsg)
return text_send_message
def get_msg():
buttons_template_message = TemplateSendMessage(
alt_text='予定表見に行く?',
template=ButtonsTemplate(
text="予定表見に行く?",
actions=[
URIAction(
label='掲示スペースに立ち寄る',
uri='https://timetr.ee/s/RrHpwO_-XUfL1DUmyFmWOCt-EvRdKx9K'
)
]
)
)
return buttons_template_message