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