/
template_json.py
113 lines (107 loc) · 3.66 KB
/
template_json.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
#coding:utf-8
class Template_json :
def __init__(self, sender_id, template_type, text=None, payload_yes=None, payload_no=None):
self.text= text
self.payload_yes = payload_yes
self.payload_no = payload_no
if template_type == 1 :
self.template = {
"recipient": {
"id": sender_id
},
"message":{
"attachment":{
"type":"template",
"payload":{
"template_type":"generic",
"elements":[
]
}
}
}
}
if template_type == 2:
self.template ={
"recipient":
{
"id": sender_id
},
"message":
{
"text": self.text,
"quick_replies": [
{
"content_type": "text",
"title": "是",
"payload": self.payload_yes
},
{
"content_type": "text",
"title": "否",
"payload": self.payload_no
}
]
}
}
if template_type == 3:
self.template ={
"recipient":
{
"id": sender_id
},
"message":
{
"text": self.text,
"quick_replies": [
{
"content_type": "text",
"title": "好喔",
"payload": self.payload_yes
},
{
"content_type": "text",
"title": "我剛剛按錯了",
"payload": self.payload_no
}
]
}
}
if template_type == 4:
self.template ={
"recipient":
{
"id": sender_id
},
"message":
{
"text": "本服務需要您的位置以計算推薦資訊~",
"quick_replies": [
{
"content_type":"location"
}
]
}
}
def addItem(self, title, image_url, feedback, address):
bobble={
"title":title,
"image_url":image_url,
"subtitle":address,
"buttons":[
{
"type":"web_url",
"url":"https://www.google.com.tw/maps/place/" + title ,
"title":"View Website"
},{
"type":"postback",
"title":"Love It!",
"payload": 'Y' + feedback
},{
"type":"postback",
"title":"Not Good...",
"payload": 'N' + feedback
}
]
}
self.template["message"]["attachment"]["payload"]["elements"].append(bobble)
return self.template