|
| 1 | +--- |
| 2 | +page_type: sample |
| 3 | +description: This sample app demonstrates how to use message reactions in Microsoft Teams with a bot built on the Bot Framework. |
| 4 | +products: |
| 5 | +- office-teams |
| 6 | +- office |
| 7 | +- office-365 |
| 8 | +languages: |
| 9 | +- Python |
| 10 | +extensions: |
| 11 | + contentType: samples |
| 12 | + createdDate: "2024-12-12" |
| 13 | +urlFragment: officedev-microsoft-teams-samples-bot-message-reaction-Python |
| 14 | +--- |
| 15 | + |
| 16 | +# Teams Message Reactions Bot Python Sample |
| 17 | + |
| 18 | +This sample app demonstrates the implementation of message reactions in Microsoft Teams using the Bot Framework. The bot responds dynamically to reactions, supporting personal, group, and team scopes. |
| 19 | + |
| 20 | +## Included Features |
| 21 | +* Bots |
| 22 | +* Message Reactions |
| 23 | + |
| 24 | +## Interaction with bot |
| 25 | +  |
| 26 | + |
| 27 | + ## Try it yourself - experience the App in your Microsoft Teams client |
| 28 | +Please find below demo manifest which is deployed on Microsoft Azure and you can try it yourself by uploading the app package (.zip file link below) to your Teams and/or as a personal app. (Sideloading must be enabled for your tenant, [see steps here](https://docs.microsoft.com/microsoftteams/platform/concepts/build-and-test/prepare-your-o365-tenant#enable-custom-teams-apps-and-turn-on-custom-app-uploading)). |
| 29 | + |
| 30 | +**Teams Message Reactions Bot:** [Manifest](/samples/bot-message-reaction/csharp/demo-manifest/bot-message-reaction.zip) |
| 31 | + |
| 32 | +## Prerequisites |
| 33 | + |
| 34 | +- Microsoft Teams is installed and you have an account |
| 35 | +- [Python SDK](https://www.python.org/downloads/) min version 3.8 |
| 36 | +- [dev tunnel](https://learn.microsoft.com/en-us/azure/developer/dev-tunnels/get-started?tabs=windows) or [ngrok](https://ngrok.com/) latest version or equivalent tunnelling solution |
| 37 | +- [Teams Toolkit for VS Code](https://marketplace.visualstudio.com/items?itemName=TeamsDevApp.ms-teams-vscode-extension) or [TeamsFx CLI](https://learn.microsoft.com/microsoftteams/platform/toolkit/teamsfx-cli?pivots=version-one) |
| 38 | + |
| 39 | +## Run the app (Using Teams Toolkit for Visual Studio Code) |
| 40 | + |
| 41 | +The simplest way to run this sample in Teams is to use Teams Toolkit for Visual Studio Code. |
| 42 | + |
| 43 | +1. Ensure you have downloaded and installed [Visual Studio Code](https://code.visualstudio.com/docs/setup/setup-overview) |
| 44 | +1. Install the [Teams Toolkit extension](https://marketplace.visualstudio.com/items?itemName=TeamsDevApp.ms-teams-vscode-extension) and [Python Extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python) |
| 45 | +1. Select **File > Open Folder** in VS Code and choose this samples directory from the repo |
| 46 | +1. Press **CTRL+Shift+P** to open the command box and enter **Python: Create Environment** to create and activate your desired virtual environment. Remember to select `requirements.txt` as dependencies to install when creating the virtual environment. |
| 47 | +1. Using the extension, sign in with your Microsoft 365 account where you have permissions to upload custom apps |
| 48 | +1. Select **Debug > Start Debugging** or **F5** to run the app in a Teams web client. |
| 49 | +1. In the browser that launches, select the **Add** button to install the app to Teams. |
| 50 | + |
| 51 | +> If you do not have permission to upload custom apps (sideloading), Teams Toolkit will recommend creating and using a Microsoft 365 Developer Program account - a free program to get your own dev environment sandbox that includes Teams. |
| 52 | +
|
| 53 | +## Setup for bot |
| 54 | + |
| 55 | +In Azure portal, create a [Azure Bot resource](https://docs.microsoft.com/azure/bot-service/bot-service-quickstart-registration). |
| 56 | + - For bot handle, make up a name. |
| 57 | + - Select "Use existing app registration" (Create the app registration in Microsoft Entra ID beforehand.) |
| 58 | + - __*If you don't have an Azure account*__ create an [Azure free account here](https://azure.microsoft.com/free/) |
| 59 | + |
| 60 | + In the new Azure Bot resource in the Portal, |
| 61 | + - Ensure that you've [enabled the Teams Channel](https://learn.microsoft.com/azure/bot-service/channel-connect-teams?view=azure-bot-service-4.0) |
| 62 | + - In Settings/Configuration/Messaging endpoint, enter the current `https` URL you were given by running the tunneling application. Append it with the path `/api/messages` |
| 63 | + |
| 64 | +## Run the app (Manually Uploading to Teams) |
| 65 | +## Setup for code |
| 66 | +> Note these instructions are for running the sample on your local machine, the tunnelling solution is required because |
| 67 | +the Teams service needs to call into the bot. |
| 68 | + |
| 69 | +1) Clone the repository |
| 70 | + |
| 71 | + ```bash |
| 72 | + git clone https://github.com/OfficeDev/Microsoft-Teams-Samples.git |
| 73 | + ``` |
| 74 | + |
| 75 | +2) Run ngrok - point to port 3978 |
| 76 | + |
| 77 | + ```bash |
| 78 | + ngrok http 3978 --host-header="localhost:3978" |
| 79 | + ``` |
| 80 | + |
| 81 | + Alternatively, you can also use the `dev tunnels`. Please follow [Create and host a dev tunnel](https://learn.microsoft.com/en-us/azure/developer/dev-tunnels/get-started?tabs=windows) and host the tunnel with anonymous user access command as shown below: |
| 82 | + |
| 83 | + ```bash |
| 84 | + devtunnel host -p 3978 --allow-anonymous |
| 85 | + ``` |
| 86 | + |
| 87 | +3) In a terminal, navigate to `samples/bot-message-reaction/Python` |
| 88 | + |
| 89 | +4) Activate your desired virtual environment |
| 90 | + |
| 91 | +5) Install dependencies by running ```pip install -r requirements.txt``` in the project folder. |
| 92 | + |
| 93 | +6) Update the `config.py` configuration for the bot to use the Microsoft App Id and App Password from the Bot Framework registration. (Note the App Password is referred to as the "client secret" in the azure portal and you can always create a new client secret anytime.) |
| 94 | + |
| 95 | +7) __*This step is specific to Teams.*__ |
| 96 | + - **Edit** the `manifest.json` contained in the `appManifest` folder to replace your Microsoft App Id (that was created when you registered your bot earlier) *everywhere* you see the place holder string `${{AAD_APP_CLIENT_ID}}` and `${{TEAMS_APP_ID}}` (depending on the scenario the Microsoft App Id may occur multiple times in the `manifest.json`) |
| 97 | + - **Zip** up the contents of the `appManifest` folder to create a `manifest.zip` |
| 98 | + - **Upload** the `manifest.zip` to Teams (in the Apps view click "Upload a custom app") |
| 99 | + |
| 100 | +8) Run your bot with `python app.py` |
| 101 | + |
| 102 | +## Running the sample |
| 103 | + |
| 104 | +Message the bot and it will respond with an 'Echo: [your message]'. Add a message reaction to the bots response, and the bot will reply accordingly. |
| 105 | + |
| 106 | +- **App Installation:** |
| 107 | + |
| 108 | + |
| 109 | +- **Personal Scope Interactions:** |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +- **Group Chat Scope Interactions:** |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +- **Team Scope Interactions:** |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +## Deploy the bot to Azure |
| 133 | + |
| 134 | +To learn more about deploying a bot to Azure, see [Deploy your bot to Azure](https://aka.ms/azuredeployment) for a complete list of deployment instructions. |
| 135 | + |
| 136 | +## Further reading |
| 137 | + |
| 138 | +- [Bot Framework Documentation](https://docs.botframework.com) |
| 139 | +- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) |
| 140 | +- [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0) |
| 141 | +- [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0) |
| 142 | +- [Teams Message Reaction Events](https://docs.microsoft.com/en-us/microsoftteams/platform/bots/how-to/conversations/subscribe-to-conversation-events?tabs=dotnet#message-reaction-events) |
| 143 | + |
| 144 | + |
| 145 | +<img src="https://pnptelemetry.azurewebsites.net/microsoft-teams-samples/samples/bot-message-reaction-Python" /> |
0 commit comments