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