首页 > 解决方案 > 使用 Microsoft Teams 机器人进行频道和群聊对话

问题描述

目前正在使用 Watson Assistant,我的新项目是将其与 Microsoft Teams 集成。它已经在 1 对 1 对话中起作用,但我不知道如何让它在频道或群组中回答。我是打字稿的新手,所以我还不了解大部分内容。有什么想法或提示可以帮助我实现这一点吗?当我寻求帮助时,我被告知尝试使用下面链接的信息在此处实施它。

https://docs.microsoft.com/en-us/microsoftteams/platform/bots/how-to/conversations/channel-and-group-conversations?tabs=typescript

    public async handleMessage(context: TurnContext) {
    let dialogResult: DialogTurnResult;

    const dc: DialogContext = await this.dialogs.createContext(context);

    let ibmWatsonContextId = await this.ibmWatsonContextId.get(context, null);
    if (ibmWatsonContextId === null) {
        ibmWatsonContextId = await this.retrieveAndSetIBMWatsonContextID(context);
    }
    // you can use the user in this function or any other dialog by passing the state property accessor
    // on local system, user won't be set automatically. so anonymous 
    const user = await this.userData.get(context, {
        firstname: "anonymous",
        lastname: "anonymous",
        email: "anonymous.anonymous@siemens.com",
        gid: "xxxxxxxx",
        department: "anonymous"
    });
    
    let response: MessageResponse;
    let topIntent: string;

    if (!dc.activeDialog || dc.activeDialog.id !== OptionsDialog.DIALOG_NAME) {
        response = await this.getConversationResponse(context.activity.text, context, ibmWatsonContextId)

        topIntent = await this.getTopIntent(response);

        const interrupted = await this.isTurnInterrupted(dc, topIntent);
        if (interrupted) {
            if (dc.activeDialog !== undefined) {
                // issue a re-prompt on the active dialog
                await dc.repromptDialog();
            } // Else: We dont have an active dialog so nothing to continue here.
        }
    }

    dialogResult = await dc.continueDialog();
    
    if (typeof dialogResult.result === "object" && dialogResult.result.rerunNLU) {
        response = await this.getConversationResponse(dialogResult.result.result, context, ibmWatsonContextId);
        topIntent = await this.getTopIntent(response);
    }

    // If no active dialog or no active dialog has responded,
    if (!dc.context.responded) {
        // Switch on return results from any active dialog.
        switch (dialogResult.status) {
            // dc.continueDialog() returns DialogTurnStatus.empty if there are no active dialogs
            case DialogTurnStatus.empty:
                if (this.intentSet.has(topIntent)) {
                    await dc.beginDialog(topIntent);
                }
            case DialogTurnStatus.complete:
                // The dialog has completed, but we haven't given the user any response. Maybe we want to send a default answer here?
                if (!this.intentSet.has(topIntent)) {
                    for (let messageResponse of response.output.generic) {
                        await this.nluResponseHandler.handle(dc, messageResponse);
                    }
                }
                break;
            case DialogTurnStatus.waiting:
                // The active dialog is waiting for a response from the user, so do nothing.
                break;
                // All child dialogs have ended. so do nothing.
                break;
            default:
                // Unrecognized status from child dialog. Cancel all dialogs.
                await dc.cancelAllDialogs();
                break;
        }
    }

    await this.userState.saveChanges(context);
    await this.conversationState.saveChanges(context);
}

如果需要更多代码,我将不得不请求许可。提前致谢,

标签: typescriptmicrosoft-teamswatson-assistant

解决方案


推荐阅读