|
| 1 | +# Facebook Events |
| 2 | + |
| 3 | +Bot Framework v4 facebook events bot sample |
| 4 | + |
| 5 | +This bot has been created using [Bot Framework](https://dev.botframework.com), is shows how to integrate and consume Facebook specific payloads, such as postbacks, quick replies and opt-in events. Since Bot Framework supports multiple Facebook pages for a single bot, we also show how to know the page to which the message was sent, so developers can have custom behavior per page. |
| 6 | + |
| 7 | +More information about configuring a bot for Facebook Messenger can be found here: [Connect a bot to Facebook](https://docs.microsoft.com/en-us/azure/bot-service/bot-service-channel-connect-facebook) |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +- Java 1.8+ |
| 12 | +- Install [Maven](https://maven.apache.org/) |
| 13 | +- An account on [Azure](https://azure.microsoft.com) if you want to deploy to Azure. |
| 14 | + |
| 15 | +## To try this sample locally |
| 16 | +- From the root of this project folder: |
| 17 | + - Build the sample using `mvn package` |
| 18 | + - Run it by using `java -jar .\target\facebook-events-sample.jar` |
| 19 | + |
| 20 | +- Test the bot using Bot Framework Emulator |
| 21 | + |
| 22 | + [Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. |
| 23 | + |
| 24 | + - Install the Bot Framework Emulator version 4.3.0 or greater from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) |
| 25 | + |
| 26 | + - Connect to the bot using Bot Framework Emulator |
| 27 | + |
| 28 | + - Launch Bot Framework Emulator |
| 29 | + - File -> Open Bot |
| 30 | + - Enter a Bot URL of `http://localhost:3978/api/messages` |
| 31 | + |
| 32 | +## Deploy the bot to Azure |
| 33 | + |
| 34 | +As described on [Deploy your bot](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-deploy-az-cli), you will perform the first 4 steps to setup the Azure app, then deploy the code using the azure-webapp Maven plugin. |
| 35 | + |
| 36 | +### 1. Login to Azure |
| 37 | +From a command (or PowerShell) prompt in the root of the bot folder, execute: |
| 38 | +`az login` |
| 39 | + |
| 40 | +### 2. Set the subscription |
| 41 | +`az account set --subscription "<azure-subscription>"` |
| 42 | + |
| 43 | +If you aren't sure which subscription to use for deploying the bot, you can view the list of subscriptions for your account by using `az account list` command. |
| 44 | + |
| 45 | +### 3. Create an App registration |
| 46 | +`az ad app create --display-name "<botname>" --password "<appsecret>" --available-to-other-tenants` |
| 47 | + |
| 48 | +Replace `<botname>` and `<appsecret>` with your own values. |
| 49 | + |
| 50 | +`<botname>` is the unique name of your bot. |
| 51 | +`<appsecret>` is a minimum 16 character password for your bot. |
| 52 | + |
| 53 | +Record the `appid` from the returned JSON |
| 54 | + |
| 55 | +### 4. Create the Azure resources |
| 56 | +Replace the values for `<appid>`, `<appsecret>`, `<botname>`, and `<groupname>` in the following commands: |
| 57 | + |
| 58 | +#### To a new Resource Group |
| 59 | +`az deployment sub create --name "facebookBotDeploy" --location "westus" --template-file ".\deploymentTemplates\template-with-new-rg.json" --parameters appId="<appid>" appSecret="<appsecret>" botId="<botname>" botSku=S1 newAppServicePlanName="facebookBotPlan" newWebAppName="facebookBot" groupLocation="westus" newAppServicePlanLocation="westus"` |
| 60 | + |
| 61 | +#### To an existing Resource Group |
| 62 | +`az deployment group create --resource-group "<groupname>" --template-file ".\deploymentTemplates\template-with-preexisting-rg.json" --parameters appId="<appid>" appSecret="<appsecret>" botId="<botname>" newWebAppName="facebookBot" newAppServicePlanName="facebookBotPlan" appServicePlanLocation="westus" --name "facebookBot"` |
| 63 | + |
| 64 | +### 5. Update app id and password |
| 65 | +In src/main/resources/application.properties update |
| 66 | + - `MicrosoftAppPassword` with the botsecret value |
| 67 | + - `MicrosoftAppId` with the appid from the first step |
| 68 | + |
| 69 | +### 6. Deploy the code |
| 70 | +- Execute `mvn clean package` |
| 71 | +- Execute `mvn azure-webapp:deploy -Dgroupname="<groupname>" -Dbotname="<botname>"` |
| 72 | + |
| 73 | +If the deployment is successful, you will be able to test it via "Test in Web Chat" from the Azure Portal using the "Bot Channel Registration" for the bot. |
| 74 | + |
| 75 | +After the bot is deployed, you only need to execute #6 if you make changes to the bot. |
| 76 | + |
| 77 | + |
| 78 | +## Further reading |
| 79 | + |
| 80 | +- [Bot Framework Documentation](https://docs.botframework.com) |
| 81 | +- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) |
| 82 | +- [Facebook Quick Replies](https://developers.facebook.com/docs/messenger-platform/send-messages/quick-replies/0) |
| 83 | +- [Facebook PostBack](https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/messaging_postbacks/) |
| 84 | +- [Facebook Opt-in](https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/messaging_optins/) |
| 85 | +- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) |
| 86 | +- [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0) |
| 87 | +- [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0) |
| 88 | +- [.NET Core CLI tools](https://docs.microsoft.com/en-us/dotnet/core/tools/?tabs=netcore2x) |
| 89 | +- [Azure CLI](https://docs.microsoft.com/cli/azure/?view=azure-cli-latest) |
| 90 | +- [Azure Portal](https://portal.azure.com) |
| 91 | +- [Channels and Bot Connector Service](https://docs.microsoft.com/en-us/azure/bot-service/bot-concepts?view=azure-bot-service-4.0) |
0 commit comments