将主动消息发送到团队中的频道 [英] Sending proactive messages to a channel in Teams

查看:123
本文介绍了将主动消息发送到团队中的频道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以

我进行了广泛搜索,阅读了有关该主题的所有内容,但我仍然失败了.我已经设法向用户发送了主动消息,在团队中回复了主题,等等.但是我无法在团队频道中发送主动消息(创建新帖子).

是否有可用的示例(我找不到任何示例)?用于NodeJS的MS Docs似乎显示了一个向团队中的每个用户发送消息的示例,而不是渠道本身的消息.

我研究了源代码,并且channelDatabotFrameworkAdapter.js内部被硬编码为null,这只会增加混乱.

因此,基本代码是:

const builder = require('botbuilder');
const adapter = new builder.BotFrameworkAdapter({
    appId: 'XXX',
    appPassword: 'YYY'
});

const conversation = {
  channelData: {
    //I have all this (saved from event when bot joined the Team)
  },
  ...
  // WHAT THIS OBJECT NEEDS TO BE TO SEND A SIMPLE "HELLO" TO A CHANNEL?
  // I have all the d
};

adapter.createConversation(conversation, async (turnContext) => {
  turnContext.sendActivity('HELLO'); //This may or may not be needed?
});

有人用Node完成此操作吗?如果是这样,谁能给我展示一个有效的示例(带有正确构造的conversation对象)?

*编辑*

正如希尔顿在下面的答案中所建议的,我尝试直接使用ConnectorClient,但是它返回的资源不可用(/v3/conversations)

这是我正在使用的代码(实际上只是为了发送演示消息而已):

const path = require('path');
const { ConnectorClient, MicrosoftAppCredentials } = require('botframework-connector');

const ENV_FILE = path.join(__dirname, '.env');
require('dotenv').config({ path: ENV_FILE });

const serviceUrl = 'https://smba.trafficmanager.net/emea/';

async function sendToChannel() {
    MicrosoftAppCredentials.trustServiceUrl(serviceUrl);

    var credentials = new MicrosoftAppCredentials(process.env.MicrosoftAppId, process.env.MicrosoftAppPassword);
    var client = new ConnectorClient(credentials, { baseUri: serviceUrl });

    var conversationResponse = await client.conversations.createConversation({
        bot: {
            id: process.env.MicrosoftAppId,
            name: process.env.BotName
        },
        isGroup: true,
        conversationType: "channel",
        id: "19:XXX@thread.tacv2"
    });

    var acivityResponse = await client.conversations.sendToConversation(conversationResponse.id, {
        type: 'message',
        from: { id: process.env.MicrosoftAppId },
        text: 'This a message from Bot Connector Client (NodeJS)'
    });

}

sendToChannel();

我在做什么错了?

解决方案

好的,这就是我的工作方式.我将其张贴在这里以供将来参考.

免责声明 :我仍然不知道如何像最初问题中所述的那样将其与botbuilder一起使用,并且此答案将使用ConnectorClient ,这是可以接受的(至少对我而言).根据希尔顿的指示和我之前看到的GitHub问题( https://github.com/OfficeDev/BotBuilder-MicrosoftTeams/issues/162#issuecomment-434978847 ),我终于使它工作了. MS Documentation并不是那么有用,因为它们始终使用context变量,当您的Bot响应消息或活动时可以使用该变量,并且在Bot运行时在内部保留这些上下文的记录.但是,如果您的Bot由于某种原因而重新启动,或者您想要将数据存储在数据库中以供以后使用,则可以采用这种方式.

因此,代码(NodeJS):

const path = require('path');
const { ConnectorClient, MicrosoftAppCredentials } = require('botframework-connector');

const ENV_FILE = path.join(__dirname, '.env');
require('dotenv').config({ path: ENV_FILE });

const serviceUrl = 'https://smba.trafficmanager.net/emea/';

async function sendToChannel() {
    MicrosoftAppCredentials.trustServiceUrl(serviceUrl);

    var credentials = new MicrosoftAppCredentials(process.env.MicrosoftAppId, process.env.MicrosoftAppPassword);
    var client = new ConnectorClient(credentials, { baseUri: serviceUrl });

    var conversationResponse = await client.conversations.createConversation({
        bot: {
            id: process.env.MicrosoftAppId,
            name: process.env.BotName
        },
        isGroup: true,
        conversationType: "channel",
        channelData: {
            channel: { id: "19:XXX@thread.tacv2" }
        },
        activity: {
            type: 'message',
            text: 'This a message from Bot Connector Client (NodeJS)'
        }
    });

}

sendToChannel();

注意 :正如希尔顿指出,serviceUrl还需要从您的数据库中加载,以及频道ID.当您将Bot和您还需要的channelId一起添加到团队/渠道/组时,该活动在您最初收到的活动中可用,并且您需要将其存储以备将来参考(不要像示例中那样对它们进行硬编码) ).

