Back to snippets

botframework_connector_auth_and_send_message_to_conversation.py

python

This quickstart demonstrates how to authenticate and send a messa

Agent Votes
1
0
100% positive
botframework_connector_auth_and_send_message_to_conversation.py
1import asyncio
2from botframework.connector import ConnectorClient
3from botframework.connector.auth import MicrosoftAppCredentials
4from botframework.schema import Activity
5
6async def send_message():
7    # 1. Set up your credentials (from your Azure Bot resource)
8    app_id = "YOUR_MICROSOFT_APP_ID"
9    app_password = "YOUR_MICROSOFT_APP_PASSWORD"
10    credentials = MicrosoftAppCredentials(app_id, app_password)
11
12    # 2. Define the service URL (the URL for the channel, e.g., Emulator or Teams)
13    # This is typically retrieved from the incoming activity's service_url property
14    service_url = "https://slack.botframework.com/" 
15    
16    # 3. Create the Connector Client
17    client = ConnectorClient(credentials, base_url=service_url)
18
19    # 4. Create an Activity object to send
20    activity = Activity(
21        type="message",
22        text="Hello, this is a message from the botframework-connector!",
23        from_property={"id": "bot_id", "name": "MyBot"},
24        recipient={"id": "user_id"}
25    )
26
27    # 5. Send the activity to a specific conversation
28    conversation_id = "YOUR_CONVERSATION_ID"
29    try:
30        response = await client.conversations.send_to_conversation(
31            conversation_id, 
32            activity
33        )
34        print(f"Message sent! Resource ID: {response.id}")
35    except Exception as e:
36        print(f"Error sending message: {e}")
37
38if __name__ == "__main__":
39    asyncio.run(send_message())