Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="ViewModels\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.1.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BotSharp.Platform.Dialogflow\BotSharp.Platform.Dialogflow.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using BotSharp.Channel.FacebookMessenger.Models;
using BotSharp.Platform.Models;
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.Channel.FacebookMessenger.Controllers
{
[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 ai = new ApiAi();
var agent = ai.LoadAgent(agentId);
ai.AiConfig = new AIConfiguration(agent.ClientAccessToken, SupportedLanguage.English) { AgentId = agentId };
ai.AiConfig.SessionId = message.Sender.Id;
var aiResponse = ai.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 = String.IsNullOrEmpty(aiResponse.Result.Fulfillment.Speech) ? aiResponse.Result.Action : 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);
}
}
}
10 changes: 10 additions & 0 deletions BotSharp.Channel.FacebookMessenger/IWebhookMessageBody.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace BotSharp.Channel.FacebookMessenger
{
public interface IWebhookMessageBody
{
}
}
14 changes: 14 additions & 0 deletions BotSharp.Channel.FacebookMessenger/Models/WebhookEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace BotSharp.Channel.FacebookMessenger.Models
{
public class WebhookEvent
{
public string Object { get; set; }
public List<WebhookEventEntry> Entry { get; set; }
}


}
13 changes: 13 additions & 0 deletions BotSharp.Channel.FacebookMessenger/Models/WebhookEventEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace BotSharp.Channel.FacebookMessenger.Models
{
public class WebhookEventEntry
{
public string Id { get; set; }
public long Time { get; set; }
public List<WebhookMessage> Messaging { get; set; }
}
}
29 changes: 29 additions & 0 deletions BotSharp.Channel.FacebookMessenger/Models/WebhookMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;

namespace BotSharp.Channel.FacebookMessenger.Models
{
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; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace BotSharp.Channel.FacebookMessenger.Models
{
public class WebhookMessageQuickReply
{
public String Payload { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace BotSharp.Channel.FacebookMessenger.Models
{
public class WebhookMessageRecipient
{
/// <summary>
/// PAGE_ID
/// </summary>
public String Id { get; set; }
}
}
14 changes: 14 additions & 0 deletions BotSharp.Channel.FacebookMessenger/Models/WebhookMessageSender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace BotSharp.Channel.FacebookMessenger.Models
{
public class WebhookMessageSender
{
/// <summary>
/// PSID
/// </summary>
public String Id { get; set; }
}
}
15 changes: 15 additions & 0 deletions BotSharp.Channel.FacebookMessenger/Models/WebhookTextMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace BotSharp.Channel.FacebookMessenger.Models
{
public class WebhookTextMessage : IWebhookMessageBody
{
public string Mid { get; set; }
public String Text { get; set; }
[JsonProperty("quick_reply")]
public WebhookMessageQuickReply QuickReply { get; set; }
}
}
22 changes: 22 additions & 0 deletions BotSharp.Channel.FacebookMessenger/ModuleInjector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using BotSharp.Core.Modules;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace BotSharp.Channel.FacebookMessenger
{
public class ModuleInjector : IModule
{
public void ConfigureServices(IServiceCollection services, IConfiguration config)
{

}

public void Configure(IApplicationBuilder app, IHostingEnvironment env/*, IOptions<SenparcSetting> senparcSetting, IOptions<SenparcWeixinSetting> senparcWeixinSetting*/)
{

}
}
}
30 changes: 30 additions & 0 deletions BotSharp.Channel.Weixin/BotSharp.Channel.Weixin.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Haiping Chen</Authors>
<RepositoryUrl>https://github.com/Oceania2018/botsharp-channel-weixin</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageProjectUrl>https://github.com/Oceania2018/botsharp-channel-weixin</PackageProjectUrl>
<Copyright>Apache 2.0</Copyright>
<PackageTags>botsharp, wechat, wexin, chatbot</PackageTags>
<AssemblyVersion>0.1.1.0</AssemblyVersion>
<FileVersion>0.1.1.0</FileVersion>
<Version>0.1.1</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.1.3" />
<PackageReference Include="Senparc.Weixin.MP.MVC" Version="7.1.11" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BotSharp.Platform.Dialogflow\BotSharp.Platform.Dialogflow.csproj" />
</ItemGroup>

</Project>
Loading