added facebook messenger webhook
This commit is contained in:
parent
ff54c0a124
commit
aeb5ff8766
|
|
@ -72,5 +72,8 @@ namespace BotSharp.Core.Agents
|
|||
|
||||
[ForeignKey("AgentId")]
|
||||
public AgentMlConfig MlConfig { get; set; }
|
||||
|
||||
[ForeignKey("AgentId")]
|
||||
public List<AgentIntegration> Integrations { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
26
BotSharp.Core/Agents/AgentIntegration.cs
Normal file
26
BotSharp.Core/Agents/AgentIntegration.cs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
using EntityFrameworkCore.BootKit;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.Core.Agents
|
||||
{
|
||||
[Table("Bot_AgentIntegration")]
|
||||
public class AgentIntegration : DbRecord, IDbRecord
|
||||
{
|
||||
[Required]
|
||||
[StringLength(36)]
|
||||
public String AgentId { get; set; }
|
||||
|
||||
[MaxLength(64)]
|
||||
public String Platform { get; set; }
|
||||
|
||||
[MaxLength(64)]
|
||||
public String VerifyToken { get; set; }
|
||||
|
||||
[MaxLength(256)]
|
||||
public String AccessToken { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using BotSharp.Core.Agents;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
|
|
@ -11,5 +12,10 @@ namespace BotSharp.Core.Engines
|
|||
public String UserId { get; set; }
|
||||
public String ClientAccessToken { get; set; }
|
||||
public String DeveloperAccessToken { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Integration with other social media platform
|
||||
/// </summary>
|
||||
public List<AgentIntegration> Integrations { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,6 +44,11 @@ namespace BotSharp.Core.Engines
|
|||
result.MlConfig = agent.ToObject<AgentMlConfig>();
|
||||
result.MlConfig.MinConfidence = agent.MlMinConfidence;
|
||||
result.MlConfig.AgentId = agent.Id;
|
||||
if(agentHeader.Integrations != null)
|
||||
{
|
||||
agentHeader.Integrations.ForEach(x => x.AgentId = agent.Id);
|
||||
result.Integrations = agentHeader.Integrations;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,4 +4,8 @@
|
|||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Fasttext\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
12
BotSharp.RestApi/Integrations/AmazonAlexaController.cs
Normal file
12
BotSharp.RestApi/Integrations/AmazonAlexaController.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.RestApi.Integrations
|
||||
{
|
||||
[Route("v1/[controller]/[action]")]
|
||||
public class AmazonAlexaController : ControllerBase
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,117 @@
|
|||
using BotSharp.Core.Agents;
|
||||
using BotSharp.Core.Engines;
|
||||
using BotSharp.Core.Models;
|
||||
using BotSharp.RestApi.Integrations.FacebookMessenger;
|
||||
using DotNetToolkit;
|
||||
using EntityFrameworkCore.BootKit;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using RestSharp;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BotSharp.RestApi.Integrations
|
||||
{
|
||||
[Route("v1/[controller]")]
|
||||
public class FacebookMessengerController : ControllerBase
|
||||
{
|
||||
[HttpGet("{agentId}")]
|
||||
public ActionResult Verify([FromRoute] string agentId)
|
||||
{
|
||||
var mode = Request.Query.ContainsKey("hub.mode") ? Request.Query["hub.mode"].ToString() : String.Empty;
|
||||
var token = Request.Query.ContainsKey("hub.verify_token") ? Request.Query["hub.verify_token"].ToString() : String.Empty;
|
||||
var challenge = Request.Query.ContainsKey("hub.challenge") ? Request.Query["hub.challenge"].ToString() : String.Empty;
|
||||
|
||||
if (mode == "subscribe")
|
||||
{
|
||||
var dc = new DefaultDataContextLoader().GetDefaultDc();
|
||||
var config = dc.Table<AgentIntegration>().FirstOrDefault(x => x.AgentId == agentId && x.Platform == "Facebook Messenger");
|
||||
|
||||
return config.VerifyToken == token ? Ok(challenge) : Ok(agentId);
|
||||
}
|
||||
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
[HttpPost("{agentId}")]
|
||||
public async Task<ActionResult> CallbackAsync([FromRoute] string agentId)
|
||||
{
|
||||
WebhookEvent body;
|
||||
IWebhookMessageBody response = null;
|
||||
|
||||
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
|
||||
{
|
||||
var json = await reader.ReadToEndAsync();
|
||||
body = JsonConvert.DeserializeObject<WebhookEvent>(json);
|
||||
}
|
||||
|
||||
body.Entry.ForEach(entry =>
|
||||
{
|
||||
entry.Messaging.ForEach(msg =>
|
||||
{
|
||||
// received text message
|
||||
if (msg.Message.ContainsKey("text"))
|
||||
{
|
||||
OnTextMessaged(agentId, new WebhookMessage<WebhookTextMessage>
|
||||
{
|
||||
Sender = msg.Sender,
|
||||
Recipient = msg.Recipient,
|
||||
Timestamp = msg.Timestamp,
|
||||
Message = msg.Message.ToObject<WebhookTextMessage>()
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
private void OnTextMessaged(string agentId, WebhookMessage<WebhookTextMessage> message)
|
||||
{
|
||||
Console.WriteLine($"OnTextMessaged: {message.Message.Text}");
|
||||
|
||||
var rasa = new RasaAi();
|
||||
var agent = rasa.LoadAgent(agentId);
|
||||
rasa.AiConfig = new AIConfiguration(agent.ClientAccessToken, SupportedLanguage.English) { AgentId = agentId };
|
||||
rasa.AiConfig.SessionId = message.Sender.Id;
|
||||
var aiResponse = rasa.TextRequest(new AIRequest { Query = new String[] { message.Message.Text } });
|
||||
|
||||
var dc = new DefaultDataContextLoader().GetDefaultDc();
|
||||
var config = dc.Table<AgentIntegration>().FirstOrDefault(x => x.AgentId == agentId && x.Platform == "Facebook Messenger");
|
||||
|
||||
SendTextMessage(config.AccessToken, new WebhookMessage<WebhookTextMessage>
|
||||
{
|
||||
Recipient = message.Sender.ToObject<WebhookMessageRecipient>(),
|
||||
Message = new WebhookTextMessage
|
||||
{
|
||||
Text = aiResponse.Result.Fulfillment.Speech
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void SendTextMessage(string accessToken, WebhookMessage<WebhookTextMessage> body)
|
||||
{
|
||||
var client = new RestClient("https://graph.facebook.com");
|
||||
|
||||
var rest = new RestRequest("v2.6/me/messages", Method.POST);
|
||||
string json = JsonConvert.SerializeObject(body,
|
||||
new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
||||
NullValueHandling = NullValueHandling.Ignore
|
||||
});
|
||||
|
||||
rest.AddParameter("application/json", json, ParameterType.RequestBody);
|
||||
rest.AddQueryParameter("access_token", accessToken);
|
||||
|
||||
var response = client.Execute(rest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.RestApi.Integrations.FacebookMessenger
|
||||
{
|
||||
public interface IWebhookMessageBody
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.RestApi.Integrations.FacebookMessenger
|
||||
{
|
||||
public class WebhookEvent
|
||||
{
|
||||
public string Object { get; set; }
|
||||
public List<WebhookEventEntry> Entry { get; set; }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.RestApi.Integrations.FacebookMessenger
|
||||
{
|
||||
public class WebhookEventEntry
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public long Time { get; set; }
|
||||
public List<WebhookMessage> Messaging { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.RestApi.Integrations.FacebookMessenger
|
||||
{
|
||||
public class WebhookMessage
|
||||
{
|
||||
public WebhookMessageSender Sender { get; set; }
|
||||
|
||||
public WebhookMessageRecipient Recipient { get; set; }
|
||||
|
||||
public long Timestamp { get; set; }
|
||||
|
||||
public JObject Message { get; set; }
|
||||
}
|
||||
|
||||
public class WebhookMessage<TWebhookMessage> where TWebhookMessage : IWebhookMessageBody
|
||||
{
|
||||
public WebhookMessageSender Sender { get; set; }
|
||||
|
||||
public WebhookMessageRecipient Recipient { get; set; }
|
||||
|
||||
public long Timestamp { get; set; }
|
||||
|
||||
public TWebhookMessage Message { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.RestApi.Integrations.FacebookMessenger
|
||||
{
|
||||
public class WebhookMessageQuickReply
|
||||
{
|
||||
public String Payload { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.RestApi.Integrations.FacebookMessenger
|
||||
{
|
||||
public class WebhookMessageRecipient
|
||||
{
|
||||
/// <summary>
|
||||
/// PAGE_ID
|
||||
/// </summary>
|
||||
public String Id { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.RestApi.Integrations.FacebookMessenger
|
||||
{
|
||||
public class WebhookMessageSender
|
||||
{
|
||||
/// <summary>
|
||||
/// PSID
|
||||
/// </summary>
|
||||
public String Id { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.RestApi.Integrations.FacebookMessenger
|
||||
{
|
||||
public class WebhookTextMessage : IWebhookMessageBody
|
||||
{
|
||||
public string Mid { get; set; }
|
||||
public String Text { get; set; }
|
||||
[JsonProperty("quick_reply")]
|
||||
public WebhookMessageQuickReply QuickReply { get; set; }
|
||||
}
|
||||
}
|
||||
12
BotSharp.RestApi/Integrations/SkypeController.cs
Normal file
12
BotSharp.RestApi/Integrations/SkypeController.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.RestApi.Integrations
|
||||
{
|
||||
[Route("v1/[controller]/[action]")]
|
||||
public class SkypeController : ControllerBase
|
||||
{
|
||||
}
|
||||
}
|
||||
12
BotSharp.RestApi/Integrations/SlackController.cs
Normal file
12
BotSharp.RestApi/Integrations/SlackController.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.RestApi.Integrations
|
||||
{
|
||||
[Route("v1/[controller]/[action]")]
|
||||
public class SlackController : ControllerBase
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -3,15 +3,11 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.RestApi
|
||||
namespace BotSharp.RestApi.Integrations
|
||||
{
|
||||
[Route("v1/[controller]/[action]")]
|
||||
public class FacebookMessengerController : ControllerBase
|
||||
public class TelegramController : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
public void Facebook()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
12
BotSharp.RestApi/Integrations/TwitterController.cs
Normal file
12
BotSharp.RestApi/Integrations/TwitterController.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.RestApi.Integrations
|
||||
{
|
||||
[Route("v1/[controller]/[action]")]
|
||||
public class TwitterController : ControllerBase
|
||||
{
|
||||
}
|
||||
}
|
||||
12
BotSharp.RestApi/Integrations/ViberController.cs
Normal file
12
BotSharp.RestApi/Integrations/ViberController.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BotSharp.RestApi.Integrations
|
||||
{
|
||||
[Route("v1/[controller]/[action]")]
|
||||
public class ViberController : ControllerBase
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -16,9 +16,8 @@ namespace BotSharp.UnitTest
|
|||
{
|
||||
var rasa = new RasaAi();
|
||||
var agent = rasa.LoadAgent(BOT_ID);
|
||||
|
||||
var config = new AIConfiguration(agent.ClientAccessToken, SupportedLanguage.English) { AgentId = BOT_ID };
|
||||
config.SessionId = Guid.NewGuid().ToString();
|
||||
rasa.AiConfig = new AIConfiguration(agent.ClientAccessToken, SupportedLanguage.English) { AgentId = BOT_ID };
|
||||
rasa.AiConfig.SessionId = Guid.NewGuid().ToString();
|
||||
|
||||
// Round 1
|
||||
var response = rasa.TextRequest(new AIRequest { Query = new String[] { "Can you play country music?" } });
|
||||
|
|
|
|||
|
|
@ -8,8 +8,9 @@
|
|||
"Integrations":
|
||||
[
|
||||
{
|
||||
"Platform": "FacebookMessenger",
|
||||
"PageAccessToken": "EAAJym8gGQFMBAPnsxTw6rZBE2WVfYtCJeGkCQ2NZC3VbQd45SyUlXjjgUf1gAkCOWq7v0blxTb1xLZAeMAXYhQj3btcMlMO9YbG7StMyx8ussz5TnD1tjjIZCye5u66PZAuFxSyIPXtTCin8GPiJ19cYB8ZCyH3rr5sro7OxUSyAZDZD"
|
||||
"Platform": "Facebook Messenger",
|
||||
"VerifyToken": "spotify_music_bot",
|
||||
"AccessToken": "EAAJym8gGQFMBAPnsxTw6rZBE2WVfYtCJeGkCQ2NZC3VbQd45SyUlXjjgUf1gAkCOWq7v0blxTb1xLZAeMAXYhQj3btcMlMO9YbG7StMyx8ussz5TnD1tjjIZCye5u66PZAuFxSyIPXtTCin8GPiJ19cYB8ZCyH3rr5sro7OxUSyAZDZD"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
BIN
BotSharp.WebHost/wwwroot/images/spotify-clip.png
Normal file
BIN
BotSharp.WebHost/wwwroot/images/spotify-clip.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.9 KiB |
BIN
BotSharp.WebHost/wwwroot/images/spotify.png
Normal file
BIN
BotSharp.WebHost/wwwroot/images/spotify.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.5 KiB |
Loading…
Reference in a new issue