使用 Python 的 Bot Framework SDK v4 初始化并向 Microsoft Teams 频道发送消息

Initial and send a message to a Microsoft Teams channel using Bot Framework SDK v4 for Python

我试图在以下示例的帮助下初始化 proactive 消息并将其发送到一个 Microsoft 团队频道: https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/python/16.proactive-messages 我将此代码添加到示例中以启动消息:

connectorClient = await ADAPTER.create_connector_client(service_url=SERVICE_URL)
parameters = ConversationParameters(
        is_group=True,
        channel_data=CHANNEL_ID,
        activity=Activity(type=ActivityTypes.message,
        text='Hello World!'),
        bot=ChannelAccount(id=BOT_ID),
        tenant_id=TENANT_ID)
response = await connectorClient.conversations.create_conversation(parameters)
response.send()

但它没有用,我尝试了很多不同的方法,其中 none 也有效,总是错误是:

Traceback (most recent call last):
File "/home/farid/works/16.proactive-messages/venv/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 418, in start
resp = await task
File "/home/farid/works/16.proactive-messages/venv/lib/python3.7/site-packages/aiohttp/web_app.py", line 458, in _handle
resp = await handler(request)
File "/home/farid/works/16.proactive-messages/app.py", line 103, in notify
raise exception
File "/home/farid/works/16.proactive-messages/app.py", line 100, in notify
await _send_proactive_message()
File "/home/farid/works/16.proactive-messages/app.py", line 152, in _send_proactive_message
response = await connectorClient.conversations.create_conversation(parameters)
File "/home/farid/works/16.proactive-messages/venv/lib/python3.7/site-packages/botframework/connector/aio/operations_async/_conversations_operations_async.py", line 176, in create_conversation
raise models.ErrorResponseException(self._deserialize, response)
botbuilder.schema._models_py3.ErrorResponseException: (BadSyntax) Incorrect conversation creation parameters

我不知道我的问题是什么!

有一个非常 的好机会我完全偏离了基地,因为我以前从未尝试过阅读 Python 之前(我是 C# /node guy),但看起来,在您的 ConversationParameters 中,您缺少 "Recipient" 详细信息(您有 "From",即指定的 Bot),通常需要为这个。

偶尔这有助于...

这是使用 sdk 3 的 C# 示例代码

        var userId = userOrChannelId.Trim();
        var botId = context.Activity.Recipient.Id;
        var botName = context.Activity.Recipient.Name;

        var channelData = context.Activity.GetChannelData<TeamsChannelData>();
        var connectorClient = new ConnectorClient(new Uri(context.Activity.ServiceUrl));
        var parameters = new ConversationParameters
        {
            Bot = new ChannelAccount(botId, botName),
            Members = !isChannelMessage ? new ChannelAccount[] { new ChannelAccount(userId) } : null,
            ChannelData = new TeamsChannelData
            {
                Tenant = channelData.Tenant,
                Channel = isChannelMessage ? new ChannelInfo(userId) : null,
                Notification = new NotificationInfo() { Alert = true }
            },
            IsGroup = isChannelMessage
        };


            var conversationResource = await connectorClient.Conversations.CreateConversationAsync(parameters);
            var replyMessage = Activity.CreateMessageActivity();
            replyMessage.From = new ChannelAccount(botId, botName);
            replyMessage.Conversation = new ConversationAccount(id: conversationResource.Id.ToString());
            replyMessage.ChannelData = new TeamsChannelData() { Notification = new NotificationInfo(true) };
            replyMessage.Text = messageText;
            if (attachment != null)
                replyMessage.Attachments.Add(attachment);

                var resourceResponse = await connectorClient.Conversations.SendToConversationAsync(conversationResource.Id, (Activity)replyMessage);

好的,昨晚微软添加了一个新的 python 示例解决了这个问题: https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/python/58.teams-start-thread-in-channel