Set Up Your Development Environment
- Ensure you have a Microsoft Teams account and relevant permissions to create and manage bots.
- Sign up or log into your OpenAI account to access API details for integration.
- Install the necessary development tools, such as Node.js and Ngrok, to facilitate local testing and tunneling.
Create a Bot in Microsoft Teams
- Access the Microsoft Bot Framework Portal and create a new bot registration. Provide all necessary details like bot name and description.
- Configure the messaging endpoint with Ngrok. For example, start Ngrok on your local machine, and use the forwarding URL as your endpoint.
ngrok http 3978
Set Up OpenAI API
- Visit OpenAI Platform and navigate to the API section. Obtain your API keys, as this will be crucial for making requests.
- Familiarize yourself with OpenAI's API documentation to understand how to send queries and receive responses.
Develop the Bot Application
- Create a Node.js application. Use npm to install necessary packages such as 'botbuilder' and 'axios' to facilitate bot development and API requests.
npm init -y
npm install botbuilder axios
- Set up your bot application using the Bot Framework SDK. Implement the basic framework to process incoming messages and send responses.
const { BotFrameworkAdapter } = require('botbuilder');
const express = require('express');
const axios = require('axios');
const server = express();
server.listen(process.env.PORT || 3978, () => console.log('Bot server listening...'));
const adapter = new BotFrameworkAdapter();
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (context) => {
if (context.activity.type === 'message') {
await context.sendActivity(`Echo: ${context.activity.text}`);
}
});
});
Integrate OpenAI with the Bot
- Modify the message processing logic to include a call to OpenAI's API. Use 'axios' to make HTTP requests to OpenAI, and handle the response to deliver meaningful replies.
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (context) => {
if (context.activity.type === 'message') {
const userMessage = context.activity.text;
const response = await axios.post('https://api.openai.com/v1/engines/davinci-codex/completions', {
prompt: userMessage,
max_tokens: 150
}, {
headers: {
'Authorization': `Bearer YOUR_OPENAI_API_KEY`,
'Content-Type': 'application/json'
}
});
await context.sendActivity(response.data.choices[0].text.trim());
}
});
});
Test and Deploy Your Bot
- Test your bot locally using Ngrok to ensure it responds correctly and seamlessly integrates OpenAI responses within Microsoft Teams.
- Once testing is successful, publish your bot to Azure or any other compatible cloud service to make it publicly accessible to your team.
Add Your Bot to Microsoft Teams
- Generate a Teams app package using the Microsoft Teams Developer Portal. Fill in necessary app details, such as name and description.
- Upload this package to your Microsoft Teams environment, enabling users to interact with the OpenAI-integrated bot effortlessly.