Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Syntax to start a new conversation in a group channel? #162

Closed
steven-liu opened this issue Oct 31, 2018 · 4 comments
Closed

Comments

@steven-liu
Copy link

steven-liu commented Oct 31, 2018

I'm trying to write a bot and am running into errors when I try to create a group conversation in a channel. I roughly followed the patterns documented here, but unfortunately it seems like the sample code is for a direct conversation, which is not what I want. Below is some sample code:

ChannelAccount botAccount = new ChannelAccount(Resources.Strings.BotAppId, "Help Bot");
TeamsChannelData channelData = activity.GetChannelData<TeamsChannelData>();

// construct parameters
ConversationParameters conversationParameters = new ConversationParameters
{
	Bot = botAccount,
	IsGroup = true,
	ChannelData = channelData,
	// Members = ... do I need to fill this field out? and with what?
};

ConversationResourceResponse conversationResourceResponse = await connectorClient.Conversations.CreateConversationAsync(conversationParameters);

// construct message
IMessageActivity message = Activity.CreateMessageActivity();
message.From = botAccount;
message.Conversation = new ConversationAccount(id: conversationResourceResponse.Id.ToString());
message.Text = "Hello World";

await connectorClient.Conversations.SendToConversationAsync((Activity)message);

Am I missing parameters or using a wrong pattern here? Been trying to find some documentation on this online but it seems pretty sparse, so any help would be appreciated.

@Wajeed-msft
Copy link
Contributor

For channel conversation, you don't need to fill Members field. You need to set the channel Id in which you are creating the conversation.

var parameters = new ConversationParameters
            {
                Bot = botAccount,
                ChannelData = new TeamsChannelData
                {
                    Channel = new ChannelInfo(channelId) ,
                },
                IsGroup = true
            };

Could you please try this and let us know if you are facing any issue?

@steven-liu
Copy link
Author

Tried the above and I'm still getting Message: Incorrect conversation creation parameters. For the channelId, I'm using channelData.Channel.Id from the activity that triggers this bot's method call.

@Wajeed-msft
Copy link
Contributor

Wajeed-msft commented Nov 1, 2018

@steven-liu - Here is the complete code for sending proactive message in a channel

        var channelData = context.Activity.GetChannelData<TeamsChannelData>();
        var message = Activity.CreateMessageActivity();
        message.Text = "Hello World";

        var conversationParameters = new ConversationParameters
        {
             IsGroup = true,
             ChannelData = new TeamsChannelData
              {
                 Channel = new ChannelInfo(channelData.Channel.Id),
             },
             Activity = (Activity) message
        };

        var connectorClient = new ConnectorClient(new Uri(activity.ServiceUrl));
        var response = await connectorClient.Conversations.CreateConversationAsync(conversationParameters);

Note: If you are calling this outside Bot's controller code then you need to call TrustServiceUrl on serviceUrl as shown here:

        MicrosoftAppCredentials.TrustServiceUrl(serviceUrl, DateTime.MaxValue);
        var connectorClient = new ConnectorClient(new Uri(serviceUrl));

@hardikverma24
Copy link

@Wajeed-msft can you please provide the code for javascript on how to create a conversation in a channel

@OfficeDev OfficeDev locked as resolved and limited conversation to collaborators Sep 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants