Botframework 通过bot框架在facebook上发布赞助消息

Botframework 通过bot框架在facebook上发布赞助消息,botframework,Botframework,如果用户不向我发送消息,我如何向用户发送消息?例如,CNN机器人每天早上自己发送消息。如何在bot框架中做到这一点?请参阅 事实上,您不需要严格地首先接收来自用户的消息,但是手动寻址可能容易出错(您必须知道用户和bot的频道帐户、服务URL等),反过来(根据@thegaram的消息),这只适用于某些频道。例如,Skype要求用户在机器人向用户发送消息之前联系机器人 一旦与您联系,您可以在用户与您联系后存储其channelAccount数据,并使用该数据向他们发送主动消息。例如,如果用户订阅了一段

如果用户不向我发送消息,我如何向用户发送消息?例如,CNN机器人每天早上自己发送消息。如何在bot框架中做到这一点?

请参阅

事实上,您不需要严格地首先接收来自用户的消息,但是手动寻址可能容易出错(您必须知道用户和bot的频道帐户、服务URL等)

,反过来(根据@thegaram的消息),这只适用于某些频道。例如,Skype要求用户在机器人向用户发送消息之前联系机器人

一旦与您联系,您可以在用户与您联系后存储其channelAccount数据,并使用该数据向他们发送主动消息。例如,如果用户订阅了一段时间来收听特定球队的运动成绩


当然,Bot框架(以及大多数渠道)的政策禁止任何形式的未经请求的垃圾邮件。

是的,您可以这样做。我们称之为来自机器人的问候。我已经完成了,并与您共享了一个示例代码

在messageController或bot中使用的第一个对话框中编写此代码

if (activity.Text == null)
            {
                ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
                Activity isActivityTyping = activity.CreateReply();
                isActivityTyping.Type = ActivityTypes.Typing;
                await connector.Conversations.ReplyToActivityAsync(isActivityTyping);
                await Conversation.SendAsync(activity, () => new Dialogs.GreetDialog());

            }
完成此代码后,您需要创建一个对话框GreetDialog。以下是cs文件代码,供您参考

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;

namespace GPP.Bot.Dialogs
{
    [Serializable]
    internal class GreetDialog : IDialog<object>
    {
        public async Task StartAsync(IDialogContext context)
        {
           context.Wait(Greeting);
        }
        private async Task Greeting(IDialogContext context, IAwaitable<IMessageActivity> argument)
        {
            var message = await argument;
            if (string.IsNullOrEmpty(message.Text))
            {

                // Hero Card
                var cardMsg = context.MakeMessage();
                var attachment = BotWelcomeCard("Hello, I am a bot. Right now I am on training and in a prototype state", "");
                cardMsg.Attachments.Add(attachment);
                await context.PostAsync(cardMsg);
                context.Call<object>(new ActionDialog(), AfterGreetingDialogCompleted);
            }
            else
            {             
               context.Call<object>(new ActionDialog(), AfterGreetingDialogCompleted);
            }
        }
        private static Attachment BotWelcomeCard(string responseFromQNAMaker, string userQuery)
        {
            var heroCard = new HeroCard
            {
                Title = userQuery,
                Subtitle = "",
                Text = responseFromQNAMaker,
                Images = new List<CardImage> { new CardImage("https://i2.wp.com/lawyerist.com/lawyerist/wp-content/uploads/2016/08/docubot.gif?fit=322%2C294&ssl=1") },
                Buttons = new List<CardAction> { new CardAction(ActionTypes.ImBack, "Show Menu", value: "Show Bot Menu") }
            };

            return heroCard.ToAttachment();
        }
        private async Task AfterGreetingDialogCompleted(IDialogContext context, IAwaitable<object> result)
        {
            context.Done<object>(new object());
        }
    }
} 
使用系统;
使用System.Collections.Generic;
使用System.Threading.Tasks;
使用Microsoft.Bot.Builder.Dialogs;
使用Microsoft.Bot.Connector;
名称空间GPP.Bot.Dialogs
{
[可序列化]
内部类GreetDialog:IDialog
{
公共异步任务StartAsync(IDialogContext上下文)
{
上下文。等待(问候);
}
专用异步任务问候语(IDialogContext上下文,IAwaitable参数)
{
var message=等待参数;
if(string.IsNullOrEmpty(message.Text))
{
//英雄牌
var cardMsg=context.MakeMessage();
var attachment=BotWelcomeCard(“你好,我是一个机器人。现在我正在接受培训,处于原型状态。”);
cardMsg.Attachments.Add(附件);
wait context.PostAsync(cardMsg);
调用(新建ActionDialog(),在GreetingDialogCompleted之后);
}
其他的
{             
调用(新建ActionDialog(),在GreetingDialogCompleted之后);
}
}
私有静态附件BotWelcomeCard(字符串响应FromQnamaker、字符串用户查询)
{
var heroCard=新heroCard
{
Title=userQuery,
副标题=”,
Text=responseFromQNAMaker,
图像=新列表{新图像(“https://i2.wp.com/lawyerist.com/lawyerist/wp-content/uploads/2016/08/docubot.gif?fit=322%2C294&ssl=1") },
按钮=新列表{new CardAction(ActionTypes.ImBack,“Show Menu”,值:“Show Bot Menu”)}
};
返回heroCard.ToAttachment();
}
私有异步任务AfterGreetingDialogCompleted(IDialogContext上下文,IAwaitable结果)
{
context.Done(新对象());
}
}
} 
这是一个工作代码。如果你遇到蚂蚁问题,一定要告诉我。 ~z~干杯:)