55from botbuilder .core import ActivityHandler , MessageFactory , TurnContext , CardFactory
66from botbuilder .schema import ChannelAccount , CardAction , ActionTypes , HeroCard
77
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"
1111
1212
1313class FacebookBot (ActivityHandler ):
@@ -19,7 +19,9 @@ async def on_members_added_activity(
1919 await turn_context .send_activity ("Hello and welcome!" )
2020
2121 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+ ):
2325 await self ._show_choices (turn_context )
2426
2527 async def on_event_activity (self , turn_context : TurnContext ):
@@ -28,96 +30,100 @@ async def on_event_activity(self, turn_context: TurnContext):
2830 async def _show_choices (self , turn_context : TurnContext ):
2931 choices = [
3032 Choice (
31- value = QuickRepliesOption ,
33+ value = QUICK_REPLIES_OPTION ,
3234 action = CardAction (
33- title = QuickRepliesOption ,
35+ title = QUICK_REPLIES_OPTION ,
3436 type = ActionTypes .post_back ,
35- value = QuickRepliesOption
36- )
37+ value = QUICK_REPLIES_OPTION ,
38+ ),
3739 ),
3840 Choice (
39- value = FacebookPageIdOption ,
41+ value = FACEBOOK_PAGEID_OPTION ,
4042 action = CardAction (
41- title = FacebookPageIdOption ,
43+ title = FACEBOOK_PAGEID_OPTION ,
4244 type = ActionTypes .post_back ,
43- value = FacebookPageIdOption
44- )
45+ value = FACEBOOK_PAGEID_OPTION ,
46+ ),
4547 ),
4648 Choice (
47- value = PostBackOption ,
49+ value = POSTBACK_OPTION ,
4850 action = CardAction (
49- title = PostBackOption ,
51+ title = POSTBACK_OPTION ,
5052 type = ActionTypes .post_back ,
51- value = PostBackOption
52- )
53- )
53+ value = POSTBACK_OPTION ,
54+ ),
55+ ),
5456 ]
5557
5658 message = ChoiceFactory .for_channel (
5759 turn_context .activity .channel_id ,
5860 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!" ,
6062 )
6163 await turn_context .send_activity (message )
6264
6365 async def _process_facebook_payload (self , turn_context : TurnContext , data ) -> bool :
6466 if "postback" in data :
6567 await self ._on_facebook_postback (turn_context , data ["postback" ])
6668 return True
67- elif "optin" in data :
69+
70+ if "optin" in data :
6871 await self ._on_facebook_optin (turn_context , data ["optin" ])
6972 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+ )
7278 return True
73- elif "message" in data and data ["message" ]["is_echo" ]:
79+
80+ if "message" in data and data ["message" ]["is_echo" ]:
7481 await self ._on_facebook_echo (turn_context , data ["message" ])
7582 return True
7683
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+ ):
7887 # TODO: Your PostBack handling logic here...
7988
80- reply = MessageFactory .text ("Postback" )
89+ reply = MessageFactory .text (f "Postback: { facebook_postback } " )
8190 await turn_context .send_activity (reply )
8291 await self ._show_choices (turn_context )
8392
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+ ):
8596 # TODO: Your quick reply event handling logic here...
8697
87- if turn_context .activity .text == FacebookPageIdOption :
98+ if turn_context .activity .text == FACEBOOK_PAGEID_OPTION :
8899 reply = MessageFactory .text (
89100 f"This message comes from the following Facebook Page: { turn_context .activity .recipient .id } "
90101 )
91102 await turn_context .send_activity (reply )
92103 await self ._show_choices (turn_context )
93- elif turn_context .activity .text == PostBackOption :
104+ elif turn_context .activity .text == POSTBACK_OPTION :
94105 card = HeroCard (
95106 text = "Is 42 the answer to the ultimate question of Life, the Universe, and Everything?" ,
96107 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+ ],
108111 )
109112 reply = MessageFactory .attachment (CardFactory .hero_card (card ))
110113 await turn_context .send_activity (reply )
111114 else :
115+ print (facebook_quick_reply )
112116 await turn_context .send_activity ("Quick Reply" )
113117 await self ._show_choices (turn_context )
114118
115119 async def _on_facebook_optin (self , turn_context : TurnContext , facebook_optin : dict ):
116120 # TODO: Your optin event handling logic here...
121+ print (facebook_optin )
117122 await turn_context .send_activity ("Opt In" )
118- pass
119123
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+ ):
121127 # TODO: Your echo event handling logic here...
128+ print (facebook_message )
122129 await turn_context .send_activity ("Echo" )
123- pass
0 commit comments