Introduction
Microsoft Teams has become an indispensable tool for businesses and organizations to communicate and collaborate. Sometimes, you might want to send a message to yourself as a reminder or to keep track of important information.In this tutorial, I will demonstrate how to send Microsoft Teams self-message using Graph SDK by creating a simple console application in .NET 6.
Requirements
For this you need the following:
- A Microsoft Teams account with a valid subscription.
- Microsoft Graph SDK installed in your development environment.
- A registered app in Azure Active Directory with required permissions.
- .NET Core installed on your development environment.
Note: When registering your application in Azure AD, you need to add a redirect URI with the following properties:
- Platform: Public client/native (Mobile & Desktop)
- Value: http://localhost
This is needed when authenticating using an interactive browser.
Step 1: Create a new .NET Core console application
Open a command prompt or terminal, and create a new .NET Core console application using the following command:
dotnet new console --name TeamsSelfMessage Navigate to the newly created project folder:
cd TeamsSelfMessage Step 2: Add necessary packages
To send a self-message on Microsoft Teams using the Graph SDK, first, install the necessary packages:
dotnet add package Microsoft.Graph dotnet add package Microsoft.Graph.Core dotnet add package Microsoft.Identity.Client dotnet add package Microsoft.Extensions.Configuration.Json dotnet add package Azure.Identity Step 3: Configure the app settings
Add a new file to the project named appsettings.json. Make sure you configure the Build Action to Content and the Copy to Output Directory to Copy Always.
{ "appId": "YOUR_APP_ID", "tenantId": "YOUR_TENANT_ID", } Replace YOUR_APP_ID and YOUR_TENANT_ID with your actual app registration details from Azure Active Directory.
Step 4: Set up the Graph SDK and authenticate
Open the Program.cs file and add the following using statements:
using System; using System.IO; using System.Threading.Tasks; using Azure.Identity; using Microsoft.Extensions.Configuration; using Microsoft.Graph; using Microsoft.Graph.Models; Add the following field to hold the scopes for the authentication of the GraphServiceClient:
private static string[] _graphScopes = new[] { "User.Read", "ChatMessage.Send", "Chat.ReadWrite" }; Also add the following field which denotes the chat with yourself:
private const string SELF_CHAT_ID = "48:notes"; Add the following method to authenticate and initialize the Graph SDK:
private static async Task<GraphServiceClient> GetGraphClient(IConfiguration configuration) { var interactiveBrowserCredentialOptions = new InteractiveBrowserCredentialOptions { ClientId = configuration["appId"], TenantId = configuration["tenantId"] }; var tokenCredential = new InteractiveBrowserCredential(interactiveBrowserCredentialOptions); var graphClient = new GraphServiceClient(tokenCredential, _graphScopes); _ = await graphClient.Me.GetAsync(); //trigger login return graphClient; } Step 5: Implement the self-message functionality
With the Graph SDK set up, we can now implement the functionality to send a self-message in Microsoft Teams. Add the following method to send a message to yourself:
private static async Task<ChatMessage> SendMessageAsync(GraphServiceClient graphClient, string messageContent) { var message = new ChatMessage { Body = new ItemBody { ContentType = BodyType.Html, Content = messageContent } }; return await graphClient.Me.Chats[SELF_CHAT_ID].Messages.PostAsync(message); } Step 6: Call the self-message methods from Main
Modify the Main method to call the self-message method as follows:
public static async Task Main(string[] args) { var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .Build(); var graphClient = await GetGraphClient(configuration); string messageContent = "This is a message to myself!"; ChatMessage sentMessage = await SendMessageAsync(graphClient, messageContent); Console.WriteLine($"Message sent with ID: {sentMessage.Id}"); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } Now, you can run the console application using the following command:
dotnet run Conclusion
The console application will prompt you for authentication and send a message to yourself on Microsoft Teams. You can check your Teams client to see the message.
That’s it! You have successfully implemented self-messaging functionality in a .NET Core console application using the Microsoft Graph SDK.
For completeness, here is the full code for the Program.cs below:
using System; using System.IO; using System.Threading.Tasks; using Azure.Identity; using Microsoft.Extensions.Configuration; using Microsoft.Graph; using Microsoft.Graph.Models; namespace TeamsSelfMessage { class Program { private static string[] _graphScopes = new[] { "User.Read", "ChatMessage.Send", "Chat.ReadWrite" }; private const string SELF_CHAT_ID = "48:notes"; public static async Task Main(string[] args) { var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .Build(); var graphClient = await GetGraphClient(configuration); string messageContent = "This is a message to myself!"; ChatMessage sentMessage = await SendMessageAsync(graphClient, messageContent); Console.WriteLine($"Message sent with ID: {sentMessage.Id}"); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } private static async Task<GraphServiceClient> GetGraphClient(IConfiguration configuration) { var interactiveBrowserCredentialOptions = new InteractiveBrowserCredentialOptions { ClientId = configuration["appId"], TenantId = configuration["tenantId"] }; var tokenCredential = new InteractiveBrowserCredential(interactiveBrowserCredentialOptions); var graphClient = new GraphServiceClient(tokenCredential, _graphScopes); _ = await graphClient.Me.GetAsync(); //trigger login return graphClient; } private static async Task<ChatMessage> SendMessageAsync(GraphServiceClient graphClient, string messageContent) { var message = new ChatMessage { Body = new ItemBody { ContentType = BodyType.Html, Content = messageContent } }; return await graphClient.Me.Chats[SELF_CHAT_ID].Messages.PostAsync(message); } } } You can also find the GitHub repository with the complete solution here.
Top comments (0)