【问题标题】:Microsoft Teams Python Botbuilder Proactive MessagingMicrosoft Teams Python Botbuilder 主动消息传递
【发布时间】:2020-06-24 04:24:30
【问题描述】:

Webex 中简单的东西现在在 Microsoft 世界中似乎相当复杂。 我特别想做的是:

  1. 在 Azure 机器人框架中创建机器人(完成)
  2. 使用 botbuilder sdk 使用收件人电子邮件识别收件人 ID
  3. 使用 Botframework-Connector 单独识别这些收件人、创建新对话并主动向他们发送消息

这是我迄今为止一直在使用的 https://pypi.org/project/botframework-connector/

from botbuilder.schema import *
from botframework.connector import ConnectorClient
from botframework.connector.auth import MicrosoftAppCredentials

APP_ID = 'azure_bot_app_id'
APP_PASSWORD = 'azure_bot_app_password'
SERVICE_URL = 'azure_bot_messaging_endpoint'
CHANNEL_ID = 'msteams'
BOT_ID = 'azure_bot_subscription_id'
RECIPIENT_ID = 'msteams_individual_user_id'

credentials = MicrosoftAppCredentials(APP_ID, APP_PASSWORD)
connector = ConnectorClient(credentials, base_url=SERVICE_URL)

conversation = connector.conversations.create_conversation(ConversationParameters(
            bot=ChannelAccount(id=BOT_ID),
            members=[ChannelAccount(id=RECIPIENT_ID)]))

connector.conversations.send_to_conversation(conversation.id, Activity(
            type=ActivityTypes.message,
            channel_id=CHANNEL_ID,
            recipient=ChannelAccount(id=RECIPIENT_ID),
            from_property=ChannelAccount(id=BOT_ID),
            text='Hello Person!'))

这是否接近正确的方法?

【问题讨论】:

  • 您所遵循的方法似乎正确发送proactive Message。你有什么问题吗?
  • 是的,我似乎缺少正确的参数来运行这个

标签: python azure botframework microsoft-graph-api microsoft-teams


【解决方案1】:

这是我发现的最简单的方法。

from config import DefaultConfig

from botframework.connector.connector_client import ConnectorClient
from botframework.connector.models import ConversationParameters
from botframework.connector.auth.microsoft_app_credentials import MicrosoftAppCredentials
from botbuilder.core import  MessageFactory
from botbuilder.schema import ChannelAccount

CONFIG = DefaultConfig()

recipient_id = <<RECIPIENT_ID>>

to = ChannelAccount(id=recipient_id)
bot_channel = ChannelAccount(id='msteams')

MicrosoftAppCredentials.trust_service_url(CONFIG.SERVICE_URL)
credentials = MicrosoftAppCredentials(CONFIG.APP_ID, CONFIG.APP_PASSWORD)
conn_client = ConnectorClient(credentials, CONFIG.SERVICE_URL)

message_activity = MessageFactory.text(f"Personal message from the Bot!");
 
conversation_params = ConversationParameters(members=[to], channel_data={ 'tenant': { 'id': CONFIG.TENANT_ID } })

conversation = conn_client.conversations.create_conversation(conversation_params)

conn_client.conversations.send_to_conversation(conversation.id, message_activity)

很容易推断出所有的大写变量。

&lt;&lt;RECIPIENT_ID&gt;&gt; 是您要发送消息的用户的 MS Teams ID。

希望这会有所帮助。

MSFT 在 Python 中没有提供好的示例。

【讨论】:

  • 我收到“提供的租户中的用户身份无效”。提供的示例中的 RECIPIENT_ID 是什么?谢谢
【解决方案2】:

粗略一看,它看起来还不错(我不在 Python 中工作,因此无法实际运行该示例)。在 TrustServiceUrl 调用中确实看起来缺少一件事。详情请见here

【讨论】:

  • 我没有看到最新版本的 TrustServiceUrl。还有希尔顿,你知道如何找到服务网址吗?我正在使用 Azure、azurewebsites.net,但无法连接或查找 service_url。
  • 我很确定 TrustServiceUrl 仍然是必需的。 ServiceUrl 对用户来说是唯一的,因此它是用户与(或首次安装)您的机器人交谈时获得的一部分。基本上,您可以从那里获得 RECIPIENT_ID
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多