GLP
GLP

Reputation: 3685

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. I am trying to send messages via the Direct Line service from a python app. 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

I got an error about access_token. Can anyone help what I am missing?

Thanks

Upvotes: 2

Views: 796

Answers (1)

Manu Jo Varghese
Manu Jo Varghese

Reputation: 400

You can make use of the request library to send direct message to azure bot via direct line like this :

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. 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

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

)

Upvotes: 2

Related Questions