neil4real
neil4real

Reputation: 51

Microsoft Teams Python Botbuilder Proactive Messaging

What was simple in Webex now seems fairly complicated in the Microsoft world. What I specifically am looking to do is:

  1. Create a bot in the Azure Bot Framework (Done)
  2. Identify recipient ids using the botbuilder sdk using the recipient email
  3. Use Botframework-Connector to individually identify these recipients, create new conversations, and proactively message them

This is what I have been using so far 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!'))

Is this even close to the right approach?

Upvotes: 0

Views: 971

Answers (2)

oscarnogueira
oscarnogueira

Reputation: 21

This was the easiest way I found to make it work.

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)

It is easy to infer all the uppercase variables.

The <<RECIPIENT_ID>> is the MS Teams ID of the user you want to send the message.

Hope this helps.

MSFT does not provide good examples in Python.

Upvotes: 2

Hilton Giesenow
Hilton Giesenow

Reputation: 10844

With a cursory glance it looks ok (I don't work in Python so can't actually run the example). One thing that does look missing in the TrustServiceUrl call. See here for details.

Upvotes: 1

Related Questions