Access Outlook Calendar with Bot Framework and Microsoft Graph

This article is based on the exercises from our workshop "Chat-bot-in-a-Day".

To make everything smooth for our readers and as well as the participants in our workshops we use Gitpod(dot)io, an Online IDE based on VS Code. Meaning that you can develop and run the bot in the cloud, no wasted time installing Visual Studio, dotnet or any additional binary. Initially, we faced issues to run ASPNET Core applications on Gtipod, we reached out and their team jumped right into it and solved it in no time.

 

 

In this exercise we represent how to delegate access to the bot to request data for your user, in our case, it's events in your Outlook Calendar. Let's get started.

Getting started

Either clone the repository Bot-Mastery or open it in Gitpod by clicking the image below:

gitpod io icon

Navigate to the directory by executing the command below:

cd ./chap-4/MsGraphBot

Before starting the application, we need to create an Azure Active Directory application and register it with our bot.

Creating an Azure AD v2 Application

As the Azure portal will change, we will link to the official documentation for creating an Azure AD v2 Application. But keep in mind that when selecting the graph permissions you need to in addition to the proposed one's select Calendars.Read, Calendars.ReadWrite for Application and Delegated permissions as summarized below:

Calendar.Read, Calendar.ReadWrite, openid, profile, User.Read, and User.ReadBasic.All

After creating the AAD Application as shown in the documentation you will have the Application ID and Application Secret, with these, let's move into the next section.

Creating an Azure Bot Service and Register with AAD Application

Navigate to the Azure Portal and create a Bot Channels Registration, with the bot application created navigate under Settings and add a new Setting:

ConnectionSetting

Sample values:

  • Name: BotAuthentication
  • Service Provider:  Azure Active Directory v2
  • Client Id and Client Secret are the values generated in the earlier section.
  • Tenant ID: common (enables every tenant to be able to use the bot for their data)
  • Scopes: Calendar.Read, Calendar.ReadWrite, openid, profile, User.Read, and User.ReadBasic.All

If you name the bot differently, you have to update the variable ConnectionName in the class MsGraphBot.cs.

Getting the App Id and App Password and Running the App

In the created bot service navigate to Settings and click on 'Manage' as shown below:

manage

Copy the Application Id and generate a new password:

newpassword

Update the properties appId and appPassword in the file MsGraphBot.bot located at MsGraphBot.bot.

And run the app from the following directory [bot-mastery]\chap-4\MsGraphBot, where you should be if you followed the article.

dotnet run

Testing the bot 

After running the bot Gitpod IO provides you with a publicly accessible URL. Use the URL to configure the bot framework as follows:

Endpoint url: Url provided after running the bot from Gitpod.

Name: Any name to help you distinguish the bot later on.

Application Id and Application Password are the values generated in the earlier section.

With these values start chatting with the bot!

BotUsage

 

Using the OAuth flow the user provides the bot with a token that is used to request resources from Microsoft Graph API.

The important points in the code are:

1. The OAuth Prompt Settings:

private readonly OAuthPromptSettings _oauthPromptSettings = new OAuthPromptSettings
{
ConnectionName = ConnectionName, // The connection name used in New Connection Setting
Text = "Please Sign In",
Title = "Sign In",
Timeout = 300000, // Time to log in
};

2. The OAuth Prompt dialog:

_dialogs.Add(new OAuthPrompt(LoginPromptName, _oauthPromptSettings));

3. Using the token with Microsoft Graph:

if (tokenResponse != null)
{
var events = await CalendarClient.FromToken(tokenResponse.Token).GetCalendarEvents();
string eventsFormatted = string.Join("\n",
events.Select(s => $"- {s.Subject} at {DateTime.Parse(s.Start.DateTime).ToShortTimeString()} ")
.ToList());
 
await step.Context.SendActivityAsync("You have the following events: \n" + eventsFormatted,
cancellationToken: cancellationToken);
}
 

Quite simple! Check here again for the next samples from our workshop.

  • Erstellt am .

Standorte

Region Nord: Hamburg, Emden
Region Mitte: Langenfeld, Offenbach am Main
Region Ost: Leipzig
Region Süd: München, Ulm
Copyright by Orange Networks GmbH