簡體   English   中英

如何在 Microsoft Team 應用程序中使用 @microosft/teamsfx 的 ConversationBot 和 botbuiler 的 EventHandler

[英]How to use ConversationBot from @microosft/teamsfx and EventHandler from botbuiler in the microsoft team app

我正在使用 teamsfx 工具包開發一個應用程序,我有兩個重要的用例。

需要通過 API 向特定用戶發送通知 我還需要向新安裝的用戶發送歡迎信息。 我正在使用 @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()) {

我還需要擴展一個 EventHandler class 來覆蓋

this.onMembersAdded(async (context, next) => {() 

發送歡迎消息的方法。 但是我無法在同一個應用程序中喜歡這兩個類。

我有

const bot = new ConversationBot

就像我也有

class WelcomeBot extends ActivityHandler
{
this.onMemebersAdded(){//sending welcome message}

所以問題是如何在同一個機器人應用程序中鏈接兩者,以便我可以使用 EventHandler 發送歡迎消息並通過 ConversationBot 發送通知?

如果您的應用程序是通過 Teams Toolkit 創建的,您可能有以下代碼(通常在index.js|ts中):

server.post("/api/messages", async (req, res) => {
  await bot.requestHandler(req, res);
});

這里的bot是一個ConversationBot 要將WelcomeBotConversationBot集成,請更改代碼await bot.requestHandler(req, res); await bot.adapter.processActivity(req, res, (context) => welcomeBot.run(context));

完整代碼可能如下所示:

/// 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));
});

背后的細節是——所有的bot操作都依賴於Adapter ConversationBot提供了一個輔助方法requestHandler來簡化代碼。 但是,如果您想將自己的機器人邏輯添加到Adapter中,則需要通過ConversationBot.adapter顯式獲取Adapter ,然后添加您自己的邏輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM