Church.Net.API/Church.Net.WebAPI/Controllers/LineMessageController.cs
2024-05-02 15:24:13 -07:00

132 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Church.Net.DAL.EF;
using Church.Net.Entity;
using Church.Net.Utility;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Primitives;
using QRCoder;
using LineMessaging;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Text;
using WebAPI.Services;
using Jint.Native;
using WebAPI.Services.Interfaces;
using Church.Net.DAL.EFCoreDBF.Migrations;
using WebAPI.Logics;
using Church.Net.DAL.EFCoreDBF.Interface;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace WebAPI.Controllers
{
//[Route("[controller]")]
[ApiController]
[Route("[controller]/[action]")]
public class LineMessageController : ControllerBase
{
private readonly LineAutoBotService lineAutoBotService;
private readonly ILoggingService loggingService;
private readonly ICrudDAL<LineMessagingAccount> crudDAL;
private readonly PastoralDomainLogic pastoralDomainLogic;
public LineMessageController(
LineAutoBotService lineAutoBotService,
ILoggingService loggingService,
ICrudDAL<LineMessagingAccount> crudDAL,
PastoralDomainLogic pastoralDomainLogic
)
{
this.lineAutoBotService = lineAutoBotService;
this.loggingService = loggingService;
this.crudDAL = crudDAL;
this.pastoralDomainLogic = pastoralDomainLogic;
}
//private ChurchNetContext dbContext = new ChurchNetContext();
//// GET: api/<BestController>
//[HttpGet]
//public IEnumerable<string> Get()
//{
// return new string[] { "value2222", "value4" };
//}
//// GET api/<BestController>/5
//[HttpGet("{id}")]
//public HappinessBEST Get(string id)
//{
// var best = dbContext.HappinessBESTs.Include(b => b.HappinessGroup).ThenInclude(b => b.Weeks).FirstOrDefault(b => b.BestId == id);
// best.HappinessGroup.Weeks = best.HappinessGroup.Weeks.OrderBy(w => w.SEQ).ToList();
// return best;
//}
// POST api/<BestController>
[HttpPost]
public async Task PostFromLine(int seq, [FromBody] object jsonData)
{
//string txtPath = ServerUtils.MapPath("App_Data/LinePostRawLog.txt");
//System.IO.File.AppendAllText(txtPath, JsonConvert.SerializeObject(jsonData.ToString(), Formatting.Indented));
try
{
LineMessagingAccount lineAccount = crudDAL.GetAll(l => l.Seq == seq).FirstOrDefault();
if (lineAccount != null)
{
LineWebhookContent content = JsonConvert.DeserializeObject<LineWebhookContent>(jsonData.ToString());
await lineAutoBotService.AutoReply(lineAccount,content);
}
//this.loggingService.Log("PostFromLine");
}
catch (Exception ex)
{
this.loggingService.Error(ex);
}
//Cac4ac5a8d7fc52daa444d71dc7c360a9 方舟小組 GroupID
//Ca20e3b65aa58e676815eb13c3222591a 方舟小組同工 GroupID
//var test = new LineMessaging.();
//var test = new LineMessagingClient("WFAyMvMEZ86cfMJIAzE+yklUZGpeS/jFYTeL9a9O35QR83oNMmwaUJfyEe48Kegadz0BArDdBoySxs479U1pwTHtlyH+Sm4jqlz8BwukX/Hsa4D1fX03Qn4zFu7TwPFKWFXnZbWq89Yg0iNzjpfTNwdB04t89/1O/w1cDnyilFU=");
//test.PushMessage(value.To, new LineTextMessage() { Text = value.Message });
}
[HttpPut("{id}")]
public async void PushTextMessage(string id, [FromBody] LineMessage value)
{
var test = new LineMessagingClient();
await test.PushMessage(id, new LineTextMessage() { Text = value.Message });
}
[HttpGet]
public Task PushCommandMessage(string groupId, string command)
{
return lineAutoBotService.PushCommandMessage(pastoralDomainLogic.GetById(groupId), "#" + command);
}
}
public class LineMessage
{
public string To { get; set; }
public string Message { get; set; }
}
}