Church.Net.API/WebAPI/Controllers/PingController.cs
2022-09-08 08:04:32 -07:00

199 lines
7.2 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Church.Net.Utility;
using LineMessaging;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using WebAPI.Logics;
using WebAPI.Services;
using static System.Net.Mime.MediaTypeNames;
using WebAPI.Services.Interfaces;
namespace WebAPI.Controllers
{
[ApiController]
[Route("[controller]/[action]")]
public class PingController : ControllerBase
{
private readonly IEnumerable<IAutoReplyCommand> autoReplyCommands;
public PingController(VideoDownloadLogic videoDownloadLogic,
IEnumerable<IAutoReplyCommand> autoReplyCommands)
{
VideoDownloadLogic = videoDownloadLogic;
this.autoReplyCommands = autoReplyCommands;
}
public VideoDownloadLogic VideoDownloadLogic { get; }
[HttpGet]
public List<Controller> GetEndpoints()
{
//using (var fileStream = new FileStream(ServerUtils.MapPath("Church/Test.mp4"), FileMode.Create))
//{
//}
//await Task.Run(() => {
//this.VideoDownloadLogic.Download(@"https://www.youtube.com/watch?v=nJBLeMrhu9w", @"\\ArkNAS\Church\Test.mp4");
//});
Console.WriteLine($"Self GetEndpoints: {DateTime.Now}");
string @namespace = this.GetType().Namespace;
Type ApiControllerType = typeof(ControllerBase);
IEnumerable<Type> controllers = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(t => t.Namespace == @namespace && ApiControllerType.IsAssignableFrom(t))
;
HashSet<string> baseMethods = new HashSet<string>(ApiControllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance).Select(mi => mi.Name));
List<Controller> _controllers = new List<Controller>();
foreach (var controller in controllers)
{
Controller _controller = new Controller { Name = controller.Name.Replace("Controller", "") };
foreach (var methodInfo in controller.GetMethods(BindingFlags.Public | BindingFlags.Instance))
{
if (baseMethods.Contains(methodInfo.Name)) continue;
try
{
bool isGet = methodInfo.GetCustomAttributes(typeof(HttpGetAttribute), false).Length > 0;
bool isPost = methodInfo.GetCustomAttributes(typeof(HttpPostAttribute), false).Length > 0;
bool isDelete = methodInfo.GetCustomAttributes(typeof(HttpDeleteAttribute), false).Length > 0;
if(isGet|| isPost|| isDelete)
{
Endpoint _endpoint = new Endpoint
{
Name = methodInfo.Name,
Method = isGet ? "GET" : (isPost ? "POST" : (isDelete ? "DELETE" : "UNKNOWN")),
Inputs = methodInfo.GetParameters().Select(m=>new Input() { Name=m.Name}).ToArray(),
ReturnType = methodInfo.ReturnType,
//MethodInfo = methodInfo,
};
//_endpoint.HasDocString = HasDocString(_endpoint);
_controller.Endpoints.Add(_endpoint);
}
}
catch (Exception ex)
{
}
}
_controllers.Add(_controller);
}
return _controllers;
}
[HttpGet]
public async void TestMessage()
{
//\\ArkNAS\Church\WorshipVideo
//this.VideoDownloadLogic.Download(@"https://www.youtube.com/watch?v=K2bdSYim7uI", @"\\ArkNAS\home\Test.mp4");
var test = new LineMessagingClient();
string text = "$$$$Menu Item";
var textMessage = new LineTextMessage() { Text = text, Emojis = new List<Emoji>() };
textMessage.AddEmoji("$$", "5ac1bfd5040ab15980c9b435", "002");
var templateMessage = new LineTemplateMessage<ButtonTemplateObject>();
var addPrayerBtn = new UriAction()
{
Uri = "https://happiness.tours/CellGroup/prayer?openExternalBrowser=1",
Label = "Prayer"
};
templateMessage.AltText= "代禱事項";
templateMessage.Template.DefaultAction = addPrayerBtn;
templateMessage.Template.ThumbnailImageUrl = "https://dailyverses.net/images/tc/cuv/matthew-21-22-3.jpg";
templateMessage.Template.Title = "代禱事項";
templateMessage.Template.Text = "Chris" + Environment.NewLine + "Testwerewiorjowerjiowejiro, erjaiworjweiorjioawereaw";
templateMessage.Template.Actions = new List<ILineAction>();
templateMessage.Template.Actions.Add(addPrayerBtn);
await test.PushMessage(EnumHelper.EnumToDescriptionString(LineGroup.Chris), textMessage);
}
[HttpGet]
public async void TestAutoReply(string command)
{
//\\ArkNAS\Church\WorshipVideo
//this.VideoDownloadLogic.Download(@"https://www.youtube.com/watch?v=K2bdSYim7uI", @"\\ArkNAS\home\Test.mp4");
if (!String.IsNullOrWhiteSpace(command))
{
string text = command;
var group = LineGroup.Chris;
var autoReply = autoReplyCommands.Where(ar => ar.SupportGroups.Contains(group) && ar.Commands.Contains(text)).FirstOrDefault();
if (autoReply != null)
{
if (autoReply.LineMessage != null)
{
ReplyLineMessage(group.EnumToDescriptionString(), autoReply.LineMessage);
}
else
{
// ReplyTextMessage(replyToken, autoReply.ReplyMessage);
}
return;
}
};
}
public class Controller
{
public string Name { get; set; }
public List<Endpoint> Endpoints { get; set; } = new List<Endpoint>();
}
public class Endpoint
{
public string Name { get; set; }
public string Method { get; set; }
public Input[] Inputs { get; set; }
public Type ReturnType { get; set; }
public MethodInfo MethodInfo { get; set; }
public bool HasDocString { get; set; }
}
public partial class Input
{
public string Name { get; set; }
public long Position { get; set; }
}
private async void ReplyLineMessage(string replyToken, IEnumerable<ILineMessage> lineMessages)
{
var test = new LineMessagingClient();
var replyMessage = new LinePushMessage() { To = replyToken };
replyMessage.Messages = lineMessages;
await test.PushMessage(replyMessage);
}
}
}