PathSeeker
PathSeeker

Reputation: 101

Initial and send a message to a Microsoft Teams channel using Bot Framework SDK v4 for Python

I was trying to initial and send a proactive message to one Microsoft teams channel with the help of below example: https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/python/16.proactive-messages I added this code to the example in order to initiate a message:

connectorClient = await ADAPTER.create_connector_client(service_url=SERVICE_URL)
parameters = ConversationParameters(
        is_group=True,
        channel_data=CHANNEL_ID,
        activity=Activity(type=ActivityTypes.message,
        text='Hello World!'),
        bot=ChannelAccount(id=BOT_ID),
        tenant_id=TENANT_ID)
response = await connectorClient.conversations.create_conversation(parameters)
response.send()

But it didn't work, and I tried many different ways and none of them worked too, always the error is:

Traceback (most recent call last):
File "/home/farid/works/16.proactive-messages/venv/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 418, in start
resp = await task
File "/home/farid/works/16.proactive-messages/venv/lib/python3.7/site-packages/aiohttp/web_app.py", line 458, in _handle
resp = await handler(request)
File "/home/farid/works/16.proactive-messages/app.py", line 103, in notify
raise exception
File "/home/farid/works/16.proactive-messages/app.py", line 100, in notify
await _send_proactive_message()
File "/home/farid/works/16.proactive-messages/app.py", line 152, in _send_proactive_message
response = await connectorClient.conversations.create_conversation(parameters)
File "/home/farid/works/16.proactive-messages/venv/lib/python3.7/site-packages/botframework/connector/aio/operations_async/_conversations_operations_async.py", line 176, in create_conversation
raise models.ErrorResponseException(self._deserialize, response)
botbuilder.schema._models_py3.ErrorResponseException: (BadSyntax) Incorrect conversation creation parameters

I don't know what is my problem here!

Upvotes: 0

Views: 1696

Answers (3)

PathSeeker
PathSeeker

Reputation: 101

Ok, last night Microsoft added a new python sample which solved this issue: https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/python/58.teams-start-thread-in-channel

Upvotes: 1

Subhasish
Subhasish

Reputation: 509

Here is a sample code in C# using sdk 3

        var userId = userOrChannelId.Trim();
        var botId = context.Activity.Recipient.Id;
        var botName = context.Activity.Recipient.Name;

        var channelData = context.Activity.GetChannelData<TeamsChannelData>();
        var connectorClient = new ConnectorClient(new Uri(context.Activity.ServiceUrl));
        var parameters = new ConversationParameters
        {
            Bot = new ChannelAccount(botId, botName),
            Members = !isChannelMessage ? new ChannelAccount[] { new ChannelAccount(userId) } : null,
            ChannelData = new TeamsChannelData
            {
                Tenant = channelData.Tenant,
                Channel = isChannelMessage ? new ChannelInfo(userId) : null,
                Notification = new NotificationInfo() { Alert = true }
            },
            IsGroup = isChannelMessage
        };


            var conversationResource = await connectorClient.Conversations.CreateConversationAsync(parameters);
            var replyMessage = Activity.CreateMessageActivity();
            replyMessage.From = new ChannelAccount(botId, botName);
            replyMessage.Conversation = new ConversationAccount(id: conversationResource.Id.ToString());
            replyMessage.ChannelData = new TeamsChannelData() { Notification = new NotificationInfo(true) };
            replyMessage.Text = messageText;
            if (attachment != null)
                replyMessage.Attachments.Add(attachment);

                var resourceResponse = await connectorClient.Conversations.SendToConversationAsync(conversationResource.Id, (Activity)replyMessage);

Upvotes: 0

Hilton Giesenow
Hilton Giesenow

Reputation: 10804

There's a very good chance I'm totally off base as I've never tried, before now, to read Python before (I'm a C#/node guy), but it looks like, in your ConversationParameters, you're missing the "Recipient" details (you have the "From", i.e. your Bot, specified), which one does usually need to specify for this.

On the off-chance this helps...

Upvotes: 0

Related Questions