[英]How to make a REST API call from Microsoft Bot for Teams using node js?
I have created a Microsoft Bot using the following documentation https://docs.microsoft.com/en-us/microsoftteams/platform/toolkit/visual-studio-code-overview .我使用以下文档https://docs.microsoft.com/en-us/microsoftteams/platform/toolkit/visual-studio-code-overview创建了一个 Microsoft Bot。 Now what I want to achieve is if user enters "show data" in the chat, I want to display the data which I will get from an API endpoint in JSON format.现在我想要实现的是,如果用户在聊天中输入“显示数据”,我想以 JSON 格式显示我将从 API 端点获取的数据。 I have tried the following code creating a new file named getData.js
:我尝试使用以下代码创建一个名为getData.js
的新文件:
const axios = require('axios');
class getData {
async getDataJson() {
const config = {
method: 'get',
url: 'https://someurl',
headers: { 'name': 'user_name', 'token': 'token' }
}
let res = await axios(config);
let data = res.data;
return data;
}
}
module.exports.getData = getData;
If this method is correct, how do I use it to send reply to the user the result data ?Also how do I populate the headers which should be the email and token generated using the SSO authentication method used in dialog/mainDialog.js
?如果此方法是正确的,我如何使用它向用户发送结果数据的回复?此外,我如何填充应该是使用dialog/mainDialog.js
使用的 SSO 身份验证方法生成的电子邮件和令牌的标头?
After you get the token from maindialog pass it as a parameter in a function.(I have a function called findintent that is where I pass the token)从maindialog获取令牌后,将其作为函数中的参数传递。(我有一个名为 findintent 的函数,这是我传递令牌的地方)
async processStep(step) {
console.log("Inside process step");
if (step.result) {
// We do not need to store the token in the bot. When we need the token we can
// send another prompt. If the token is valid the user will not need to log back in.
// The token will be available in the Result property of the task.
const tokenResponse = step.result;
// If we have the token use the user is authenticated so we may use it to make API calls.
if (tokenResponse && tokenResponse.token) {
await OAuthHelpers.findIntent(step.context, tokenResponse);
}
} else {
await step.context.sendActivity('We couldn\'t log you in. Please try again later.');
}
return await step.endDialog();
}
After that try using node fetch to get the data from API.之后尝试使用 node fetch 从 API 获取数据。
async findIntent(context,tokenResponse){
let header_info = {
Authorization: "Bearer " + tokenResponse.token
};
let respone = await fetch("your URL",{
method: 'GET'
headers: header_info
});
let result = await response.json();
console.log(result);
}
also refer this page https://www.npmjs.com/package/node-fetch for more info.另请参阅此页面https://www.npmjs.com/package/node-fetch了解更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.