AWS Machine Learning Blog

Schedule an appointment in Office 365 using an Amazon Lex bot

You can use chatbots for automating tasks such as scheduling appointments to improve productivity in enterprise and small business environments. In this blog post, we show how you can build the backend integration for an appointment bot with the calendar software in Microsoft Office 365 Exchange Online. For scheduling appointments, the bot interacts with the end user to find convenient time slots and reserves a slot.

We use the scenario of a retail banking customer booking an appointment using a chatbot powered by Amazon Lex. The bank offers personal banking services and investment banking services and uses Office 365 Exchange Online for email and calendars.

Bank customers interact with the bot using a web browser. Behind the scenes, Amazon Lex uses an AWS Lambda function to connect with the banking agent’s Office 365 calendar. This function looks up the bank agent’s calendar and provides available times to Amazon Lex, so these can be displayed to the end user. After the booking is complete, an invitation is saved on the agent’s Office 365 and the bank customer’s calendar as shown in the following graphic:

The following flowchart describes the scenario:


To achieve this automation we use an AWS Lambda function to call Office 365 APIs to fulfill the Amazon Lex intent. The Office 365 secrets are stored securely in AWS Secrets Manager. The bot is integrated with a web application that is hosted on Amazon S3Amazon Cognito  is used to authorize calls to Amazon Lex services from the web application.

To make it easy to build the solution, we have split it into three stages:

  • Stage 1: Create an Office 365 application. In this stage, you create an application in Office 365. The application is necessary to call the Microsoft Graph Calendar APIs for discovering and booking free calendar slots. You need to work with your Azure Active Directory (AAD) admin to complete this stage.
  • Stage 2: Create the Amazon Lex bot for booking appointments. In this stage, you create an Amazon Lex bot with necessary intents, utterances, and slots. You also create an AWS Lambda function that calls Office 365 APIs for fulfilling the intent.
  • Stage 3: Deploy the bot to a website. After completion of stage 1 and stage 2, you have a fully functional bot that discovers and books Office 365 calendars slots.

Let’s start building the solution.

Stage 1: Create an Office 365 application

Follow these steps to create the Office 365 application. If you don’t have an existing office 365 account for testing, you can use the free trial of Office 365 business premium.

Note: To complete this stage, you will need to work with your Azure Active Directory administrator.

Log in to the Azure portal with your Office365 account credentials. Once logged in, click on Azure Active Directory.

  1. In the Azure Active Directory console, click on App registrations. In the app registration screen, click on new registration.
  2. On the new registration screen, enter a suitable name, select your choice of account type and click on Register.
  3. Make note of the Application (client) ID and Directory (tenant) ID
  4. Click on View API Permissions
  5. In the API Permissions page, click on Add a permission
  6. Select Microsoft Graph and click on Application permissions
  7. In the Select permissions section, search for Calendars.ReadWrite and click on Add permissions
  8. Click on grant admin consent. In the confirmation pop up, click Yes to grant Admin consent for this app
  9. In the left navigation, Click on certificates and secrets
  10. Click on New client secret and select a suitable expiration period and click on Add
  11. Copy the client secret and store it securely
  12. Request your AAD Administrator for the user id of the agents whose calendar you wish to book. This information is available on the Azure portal.
  13. To proceed to the next step, a few important parameters need to be saved. Open a text pad and create the following key value pairs. These are the keys that you need to use.


    Values/ Details

    Application Id The ID of the Office 365 application that you created. Specified in step 3
    Application Password The Office 365 application password stored in step 11
    Azure Active Directory Id The AAD Administrator has this information as described in step 3
    Investment Agent UserId The user ID of the investment agent from step 12
    Personal Agent UserId The User ID of the personal banking agent from step 12

Stage 2: Create the Amazon Lex bot for booking appointments

In this stage, you create the Amazon Lex bot and the AWS Lambda function and store the application passwords in AWS Secrets Manager. After completing this stage you will have a fully functional bot that is ready for deployment. The code for the lambda function is available here.

