简体   繁体   中英

MS teams bot JS - create conversation

I need to proactivly create conversation on channel in msteams. So i looked for examples but didn't find any examples of realy proactive conversation creation. All examples include first of all handling message, and creating conversation using context. To be more accurate i have facebook, whatsapp, and web widget. Users write for example from facebook, i get message through fb webhook and i need to create new thread ( block with replies inside channel ) and only after that, someone who will answer in channel using thread block and i'll get message. As i understood i need to bootstrap conversation object and use adapter.createConversation({ ...convFields }), but for example i don't have serviceUrl.

// i am using adapter from examples
new BotFrameworkAdapter({
      appId: id,
      appPassword: password
});

// then i have something like that in examples

    const conversationParameters = {
      isGroup: true,
      channelData: {
        channel: {
          id: 'msteams'
        }
      },

      activity: 'dsada'
    };


    const connectorClient = this.adapter.createConnectorClient(
      context.activity.serviceUrl // i don't have context to get that serviceUrl, because i must do it first, not handle message and create thread after that.
    );
    const conversationResourceResponse = await connectorClient.conversations.createConversation(
      conversationParameters as any
    );
    const conversationReference = TurnContext.getConversationReference(
      context.activity // same here, i don't have context
    );
    conversationReference.conversation.id = conversationResourceResponse.id;
    return [conversationReference, conversationResourceResponse.activityId];

In order to prevent (or at least hinder) spam, your bot can only send proactive messages to channels or group chats where it is already installed. In that instance you'll need to store the necessary information from the conversationUpdate membersAdded event you'll receive.

For 1:1 chats, it is possible to create a new conversation with a user, however your bot needs to know the Id of the user in order to do so. Typically this is achieved by retrieving the roster of a group chat or team where your bot is installed.

Essentially, it isn't possible to send a completely proactive message - the bot needs to be installed and/or have some amount of information about where it is sending the message previously.

Assuming you can achieve the correct permissions, it is possible to proactively install your bot. See this article for more details on that approach: https://docs.microsoft.com/en-us/graph/teams-proactive-messaging

Even though it is in C#, you may find this sample helpful as it demonstrates the minimal amount of information required in order to send proactive messages to each type of destination (which is the same across languages): https://github.com/clearab/teamsProactiveMessaging .

The relevant file is below:

public class MessageSender : IMessageSender
{
    private ConnectorClient conClient;
    private string serviceUrl;

    public MessageSender(string serviceUrl, string id, string password)
    {
        MicrosoftAppCredentials.TrustServiceUrl(serviceUrl);
        conClient = new ConnectorClient(new Uri(serviceUrl), id, password);
    }

    public async Task<ResourceResponse> SendOneToOneMessage(string conversationId, Activity activity)
    {
        return await conClient.Conversations.SendToConversationAsync(conversationId, activity);

    }

    public async Task<ConversationResourceResponse> CreateOneToOneConversation(string userId, string tenantId)
    {
        var members = new List<ChannelAccount>()
        {
            new ChannelAccount
            {
                Id = userId
            }
        };

        ConversationParameters conParams = new ConversationParameters
        {
            Members = members,
            TenantId = tenantId
        };

        return await this.conClient.Conversations.CreateConversationAsync(conParams);

    }

    public async Task<ConversationResourceResponse> CreateAndSendChannelMessage(string channelId, Activity activity)
    {
        ConversationParameters conParams = new ConversationParameters
        {
            ChannelData = new TeamsChannelData
            {
                Channel = new ChannelInfo(channelId)
            },
            Activity = activity
        };

        ConversationResourceResponse response = await this.conClient.Conversations.CreateConversationAsync(conParams);

        return response;
    }

    public async Task<ResourceResponse> SendReplyToConversationThread(string threadId, Activity activity)
    {
        return await this.conClient.Conversations.SendToConversationAsync(threadId, activity);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM