简体   繁体   English

如何通过直线服务向 python 中的 azure bot 发送消息

[英]how to send message to azure bot in python via direct line service

We recently deployed a Bot to Azure with type User assigned managed identity, so I only have app id, but not app password.我们最近部署了一个 Bot 到 Azure,类型为用户分配的托管身份,所以我只有应用程序 ID,但没有应用程序密码。 I am trying to send messages via the Direct Line service from a python app.我正在尝试从 python 应用程序通过直线服务发送消息。 Following are my code:以下是我的代码:

from botframework.connector.auth import MicrosoftAppCredentials
from botframework.connector.client import ConnectorClient

credentials = MicrosoftAppCredentials("YOUR_CLIENT_ID", "")
base_url="https://directline.botframework.com/"
client = ConnectorClient(credentials, base_url=base_url)

connectorClient = ConnectorClient(credentials, base_url=base_url)
client.conversations.send_to_conversation(conversation_id, message, service_url=base_url)

The python package I installed is botframework-connector==4.14.0我安装的python package是botframework-connector==4.14.0

I got an error about access_token.我收到有关 access_token 的错误。 Can anyone help what I am missing?谁能帮助我缺少什么?

Thanks谢谢

You can make use of the request library to send direct message to azure bot via direct line like this:您可以使用request库通过直线向 azure 机器人发送直接消息,如下所示:

import requests

# Replace YOUR_DIRECT_LINE_SECRET with your bot's Direct Line secret
direct_line_secret = 'YOUR_DIRECT_LINE_SECRET'

# Set the headers for the request
headers = {
    'Authorization': 'Bearer ' + direct_line_secret,
    'Content-Type': 'application/json'
}

# Set the parameters for the request
data = {
    'type': 'message',
    'from': {
        'id': 'YOUR_USER_ID'
    },
    'text': 'Hello, world!'
}

# Send the request to the Direct Line API
response = requests.post('https://directline.botframework.com/v3/directline/conversations/YOUR_CONVERSATION_ID/activities', json=data, headers=headers)

# Print the response status code
print(response.status_code)

If you want a user-assigned managed identity to authenticate the BotFrameworkConnectorClient without specifying an app password.如果您希望用户分配的托管标识在不指定应用程序密码的情况下对 BotFrameworkConnectorClient 进行身份验证。 Follow this:按照这个:

import os
from botframework.connector import ConnectorClient
from botframework.connector.auth import AzureActiveDirectoryAuthentication

# Replace YOUR_CLIENT_ID and YOUR_TENANT_ID with your own values
CLIENT_ID = 'YOUR_CLIENT_ID'
TENANT_ID = 'YOUR_TENANT_ID'

# Get the access token for the Bot Framework API
auth = AzureActiveDirectoryAuthentication(CLIENT_ID, TENANT_ID)
access_token = auth.get_access_token()

# Create a ConnectorClient
client = ConnectorClient(auth, base_url='https://directline.botframework.com/v3/directline')

# Set the parameters for the request
conversation_id = 'YOUR_CONVERSATION_ID'
activity = {
    'type': 'message',
    'from': {
        'id': 'YOUR_USER_ID'
    },
    'text': 'Hello, world!'
}

# Send the message
response = client.conversations.send_to_conversation(conversation_id, activity)

# Print the response
print(response)

or you could use ManagedIdentityCredential或者您可以使用ManagedIdentityCredential

import os
from azure.identity import ManagedIdentityCredential
from botframework.connector import ConnectorClient

# Set the resource and tenant ID
RESOURCE = "https://directline.botframework.com"
TENANT_ID = "YOUR_TENANT_ID"

# Create a ManagedIdentityCredential
credential = ManagedIdentityCredential(resource=RESOURCE, tenant_id=TENANT_ID)

# Create a ConnectorClient
client = ConnectorClient(credential, base_url='https://directline.botframework.com/v3/directline')

# Set the parameters for the request
conversation_id = 'YOUR_CONVERSATION_ID'
activity = {
    'type': 'message',
    'from': {
        'id': 'YOUR_USER_ID'
    },
    'text': 'Hello, world!'
}

# Send the message
response = client.conversations.send_to_conversation(conversation_id, activity)

# Print the response
print(response

) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM