0

我正在尝试使用机器人在频道中发送消息,但出现以下错误。

错误:TypeError:无法读取未定义的属性“创建”

在调试时我发现那context.adapter.ConnectorFactoryKey是空的

我从这里的源代码中获取代码的动机:

https://github.com/microsoft/BotBuilder-Samples/tree/main/samples/javascript_nodejs/58.teams-start-new-thread-in-channel

团队机器人.js


    const {
        TurnContext,
        MessageFactory,
        TeamsActivityHandler,
        teamsGetChannelId
    } = require('botbuilder');
    
    class TeamsStartNewThreadInChannel extends TeamsActivityHandler {
        constructor() {
            super();
    
            this.onMessage(async (context, next) => {
                const teamsChannelId = teamsGetChannelId(context.activity);
                const message = MessageFactory.text('This will be the first message in a new thread');
                const newConversation = await this.teamsCreateConversation(context, teamsChannelId, message);
    
                await context.adapter.continueConversationAsync(process.env.MicrosoftAppId, newConversation[0], async turnContext => {
                    await turnContext.sendActivity(MessageFactory.text('This will be the first response to the new thread'));
                });
    
                await next();
            });
        }
    
        async teamsCreateConversation(context, teamsChannelId, message) {
            const conversationParameters = {
                isGroup: true,
                channelData: {
                    channel: {
                        id: teamsChannelId
                    }
                },
    
                activity: message
            };
    
            const connectorFactory = context.turnState.get(context.adapter.ConnectorFactoryKey);
            const connectorClient = await connectorFactory.create(context.activity.serviceUrl);
    
            const conversationResourceResponse = await connectorClient.conversations.createConversation(conversationParameters);
            const conversationReference = TurnContext.getConversationReference(context.activity);
            conversationReference.conversation.id = conversationResourceResponse.id;
            return [conversationReference, conversationResourceResponse.activityId];
        }
    }

清单.json

{
    "$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.11/MicrosoftTeams.schema.json",
    "manifestVersion": "1.11",
    "version": "1.0.0",
    "id": "ef2axxx-xx-xx-xxx-4810d",
    "packageName": "com.microsoft.teams.extension",
    "developer": {
        "name": "Teams App, Inc.",
        "websiteUrl": "https://www.example.com",
        "privacyUrl": "https://www.example.com/index.html#/privacy",
        "termsOfUseUrl": "https://www.example.com/index.html#/termsofuse"
    },
    "icons": {
        "color": "resources/color.png",
        "outline": "resources/outline.png"
    },
    "name": {
        "short": "heloo-local-debug",
        "full": "heloo-local-debug"
    },
    "description": {
        "short": "Short description of heloo-local-debug",
        "full": "Full description of heloo-local-debug"
    },
    "accentColor": "#FFFFFF",
    "bots": [
        {
            "botId": "ddd5e0xxx-xxx-xxx-xxxx-5e89c1a83d",
            "scopes": [
                "personal",
                "team",
                "groupchat"
            ],
            "supportsFiles": false,
            "isNotificationOnly": false,
            "commandLists": [
                {
                    "scopes": [
                        "personal",
                        "team",
                        "groupchat"
                    ],
                    "commands": [
                        {
                            "title": "welcome",
                            "description": "Resend welcome card of this Bot"
                        },
                        {
                            "title": "learn",
                            "description": "Learn about Adaptive Card and Bot Command"
                        }
                    ]
                }
            ]
        }
    ],
    "composeExtensions": [],
    "configurableTabs": [],
    "staticTabs": [],
    "permissions": [
        "identity",
        "messageTeamMembers"
    ],
    "validDomains": [
        "d510-xxx-xxx-215.ngrok.io"
    ],
    "webApplicationInfo": {
        "id": "6-xxx-xxx-xxx7",
        "resource": "api://botid-dd-xxxxx-xxx-xxx565e89c1a83d"
    }
}

我在上面做错了什么?

4

1 回答 1

1

请记住,Bot Framework 可以在许多情况下使用,Teams 只是其中之一。但是,当你在 Teams 中时,没有与用户“创建”对话的概念。他们只是一个单一的“对话”,而你基本上是在“继续”对话。结果,您想调用continueConversation. 看看我不久前发布的这个示例- 我已经链接到最相关的行。

请注意,正如我上面提到的,这要求用户已经安装了您的机器人应用程序 - 如果没有现有上下文,您将无法与用户开始主动对话。如果您想了解如何预安装机器人,请参阅以下问题:Proactively Install / Push Apps in Teams for Multiple Users

于 2021-12-28T18:45:48.313 回答