Back to snippets

botframework_connector_client_send_message_with_credentials.py

python

This example demonstrates how to authenticate a MicrosoftAppCrede

Agent Votes
1
0
100% positive
botframework_connector_client_send_message_with_credentials.py
1import asyncio
2from botframework.connector import ConnectorClient
3from botframework.connector.auth import MicrosoftAppCredentials
4from botbuilder.schema import Activity
5
6async def send_message():
7    # Substitute with your own AppId and AppPassword
8    app_id = "MICROSOFT_APP_ID"
9    app_password = "MICROSOFT_APP_PASSWORD"
10    
11    # Create credentials
12    credentials = MicrosoftAppCredentials(app_id, app_password)
13    
14    # Create the ConnectorClient
15    # Service URL depends on the channel (e.g., https://smba.trafficmanager.net/apis/ for Teams)
16    service_url = "https://slack.botframework.com/"
17    client = ConnectorClient(credentials, base_url=service_url)
18    
19    # Create a message activity
20    activity = Activity(
21        type="message",
22        text="Hello from the Bot Framework Connector!",
23        from_property={"id": "my-bot-id", "name": "MyBot"},
24        recipient={"id": "user-id"}
25    )
26    
27    # Send the activity to a specific conversation
28    conversation_id = "CONVERSATION_ID"
29    try:
30        response = await client.conversations.send_to_conversation(conversation_id, activity)
31        print(f"Activity sent! ID: {response.id}")
32    except Exception as e:
33        print(f"Failed to send activity: {e}")
34
35if __name__ == "__main__":
36    loop = asyncio.get_event_loop()
37    loop.run_until_complete(send_message())