简体   繁体   English

MS 团队 bot JS - 创建对话

[英]MS teams bot JS - create conversation

I need to proactivly create conversation on channel in msteams.我需要在 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.更准确地说,我有 facebook、whatsapp 和 web 小部件。 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.例如,用户从 facebook 写信,我通过 fb webhook 收到消息,我需要创建新线程(在频道内有回复的块),只有在此之后,才会有人使用线程块在频道中回答,我会收到消息。 As i understood i need to bootstrap conversation object and use adapter.createConversation({ ...convFields }), but for example i don't have serviceUrl.据我所知,我需要引导对话对象并使用adapter.createConversation({ ...convFields }),但例如我没有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.对于 1:1 聊天,可以与用户创建新对话,但是您的机器人需要知道用户的 ID 才能这样做。 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有关该方法的更多详细信息,请参阅本文: 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 .即使它是在 C# 中,您可能会发现此示例很有帮助,因为它演示了将主动消息发送到每种类型的目的地所需的最少信息(跨语言相同): 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不和谐机器人对话 - Discord bot conversation 如何使用 Bot Framework Connection 从 MS Teams 组织中获取所有成员的列表 - How to get a list of all the members from MS Teams organization using Bot Framework Connection MS Teams Bot——是否可以从任务/提交活动而不是从 composeExtension/submitAction 活动调用 botMessagePreview? - MS Teams Bot -- Is it possible to invoke a botMessagePreview from a task/submit activity as opposed to from a composeExtension/submitAction activity? 标记机器人消息重要或机器人提及团队 - Marking bot Message Important or bot to mention teams Discord.Js Bot 为用户创建邀请 - Discord.Js Bot to create invites for users 移动设备上的 MS Teams 选项卡中的身份验证 - Authentication in MS Teams tab on mobile 为我的discord.js Bot创建一个等级列表 - Create a ranklist for my discord.js Bot Bot Connector - Direct Line API 交叉对话? - Bot Connector - Direct Line API cross conversation? 在 MS Lync 中与多个用户开始对话 - Start conversation with mutiple users in MS Lync 有时我的机器人无法继续流动。 一张卡继续循环。(直接线特定问题,而不是在 MS 团队中上升) - Some times my bot can't flow continue. one card continues looping.(directline specific problem and not rise in MS-Teams)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM