How to send an event to our bot
Hi,
I have a little question : i want to send an event to my bot using my website. For exemple in my website i can add some Reel life event and when i add 1 event i want to notify my bot that a reel event is added into my Data base by sending him a Activity.Type.Trigger (or Event ).
Is-it possible ? Do you have so docs, example for that ? Can i use HTTP to notify my bot ?
I tryed to use Azure Function for that but seems not working for me ... I did maybe a mistake but i don't know where because i don't found a lot about Azure Function and BotFramework.
Thanks a lot to read me.
DirectLine is a method for talking to your bot directly through the same authenticated bot web service endpoint - you might consider that as one option. Otherwise, you might just have some back channel web service url for posting your events, though you would have to secure that separately.
Looks Activity.Type.Trigger is deprecated you may have to select event , I have tried the proactive bot documentation but Azure function is throwing following error and I don’t know how to resolve this error.
Function ($QueueTriggerProactiveBot) Error: Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.QueueTriggerProactiveBot'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'message' to type BotMessage&. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. config.UseServiceBus(), config.UseTimers(), etc.).
Right, i chose to add an ApiController (on the same project than my bot) that will be used to send a proactive message to a list of user on Facebook (Messenger). Now i can send a proactive message when i want by just using this API Controller.
But i had some experimental issue when i was coding this ApiController. One of this issue was an Exception that get thrown : " InternalServerError: Authorization for Microsoft App ID failed with status code Forbidden". I don't know why i get this but after searching on Internet i found an alternative. I had to add this in my code :
MicrosoftAppCredentials.TrustServiceUrl(url);
And change this :
var connector = new ConnectorClient(new Uri(url));
into this :
var connector = new ConnectorClient(new Uri(url), ConfigurationManager.AppSettings["MicrosoftAppId"], ConfigurationManager.AppSettings["MicrosoftAppPassword"]);
`
Do you know why ? Maybe is this only for Facebook ? Because in this this example, they didn't use it.
Here is my code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Threading.Tasks;
using Microsoft.Bot.Connector;
using Microsoft.Rest;
using Microsoft.IdentityModel.Protocols;
using System.Configuration;
namespace CitaBot.Facebook.Controllers
{
public class EventController : ApiController
{
[HttpGet]
[Route("api/SendEvent")]
public async Task<HttpResponseMessage> SendMessage()
{
var resp = new HttpResponseMessage(HttpStatusCode.OK);
try
{
await SendProactiveMessage("https://facebook.botframework.com", new ChannelAccount("FACEBOOKUSER_IDe", "FACEBOOKUSER_NAME"), "Testing");
}
catch (Exception ex)
{
resp = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
return resp;
}
private async Task SendProactiveMessage(string url, ChannelAccount recipient, string topic)
{
MicrosoftAppCredentials.TrustServiceUrl(url);
ChannelAccount botAccount = new ChannelAccount("FACEBOOKBOT_ID", "FACEBOOKBOT_NAME");
var connector = new ConnectorClient(new Uri(url), ConfigurationManager.AppSettings["MicrosoftAppId"], ConfigurationManager.AppSettings["MicrosoftAppPassword"]);
var newMessage = Activity.CreateMessageActivity();
newMessage.Type = ActivityTypes.Message;
newMessage.From = botAccount;
newMessage.Recipient = recipient;
newMessage.Text = topic;
newMessage.ChannelId = "facebook";
var conversation = await connector.Conversations.CreateDirectConversationAsync(botAccount, recipient);
newMessage.Conversation = new ConversationAccount(id: conversation.Id);
await connector.Conversations.SendToConversationAsync((Activity)newMessage);
}
}
}
My code is working but i want to understand why i have to add/edit the lines above. I tryed also to add [BotAuthentification] attribute to my ApiController.
@JeevaJose I had a similar issue yesterday i posted on StackOverFlow and they someone helped me to resolve this.
Just After that i had another issue, and i got help too here. If you still have the problem give me a feedback.
Thanks for your help. I will try azure function and let you know later.
Even I was using the ApiController to send the proactive message to webchat it was working that message directly go to webchat. What I understood is we can’t send the proactive msg from anywhere and it will work only from the hosted place. But you can try from local then local to local.
If you are using the [BotAuthentification] attribute you need to Authorize to the Url. I am doing some more experiment.
I am able to compile my azure function without any errors. The function is calling correctly but I could not see any output and don’t know what is happening on the behind.
I can confirm nothing is hitting on
public async Task
I would like to know would you be able to run the function.
Do you have a Json file in your project ?
Are u using Azure function from the Azure portal or from Visual studio ?
I am using from Azure portal. Could you please confirm are you able to execute the function and it is working as required.
I confirm that i'm able to execute the function and each 1 minute (defined for my timer function) the event is triggered and the bot is able to receive ActivityTypes.Event or ActivityTypes.Trigger.
You have to add an Bot Framework Output in your azure function. For that you have to go into Azure Function in Azure Portal (follow this steps) :
http://i.imgur.com/vwGviFs.png
Set this value : (Sender Id can be what you want)
http://i.imgur.com/LF4nHfB.png
My code return a object of my BotMessage Class to my output (My bot) but you have to say which bot has to receive this for that you have to set the Direct line Key secret, you got on BotFramework.com , into the AppSetting :
http://i.imgur.com/uba3eSa.png
To add a key into App settings:
http://i.imgur.com/rfWHp6t.png
You should have something like that :
http://i.imgur.com/xOOUkaa.png
I hope it will help you !
Thank you very much for your support.
I have already implemented the above functionality looks nothing is happening. In my C# code I am using ResumptionCookie and pass it back to webchat.
I have some question here :
Are you using Directline approach? what is BotMeesage.Source? sender Id is our botId?
I know webchat is a direct line and once the azure function trigger send back to my webchat.
I will try your solution and let you know the status.
That's weird...
Of what I understood :
My bot is on Facebook, then i use Facebook Channel but directline is used here to communicate between Azure Function and my bot. BotMessage.Source can be whatever you want. I just created a class named BotMessage but it could be a class named MySuperClass it would have changed anynothing. That's just the class which will be serialized into Json and will be passed back to Bot.
Sender Id can be whatever you want too, it will be the Activity.From.Id property when you will get the activity on your bot. It will identify the sender ( Azure Function ) of the activity.
Hope it will help you.