Mazen Abu Taweelih
Mazen Abu Taweelih

Reputation: 647

sponsored messages on facebook with bot framework

How can I send a message to the user without the user sending me a message? Like for example CNN bot is sending messages every day in the morning by itself. How can I do that in the bot framework?

Upvotes: 1

Views: 146

Answers (3)

Manoj Bhardwaj
Manoj Bhardwaj

Reputation: 260

Yes you can do that. We called it Greeting from Bot. I have done it and sharing a sample code with you.

Write this code in your messageController or first dialog used in 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());

            }

after this code you need to create a dialog GreetDialog. Below is the cs file code for your reference.

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

this is a working code. Do let me know in case you face ant issue. ~cheers :)

Upvotes: 0

Jim Lewallen
Jim Lewallen

Reputation: 1006

And in turn (per @thegaram's message), that only works for some channels. For example, Skype requires that the user contact the bot before the bot can message the user.

Once contacted, you can store the user's channelAccount data once they contact you and use that to send them proactive messages. For example if the user has subscribed to hear sports scores for a particular team over time.

Any sort of unsolicited spam messages of course are prohibited by the policies of the Bot Framework (and most of the channels).

Upvotes: 0

thegaram
thegaram

Reputation: 777

See this.

In fact, you do not strictly need to receive a message from the user first, but addressing manually can be error-prone (you have to know the user's and bot's channel account, the service URL, etc.)

Upvotes: 1

Related Questions