MS团队漫游器JS-创建对话

时间:2020-02-05 21:53:20

标签: javascript node.js botframework microsoft-teams

我需要在msteams频道中主动创建对话。因此,我查找了示例,但没有找到真正主动创建对话的任何示例。所有示例首先包括处理消息,以及使用上下文创建对话。 更准确地说,我有facebook,whatsapp和网络小部件。例如,用户从Facebook写信,我通过fb webhook收到消息,我需要创建新线程(在channel内有回复的代码块),只有在那之后,才会有人在线程中使用线程块回答问题,我会得到消息。 据我了解,我需要引导对话对象并使用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];

1 个答案:

答案 0 :(得分:0)

为了防止(或至少阻止)垃圾邮件,您的机器人只能将主动消息发送到已经安装了该消息的频道或群聊。在这种情况下,您将需要存储来自sessionUpdateUpdate成员Fyre事件的必要信息。

对于1:1聊天,可以与用户建立新的对话,但是您的漫游器需要知道用户的ID才能这样做。通常,这是通过检索机器人安装所在的群聊或团队的花名册来实现的。

从本质上讲,不可能发送完全主动的消息-僵尸程序需要安装,并且/或者需要一些有关它以前在何处发送消息的信息。

假设您可以获取正确的权限,则可以主动安装您的机器人。有关该方法的更多详细信息,请参见本文:https://docs.microsoft.com/en-us/graph/teams-proactive-messaging

即使它是C#语言,您也可能会发现此示例很有帮助,因为它演示了将主动消息发送到每种类型的目的地(在各种语言中都是相同的)所需的最少信息量:https://github.com/clearab/teamsProactiveMessaging

相关文件如下:

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);
    }
}