This is a sample example OpenAI bot for Microsoft Teams. It allows a user in Teams to send a chat message to the Bot, which then uses OpenAI API to get an answer to the chat message.
Please note: Currently there is no publicly available API for OpenAI's ChatGPT. This sample uses the OpenAI API, which models doesn't currently include ChatGPT. The OpenAI API is currently in beta, and is not free. You can sign up for a free account here. For a list of supported models, see here.
-
OpenAI account and API key
-
Node.js version 14 or higher
# determine node version node --version
-
Azure Functions Core Tools version 4 or higher
# determine core tools version func --version
-
ngrok - Although a free account will work with this sample, the tunnel subdomain will change each time you run ngrok, requiring a change to the Azure Bot messaging endpoint and the Teams app manifest. A paid account with a permanent subdomain is recommended.
Version | Date | Author | Comments |
---|---|---|---|
1.0 | Jan 3, 2023 | Lee Ford | Initial release |
THIS CODE IS PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
Start ngrok listening on port 7071 (or whatever port you are running the Azure Function on)
ngrok http 7071
If you have a paid account, add the subdomain:
# Replace 12345678 with your ngrok subdomain
ngrok http 7071 -subdomain=12345678
Take a note of the forwarding URL, as you will need it later.
- Go to the Azure Portal
- Search for Azure Bot and choose Create
- Populate the bot with the following:
- With the bot created, under Configuration:
- Take a note of the Microsoft App ID and Microsoft App Tenant ID (you will need these later)
- Populate the Messaging endpoint with the ngrok forwarding URL (with
/api/messages
appended). Note: if you are not using a paid account, you will need to update this each time you restart ngrok. - Click on Manage Password and create a New client secret. Specify a name and when it expires. Take a copy of the Secret value (you will need this later)
Note in a production environment, you should be using a Managed Identity to authenticate to the Azure Bot, rather than a client secret.
- Under Channels, add the Microsoft Teams channel and enable messaging
-
Clone this repository
-
Create and populate a
local.settings.json
file in the root folder with the following (with your own values):{ "IsEncrypted": false, "Values": { "FUNCTIONS_WORKER_RUNTIME": "node", "AzureWebJobsStorage": "", "OPENAI_API_KEY": "<YOUR OPENAI API KEY>", "OPENAI_MODEL": "text-davinci-003", // Or whatever model you want to use (see https://beta.openai.com/docs/api-reference/models) "MicrosoftAppId": "<YOUR MICROSOFT APP REGISTRATION ID>", "MicrosoftAppPassword": "<YOUR MICROSOFT APP REGISTRATION CLIENT SECRET>", "MicrosoftAppTenantId": "<YOUR MICROSOFT APP REGISTRATION TENANT ID>", "MicrosoftAppType": "SingleTenant" } }
-
Run the following to install, build and run the code (from the root folder):
npm install npm run build func host start
- Edit the
manifest.json
file and replace theid
andbotId
values with the Microsoft App ID from the Azure Bot - Zip the
manifest
folder into amanifest.zip
file - Upload the
manifest.zip
file to Teams (side load the or upload in Teams Admin Center)