[英]How to use ConversationBot from @microosft/teamsfx and EventHandler from botbuiler in the microsoft team app
I am developing an app using teamsfx toolkit and I have two important use case.我正在使用 teamsfx 工具包开发一个应用程序,我有两个重要的用例。
Need to send notifications to particular users via API I also need to send a welcome message to newly installed users.需要通过 API 向特定用户发送通知 我还需要向新安装的用户发送欢迎信息。 I am using ConversationBot from @microosft/temasfx library to set adapter config and send notifications to users using API/notification URL like
我正在使用 @microosft/temasfx 库中的 ConversationBot 来设置适配器配置并使用 API/通知 URL 向用户发送通知,例如
const bot = new ConversationBot({
// The bot id and password to create BotFrameworkAdapter.
adapterConfig: {
appId: config.botId,
appPassword: config.botPassword,
}}
// and when the server.post api use this to send notification
for (const target of await Mybot.notification.installations()) {
I also need to extend an EventHandler class to override我还需要扩展一个 EventHandler class 来覆盖
this.onMembersAdded(async (context, next) => {()
method to send a welcome message.发送欢迎消息的方法。 But I am unable to like these two classes in the same app.
但是我无法在同一个应用程序中喜欢这两个类。
I have我有
const bot = new ConversationBot
Like I also have就像我也有
class WelcomeBot extends ActivityHandler
{
this.onMemebersAdded(){//sending welcome message}
So the question is how to link both in the same bot app so that I can send a welcome Message using EventHandler and send Notifications via ConversationBot?所以问题是如何在同一个机器人应用程序中链接两者,以便我可以使用 EventHandler 发送欢迎消息并通过 ConversationBot 发送通知?
If your app is created via Teams Toolkit, probably you have following code (normally in index.js|ts
):如果您的应用程序是通过 Teams Toolkit 创建的,您可能有以下代码(通常在
index.js|ts
中):
server.post("/api/messages", async (req, res) => {
await bot.requestHandler(req, res);
});
Here bot
is a ConversationBot
.这里的
bot
是一个ConversationBot
。 To integrate your WelcomeBot
with ConversationBot
, please change the code await bot.requestHandler(req, res);
要将
WelcomeBot
与ConversationBot
集成,请更改代码await bot.requestHandler(req, res);
to await bot.adapter.processActivity(req, res, (context) => welcomeBot.run(context));
await bot.adapter.processActivity(req, res, (context) => welcomeBot.run(context));
The full code may look like:完整代码可能如下所示:
/// init somewhere
const bot = new ConversationBot...
const welcomeBot = new WelcomeBot...
/// The "/api/messages" handler
server.post("/api/messages", async (req, res) => {
await bot.adapter.processActivity(req, res, (context) => welcomeBot.run(context));
});
The details behind are - All bot operations depend on Adapter
.背后的细节是——所有的bot操作都依赖于
Adapter
。 The ConversationBot
provides a helper method requestHandler
to simplify the code. ConversationBot
提供了一个辅助方法requestHandler
来简化代码。 But if you'd like to add your own bot logic to the Adapter
, you need to explicitly get Adapter
via ConversationBot.adapter
, then add your own logic.但是,如果您想将自己的机器人逻辑添加到
Adapter
中,则需要通过ConversationBot.adapter
显式获取Adapter
,然后添加您自己的逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.