我是团队工具包的新手。我正在尝试构建一个可以发送消息和接收通知的机器人。我正在使用团队工具包中提供的示例代码。当我单独运行启用 SSO 的应用程序时,它工作正常。当我单独运行通知机器人时,它工作正常并且我收到通知。当我尝试合并两者时,通知不起作用。无法提取该成员。机器人必须向成员发送通知,而不是向频道发送通知。目前我仅在 VS 代码中进行本地测试。不使用任何 Azure 功能。使用以下端点触发通知 http://localhost:3978/api/[电子邮件受保护]
下面的代码失败并且成员未定义
const member = await notificationApp.notification.findMember(
async (m) => m.account.email === '[email protected]'
);
await member?.sendAdaptiveCard(AdaptiveCards.declare<CardData>(notificationTemplate).render({
title: "New Event Occurred!",
appName: "Contoso App Notification",
description: `This is a sample http-triggered notification to`,
notificationUrl: "https://aka.ms/teamsfx-notification-new",
}));
下面是我的index.js。
// Import required packages
import * as restify from "restify";
import * as path from "path";
// Import required bot services.
// See https://aka.ms/bot-services to learn more about the different parts of a bot.
import {
CloudAdapter,
ConfigurationServiceClientCredentialFactory,
ConfigurationBotFrameworkAuthentication, TurnContext
} from "botbuilder";
// This bot's main dialog.
import { TeamsBot } from "./teamsBot";
import config from "./config";
import { AdaptiveCards } from "@microsoft/adaptivecards-tools";
import notificationTemplate from "./adaptiveCards/notification-default.json";
import { notificationApp } from "./internal/initialize";
import { CardData } from "./cardModels";
// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about adapters.
const credentialsFactory = new ConfigurationServiceClientCredentialFactory({
MicrosoftAppId: config.botId,
MicrosoftAppPassword: config.botPassword,
MicrosoftAppType: "MultiTenant",
});
const botFrameworkAuthentication = new ConfigurationBotFrameworkAuthentication(
{},
credentialsFactory
);
const adapter = new CloudAdapter(botFrameworkAuthentication);
// Catch-all for errors.
const onTurnErrorHandler = async (context: TurnContext, error: Error) => {
// This check writes out errors to console log .vs. app insights.
// NOTE: In production environment, you should consider logging this to Azure
// application insights.
console.error(`\n [onTurnError] unhandled error: ${error}`);
// Send a trace activity, which will be displayed in Bot Framework Emulator
await context.sendTraceActivity(
"OnTurnError Trace",
`${error}`,
"https://www.botframework.com/schemas/error",
"TurnError"
);
// Send a message to the user
await context.sendActivity(`The bot encountered unhandled error:\n ${error.message}`);
await context.sendActivity("To continue to run this bot, please fix the bot source code.");
};
// Set the onTurnError for the singleton CloudAdapter
adapter.onTurnError = onTurnErrorHandler;
// Create the bot that will handle incoming messages.
const bot = new TeamsBot();
// Create HTTP server.
const server = restify.createServer();
server.use(restify.plugins.bodyParser());
server.listen(process.env.port || process.env.PORT || 3978, () => {
console.log(`\nBot Started, ${server.name} listening to ${server.url}`);
});
// Listen for incoming requests.
server.post("/api/messages", async (req, res) => {
console.log('api/messages')
await adapter
.process(req, res, async (context) => {
await bot.run(context);
})
.catch((err) => {
// Error message including "412" means it is waiting for user's consent, which is a normal process of SSO, sholdn't throw this error.
if (!err.message.includes("412")) {
throw err;
}
});
});
server.get(
"/auth-:name(start|end).html",
restify.plugins.serveStatic({
directory: path.join(__dirname, "public"),
})
);
server.post(
"/api/notification",
restify.plugins.queryParser(),
restify.plugins.bodyParser(), // Add more parsers if needed
async (req, res) => {
await target.adapter.continueConversationAsync(
botId,
target.conversationReference,
async (context) => {
// trigger sso
});
// By default this function will iterate all the installation points and send an Adaptive Card
// to every installation.
const pageSize = 100;
const email= req.query.email;
console.log('notiictaion api call ')
let continuationToken: string | undefined = undefined;
const member = await notificationApp.notification.findMember(
async (m) => m.account.email === email
);
await member?.sendAdaptiveCard(AdaptiveCards.declare<CardData>(notificationTemplate).render({
title: "New Event Occurred!",
appName: "Contoso App Notification",
description: `This is a sample http-triggered notification to`,
notificationUrl: "https://aka.ms/teamsfx-notification-new",
}));
res.json({});
}
);
下面是配置的Notifictaionapp
import { BotBuilderCloudAdapter } from "@microsoft/teamsfx";
import ConversationBot = BotBuilderCloudAdapter.ConversationBot;
import config from "./../config";
// Create bot.
export const notificationApp = new ConversationBot({
// The bot id and password to create CloudAdapter.
// See https://aka.ms/about-bot-adapter to learn more about adapters.
adapterConfig: {
MicrosoftAppId: config.botId,
MicrosoftAppPassword: config.botPassword,
MicrosoftAppType: "MultiTenant",
},
// Enable notification
notification: {
enabled: true,
},
});
任何帮助或指导将不胜感激。
从您的描述来看,您遇到的问题似乎是
await notificationApp.notification.findMember(...)
返回未定义。
请检查您当前的工程是否包含
.notification.localstore.json
文件且内容不为空。该代码依赖于其中的内容来搜索成员。您可以在 https://github.com/OfficeDev/TeamsFx/wiki/Send-notification-to-Teams#customize-storage 找到更多信息。
如果文件为空,您可以在 Teams 中卸载该应用程序并再次安装,以便包可以将您的安装记录到文件中。