Teams-logo.png Open-AI-logo.png

Introduction

A Teams bot to let you chat with ChatGPT on Teams.

Build and Test

Prerequisites

  1. Install Node.js: Node.js (nodejs.org)
  2. Install Git: Git for Windows
  3. Install VS Code: Visual Studio Code - Code Editing. Redefined
  4. Install Azure Function Extension on VSCode In VSCode, go to extensions and search Azure Function to isntall it: image
  5. Ensure you have a Microsoft 365 account and follow the Teams development document to register an App with bot feature.

image Ensure the below permissions are set and you can add a command to allow users to delete chat history. image Please note the bot id and create a client secret which will be used in later.

Bot Id/MicrosoftAppId You can go to Tools in Teams Developer portal to find the bot you created. image

Client Secret/MicrosoftAppPassword And create a new client secret. image

  1. Ensure you have an Azure subscription to create these resources in one resource group:

How to run it

  1. Open Terminal and clone the project: git clone https://github.com/yangsa666/node-js-teams-bot-with-chatgpt.git.

  2. Open the project folder with VS Code.

    image
  3. Raname local.samplesettings.json to local.settings.json, fill the required information. In this sample, it uses MongoDb to store user's chat history with ChatGPT. If you don't have a MongoDb, you can create one on Azure for test.

    image image

AzureWebJobsStorage: Azure storage account connection string. Create a storage account in Azure Portal and go to Access Key to copy a connection string. image

MicrosoftAppTenantId: Your Azure AD / MIcrosoft Entra Tenant ID.

MicrosoftAppId: Bot Id of the bot you created previously.

MicrosoftAppPassword: Client secret of the bot you created previously.

OPENAI_API_KEY: Your OpenAI API key.

OpenAI_MODEL: Your OpenAI model

MongoDb_Connection_String: The connectionstring of the Azure CosmosDB for Mongo you created previously. image

  1. Open Azure Functions Extension, sign in your Azure subscription account to deploy it in the resource group you created before and upload settings to Azure Functions App. See more details here.

    !!!Note!!! If you're using Azure Open AI API, please switch to AzureOpenAIStack branch. You can switch branch via the steps below in VSCode.

    image

    image

  2. After deploying it, configure the Azure function URL in the Teams bot Endpoint URL in Teams developer portal. You can get Azure Function URL by going to Azure Portal, locate your function app you created in the steps above. Then go to messages function, click Get Function Url button. image

    Then you can go back to Teams Developer portal > Tools > find the bot you created > Configure. In the endpoint address field, fill the Azure Function Url you got. NOTE: Please remove ? in the endpoint address. image

  3. Install or preview the Teams bot app on your teams To export the installation package, you can go to Teams Developer portal > locate your app > Publish > Download the app package. image

    Then you can go to Teams to upload this as a custom app. Reference: https://learn.microsoft.com/en-us/microsoftteams/platform/concepts/deploy-and-publish/apps-upload

  4. Enjoy it by saying hello to the bot.

    image