因此,没有单独的createConversationsendActivity,全部都在一个呼叫中.

感谢希尔顿为您提供的时间,以及我的手的模糊的图像:MS Docs:)

希望这对其他人有帮助

So,

I searched far and wide, read everything I could find on the topic and I am still failing at this. I have managed to send proactive message to user, reply to a topic in team, etc. but I am unable to send a proactive message (create new post) in a team channel.

Is there an available example (I was unable to find any)? MS Docs for NodeJS seem to show an example of messaging each user in the team, but not the channel itself.

I explored the source code, and channelData is hardcoded to null inside botFrameworkAdapter.js, which only adds to the confusion.

So, basic code is:

const builder = require('botbuilder');
const adapter = new builder.BotFrameworkAdapter({
    appId: 'XXX',
    appPassword: 'YYY'
});

const conversation = {
  channelData: {
    //I have all this (saved from event when bot joined the Team)
  },
  ...
  // WHAT THIS OBJECT NEEDS TO BE TO SEND A SIMPLE "HELLO" TO A CHANNEL?
  // I have all the d
};

adapter.createConversation(conversation, async (turnContext) => {
  turnContext.sendActivity('HELLO'); //This may or may not be needed?
});

Has anyone done this with Node ? If so, can anyone show me a working example (with properly constructed conversation object)?

* EDIT *

As Hilton suggested in the answer below, I tried using ConnectorClient directly, but it returns resource unavailable (/v3/conversations)

Here is the code that I am using (it's literally only that, just trying to send demo message):

const path = require('path');
const { ConnectorClient, MicrosoftAppCredentials } = require('botframework-connector');

const ENV_FILE = path.join(__dirname, '.env');
require('dotenv').config({ path: ENV_FILE });

const serviceUrl = 'https://smba.trafficmanager.net/emea/';

async function sendToChannel() {
    MicrosoftAppCredentials.trustServiceUrl(serviceUrl);

    var credentials = new MicrosoftAppCredentials(process.env.MicrosoftAppId, process.env.MicrosoftAppPassword);
    var client = new ConnectorClient(credentials, { baseUri: serviceUrl });

    var conversationResponse = await client.conversations.createConversation({
        bot: {
            id: process.env.MicrosoftAppId,
            name: process.env.BotName
        },
        isGroup: true,
        conversationType: "channel",
        id: "19:XXX@thread.tacv2"
    });

    var acivityResponse = await client.conversations.sendToConversation(conversationResponse.id, {
        type: 'message',
        from: { id: process.env.MicrosoftAppId },
        text: 'This a message from Bot Connector Client (NodeJS)'
    });

}

sendToChannel();

What am I doing wrong?

解决方案

Okay, so, this is how I made it work. I am posting it here for future reference.

DISCLAIMER: I still have no idea how to use it with botbuilder as was asked in my initial question, and this answer is going to use ConnectorClient, which is acceptable (for me, at least). Based on Hilton's directions and a GitHub issue that I saw earlier ( https://github.com/OfficeDev/BotBuilder-MicrosoftTeams/issues/162#issuecomment-434978847 ), I made it work finally. MS Documentation is not that helpful, since they always use context variable which is available when your Bot is responding to a message or activity, and they keep a record of these contexts internally while the Bot is running. However, if your Bot is restarted for some reason or you want to store your data in your database to be used later, this is the way to go.

So, the code (NodeJS):

const path = require('path');
const { ConnectorClient, MicrosoftAppCredentials } = require('botframework-connector');

const ENV_FILE = path.join(__dirname, '.env');
require('dotenv').config({ path: ENV_FILE });

const serviceUrl = 'https://smba.trafficmanager.net/emea/';

async function sendToChannel() {
    MicrosoftAppCredentials.trustServiceUrl(serviceUrl);

    var credentials = new MicrosoftAppCredentials(process.env.MicrosoftAppId, process.env.MicrosoftAppPassword);
    var client = new ConnectorClient(credentials, { baseUri: serviceUrl });

    var conversationResponse = await client.conversations.createConversation({
        bot: {
            id: process.env.MicrosoftAppId,
            name: process.env.BotName
        },
        isGroup: true,
        conversationType: "channel",
        channelData: {
            channel: { id: "19:XXX@thread.tacv2" }
        },
        activity: {
            type: 'message',
            text: 'This a message from Bot Connector Client (NodeJS)'
        }
    });

}

sendToChannel();

NOTE: As Hilton pointed out, serviceUrl also needs to be loaded from your database, along with the channel id. It is available inside the activity which you receive initially when your Bot is added to team / channel / group along with channelId which you will also need, and you need to store those for future reference (do not hardcode them like in the example).

So, there is no separate createConversation and sendActivity, it's all in one call.

Thanks Hilton for your time, and a blurred image of my hand to MS Docs :)

Hope this helps someone else

这篇关于将主动消息发送到团队中的频道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