page_type | languages | products | description | extensions | urlFragment | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sample |
|
|
This sample provides solutions for proactive messaging in Teams, including user and channel coordinate logging and message throttling. |
|
officedev-microsoft-teams-samples-bot-proactive-messaging-csharp |
This sample showcases two approaches for building proactive messaging apps in Microsoft Teams using C#. The Coordinate Logger solution captures user and channel conversation coordinates, while the Proactive CMD solution demonstrates how to send messages with policies that handle throttling, ensuring reliable delivery.
Two samples to highlight solutions to two challenges with building proactive messaging apps in Microsoft Teams.
File/folder | Description |
---|---|
/coordinate-logger |
Sample of getting conversation coordinates using BotFramework Events & SDK. |
/proactive-cmd |
Sample of sending proactive messages with throttling policies. |
/teamsAppManifest |
App manifest for the teams app. |
.gitignore |
Define what to ignore at commit time. |
CHANGELOG.md |
List of changes to the sample. |
CONTRIBUTING.md |
Guidelines for contributing to the sample. |
README.md |
This README file. |
LICENSE |
The license for the sample. |
- Bots
- Microsoft Teams account
- .NET SDK version 6.0
- Publicly addressable https url or tunnel such as dev tunnel or ngrok latest version or Tunnel Relay
-
Configure public url to point to http port 5000
ngrok http 5000 --host-header="localhost:5000"
Alternatively, you can also use the
dev tunnels
. Please follow Create and host a dev tunnel and host the tunnel with anonymous user access command as shown below:devtunnel host -p 5000 --allow-anonymous
-
Create a Microsoft Entra ID app registration and Azure Bot in Azure portal: Azure Bot registration resource.
- Ensure that you've enabled the Teams Channel
- configure the messaging endpoint in Azure bot with your application domain URL and append
/api/messages
like:https://111xxx.ngrok-free.app/api/messages
-
Modify the
manifest.json
in the/AppManifest
folder and replace the{{BOT-ID}}
with the id received while doing Microsoft Entra ID app registration in previous steps and also update the<<App Domain>>
with your application domain like ngrok domain:111xxx.ngrok-free.app
or dev tunnels domain:12345.devtunnels.ms
excluding http/https. -
Zip the contents of
AppManifest
into amanifest.zip
. -
Modify the
/coordinate-logger/appsettings.local.json
file and fill in the{{ Bot Id }}
and{{ Bot Password }}
with the id's received while doing Microsoft Entra ID app registration in previous steps.
Coordinate Logger
Note this is a noisy application, it is recommended you not run this in shared teams where you would disturb other users!
-
Start the application
# dotnet run
It is running when the following output is displayed.
Hosting environment: Development Content root path: C:\msteams-samples-proactive-messaging\coordinate-logger Now listening on: https://localhost:5001 Now listening on: http://localhost:5000 Application started. Press Ctrl+C to shut down.
-
Install the app personally in Teams
- Go to the
Apps
in the left rail - Select
Upload a custom app
and select the manifest.json file - Click the
Add
Button on the consent screen
The coordinates of the user should be log in the console window(refer above image)
- Go to the
-
Install the app to a Team
- Go to the
Apps
in the left rail - Select
Upload a custom app
and select the manifest.json file - Select the down arrow next to the
Add
Button on the consent screen - Select a Team to install to.
The app will send a message to each channel in the Team & log the coordinates for each new thread to the console window.(refer above image)
- Go to the
Proactive CMD
-
Send a message to a user Using the values from the Coordinate Logger for a User's conversation coordinates & Bot registration fill in the parameters to the following command.
# dotnet run -- sendUserMessage --app-id="{{Bot Id}}" --app-password="{{Bot Password}}" --service-url="{{ServiceUrl}}" --conversation-id="{{Conversation Id}}" --message="Send Message to a User"
This will send a message to the 1-on-1 conversation with the user
-
Send a message to a thread Using the values from the Coordinate Logger for a Channel Thread's conversation coordinates & Bot registration fill in the parameters to the following command.
# dotnet run -- sendChannelThread --app-id="{{Bot Id}}" --app-password="{{Bot Password}}" --service-url="{{ServiceUrl}}" --conversation-id="{{Conversation Id}}" --message="Send Message to a Thread"
This will send a message to the thread
Team name in which message will post
The two samples correspond with two of the most common challenges when building proactive messaging apps in Microsoft Teams, getting the conversation coordinates and sending messages reliably.
The CoordinateLoggerActivityHandler.cs
file in the Conversation Logger demonstrates three separate ways to obtain or generate conversation coordinates for users or channel threads using the BotBuilder SDK.
The SendWithRetries
function and policy from the CreatePolicy
function demonstrate how to reliably send messages when dealing with throttling from Microsoft Teams.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.