5
5
from botbuilder .core import ActivityHandler , MessageFactory , TurnContext , CardFactory
6
6
from botbuilder .schema import ChannelAccount , CardAction , ActionTypes , HeroCard
7
7
8
- FacebookPageIdOption = "Facebook Id"
9
- QuickRepliesOption = "Quick Replies"
10
- PostBackOption = "PostBack"
8
+ FACEBOOK_PAGEID_OPTION = "Facebook Id"
9
+ QUICK_REPLIES_OPTION = "Quick Replies"
10
+ POSTBACK_OPTION = "PostBack"
11
11
12
12
13
13
class FacebookBot (ActivityHandler ):
@@ -19,7 +19,9 @@ async def on_members_added_activity(
19
19
await turn_context .send_activity ("Hello and welcome!" )
20
20
21
21
async def on_message_activity (self , turn_context : TurnContext ):
22
- if not await self ._process_facebook_payload (turn_context , turn_context .activity .channel_data ):
22
+ if not await self ._process_facebook_payload (
23
+ turn_context , turn_context .activity .channel_data
24
+ ):
23
25
await self ._show_choices (turn_context )
24
26
25
27
async def on_event_activity (self , turn_context : TurnContext ):
@@ -28,96 +30,100 @@ async def on_event_activity(self, turn_context: TurnContext):
28
30
async def _show_choices (self , turn_context : TurnContext ):
29
31
choices = [
30
32
Choice (
31
- value = QuickRepliesOption ,
33
+ value = QUICK_REPLIES_OPTION ,
32
34
action = CardAction (
33
- title = QuickRepliesOption ,
35
+ title = QUICK_REPLIES_OPTION ,
34
36
type = ActionTypes .post_back ,
35
- value = QuickRepliesOption
36
- )
37
+ value = QUICK_REPLIES_OPTION ,
38
+ ),
37
39
),
38
40
Choice (
39
- value = FacebookPageIdOption ,
41
+ value = FACEBOOK_PAGEID_OPTION ,
40
42
action = CardAction (
41
- title = FacebookPageIdOption ,
43
+ title = FACEBOOK_PAGEID_OPTION ,
42
44
type = ActionTypes .post_back ,
43
- value = FacebookPageIdOption
44
- )
45
+ value = FACEBOOK_PAGEID_OPTION ,
46
+ ),
45
47
),
46
48
Choice (
47
- value = PostBackOption ,
49
+ value = POSTBACK_OPTION ,
48
50
action = CardAction (
49
- title = PostBackOption ,
51
+ title = POSTBACK_OPTION ,
50
52
type = ActionTypes .post_back ,
51
- value = PostBackOption
52
- )
53
- )
53
+ value = POSTBACK_OPTION ,
54
+ ),
55
+ ),
54
56
]
55
57
56
58
message = ChoiceFactory .for_channel (
57
59
turn_context .activity .channel_id ,
58
60
choices ,
59
- "What Facebook feature would you like to try? Here are some quick replies to choose from!"
61
+ "What Facebook feature would you like to try? Here are some quick replies to choose from!" ,
60
62
)
61
63
await turn_context .send_activity (message )
62
64
63
65
async def _process_facebook_payload (self , turn_context : TurnContext , data ) -> bool :
64
66
if "postback" in data :
65
67
await self ._on_facebook_postback (turn_context , data ["postback" ])
66
68
return True
67
- elif "optin" in data :
69
+
70
+ if "optin" in data :
68
71
await self ._on_facebook_optin (turn_context , data ["optin" ])
69
72
return True
70
- elif "message" in data and "quick_reply" in data ["message" ]:
71
- await self ._on_facebook_quick_reply (turn_context , data ["message" ]["quick_reply" ])
73
+
74
+ if "message" in data and "quick_reply" in data ["message" ]:
75
+ await self ._on_facebook_quick_reply (
76
+ turn_context , data ["message" ]["quick_reply" ]
77
+ )
72
78
return True
73
- elif "message" in data and data ["message" ]["is_echo" ]:
79
+
80
+ if "message" in data and data ["message" ]["is_echo" ]:
74
81
await self ._on_facebook_echo (turn_context , data ["message" ])
75
82
return True
76
83
77
- async def _on_facebook_postback (self , turn_context : TurnContext , facebook_postback : dict ):
84
+ async def _on_facebook_postback (
85
+ self , turn_context : TurnContext , facebook_postback : dict
86
+ ):
78
87
# TODO: Your PostBack handling logic here...
79
88
80
- reply = MessageFactory .text ("Postback" )
89
+ reply = MessageFactory .text (f "Postback: { facebook_postback } " )
81
90
await turn_context .send_activity (reply )
82
91
await self ._show_choices (turn_context )
83
92
84
- async def _on_facebook_quick_reply (self , turn_context : TurnContext , facebook_quick_reply : dict ):
93
+ async def _on_facebook_quick_reply (
94
+ self , turn_context : TurnContext , facebook_quick_reply : dict
95
+ ):
85
96
# TODO: Your quick reply event handling logic here...
86
97
87
- if turn_context .activity .text == FacebookPageIdOption :
98
+ if turn_context .activity .text == FACEBOOK_PAGEID_OPTION :
88
99
reply = MessageFactory .text (
89
100
f"This message comes from the following Facebook Page: { turn_context .activity .recipient .id } "
90
101
)
91
102
await turn_context .send_activity (reply )
92
103
await self ._show_choices (turn_context )
93
- elif turn_context .activity .text == PostBackOption :
104
+ elif turn_context .activity .text == POSTBACK_OPTION :
94
105
card = HeroCard (
95
106
text = "Is 42 the answer to the ultimate question of Life, the Universe, and Everything?" ,
96
107
buttons = [
97
- CardAction (
98
- title = "Yes" ,
99
- type = ActionTypes .post_back ,
100
- value = "Yes"
101
- ),
102
- CardAction (
103
- title = "No" ,
104
- type = ActionTypes .post_back ,
105
- value = "No"
106
- )
107
- ]
108
+ CardAction (title = "Yes" , type = ActionTypes .post_back , value = "Yes" ),
109
+ CardAction (title = "No" , type = ActionTypes .post_back , value = "No" ),
110
+ ],
108
111
)
109
112
reply = MessageFactory .attachment (CardFactory .hero_card (card ))
110
113
await turn_context .send_activity (reply )
111
114
else :
115
+ print (facebook_quick_reply )
112
116
await turn_context .send_activity ("Quick Reply" )
113
117
await self ._show_choices (turn_context )
114
118
115
119
async def _on_facebook_optin (self , turn_context : TurnContext , facebook_optin : dict ):
116
120
# TODO: Your optin event handling logic here...
121
+ print (facebook_optin )
117
122
await turn_context .send_activity ("Opt In" )
118
- pass
119
123
120
- async def _on_facebook_echo (self , turn_context : TurnContext , facebook_message : dict ):
124
+ async def _on_facebook_echo (
125
+ self , turn_context : TurnContext , facebook_message : dict
126
+ ):
121
127
# TODO: Your echo event handling logic here...
128
+ print (facebook_message )
122
129
await turn_context .send_activity ("Echo" )
123
- pass
0 commit comments