This stage is automated using AWS CloudFormation and accomplishes the following tasks:

  • Creates an Amazon Lex bot with required intents, utterances, and slots.
  • Stores Office 365 secrets in AWS Secrets Manager.
  • Deploys the AWS Lambda function.
  • Creates AWS Identity and Access Management (IAM) roles necessary for the AWS Lambda function.
  • Associates the Lambda function with the Amazon Lex bot.
  • Builds the Amazon Lex bot.

Choose the launch stack button to deploy the solution.

On the AWS CloudFormation console, use the data from Step 11 of Stage 1 as parameters to deploy the solution.

The key aspects of the solution are the Amazon Lex bot and the AWS Lambda function used for fulfilment. Let’s dive deep into these components.

Amazon Lex bot

The Amazon Lex bot consist of intents, utterances, and slots. The following image describes them.

AWS Lambda function

The AWS Lambda function gets inputs from Amazon Lex and calls Office 365 APIs to book appointments. The following are the key AWS Lambda functions and methods.

Function: 1 – Get Office 365 bearer token

To call Office 365 APIs, you first need to get the bearer token from Microsoft. The method described in this section gets the bearer token by passing the Office 365 application secrets stored in AWS Secrets Manager.

var reqBody = "client_id=" + ClientId + "&" + RedirectUri + "&grant_type=client_credentials&client_secret=" + ClientSecret;
    var url = "" + ADDirectoryId + "/oauth2/v2.0/token";{
        "headers": { "content-type": "application/x-www-form-urlencoded" },
        "url": url,
        "body": reqBody,
    }, (error, response, body) => {
        if (error) {
            return console.log(error);
	 accessToken = JSON.parse(body).access_token;
        if (bookAppointment) {

            BookAppointment(accessToken , //other params);
        else {
            GetDateValues(accessToken , //other params);

Function: 2 – Book calendar slots

This function books a slots in the agent’s calendar. The Graph API called is user/events. As noted earlier, the access token is necessary for all API calls and is passed as a header.

var postUrl = "" + userId + "/events";
    var endTime = parseInt(time) + 1;

    var pBody = JSON.stringify({
        "subject": "Customer meeting",
        "start": { "dateTime": date + "T" + time + ":00", "timeZone": timeZone },
        "end": { "dateTime": date + "T" + endTime + ":00:00", "timeZone": timeZone }
        "headers": {
            "Content-type": "application/json",
            "Authorization": "Bearer " + accesstoken
        "url": postUrl,
        "body": pBody
    }, (error, response, postResBody) => {
        if (error) {
            return console.log(error);

        //Return successful message to customer and complete the intent..

You have completed Stage 2, and you have built the bot. It’s now time to test the bot and deploy it on a website. Use the following steps to the test the bot in the Amazon Lex console.

Testing the bot

  1. In the Amazon Lex console, choose the MakeAppointment bot, choose Test bot, and then enter Book an appointment.
  2. Select Personal/ Investment and Choose a Day from the response cards.
  3. Specify a time from the list of slots available.
  4. Confirm the appointment.
  5. Go to the outlook calendar of the investment/ personal banking agent to verify that a slot has been booked on the calendar.

Congratulations! You have successfully deployed and tested a bot that is able to book appointments in Office 365.

Stage 3: Make the bot available on the web  

Now your bot is ready to be deployed. You can choose to deploy it on a mobile application or on messaging platforms like Facebook, Slack, and Twilio by using these instructions. You can also use this blog that shows you how you can integrate your Amazon Lex bot with a web application. It gives you an AWS CloudFormation template to deploy the web application.

Note: To deploy this in production, use AWS Cognito user pools or use federation to add authentication and authorization to access the website.

Clean up

You can delete the entire CloudFormation stack. Open the AWS CloudFormation console, select the stack, and choose the Delete Stack option on the Actions menu. It will delete all the AWS Lambda functions and secrets stored in AWS Secrets Manager. To delete the bot, go to the Amazon Lex console, select the Make Appointments bot, and then choose Delete on the Actions menu.


This blog post shows you how to build a bot that schedules appointments with Office 365 and deploys it to your website within minutes. This is one of the many ways bots can help you improve productivity and deliver a better customer experience.

About the Author

Rahul Kulkarni is a solutions architect at Amazon Web Services. He works with partners and customers to help them build on AWS