296 lines
11 KiB
C#
296 lines
11 KiB
C#
using Church.Net.Utility;
|
|
using LineMessaging;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using WebAPI.Services.Interfaces;
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
|
|
namespace WebAPI.Services
|
|
{
|
|
[Flags]
|
|
public enum LineGroup
|
|
{
|
|
[Description("Cac4ac5a8d7fc52daa444d71dc7c360a9")]
|
|
Ark = 1 << 0,
|
|
[Description("Ca20e3b65aa58e676815eb13c3222591a")]
|
|
ArkCowoker = 1 << 1,
|
|
[Description("U97e9e579a41a5222e33bf68a3bf479a0")]
|
|
Chris = ~(~0 << 20),
|
|
}
|
|
public class LineAutoBotService
|
|
{
|
|
private readonly IEnumerable<IAutoReplyCommand> autoReplyCommands;
|
|
private readonly ILoggingService loggingService;
|
|
|
|
public LineAutoBotService(
|
|
IEnumerable<IAutoReplyCommand> autoReplyCommands,
|
|
ILoggingService loggingService
|
|
)
|
|
{
|
|
this.autoReplyCommands = autoReplyCommands;
|
|
this.loggingService = loggingService;
|
|
}
|
|
public void SendTextMessage(string text, LineGroup target)
|
|
{
|
|
var test = new LineMessagingClient();
|
|
|
|
try
|
|
{
|
|
var wait = test.PushMessage(EnumHelper.EnumToDescriptionString(target), new LineTextMessage() { Text = text })
|
|
.GetAwaiter();
|
|
wait.GetResult();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.loggingService.Error(ex);
|
|
}
|
|
|
|
}
|
|
public async Task<bool> ReplyTextMessage(string replyToken, IEnumerable<string> textMessages)
|
|
{
|
|
var test = new LineMessagingClient();
|
|
|
|
var replyMessage = new LineReplyMessage() { ReplyToken = replyToken };
|
|
var messages = new List<ILineMessage>();
|
|
foreach (var message in textMessages)
|
|
{
|
|
messages.Add(new LineTextMessage() { Text = message });
|
|
}
|
|
replyMessage.Messages = messages;
|
|
//test.ReplyMessage(replyMessage);
|
|
|
|
try
|
|
{
|
|
await test.ReplyMessage(replyMessage);
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.loggingService.Error(ex, "ReplyTextMessage:68", JsonConvert.SerializeObject(replyMessage, Formatting.Indented));
|
|
return false;
|
|
}
|
|
}
|
|
public async Task<bool> ReplyLineMessage(string replyToken, IEnumerable<ILineMessage> lineMessages)
|
|
{
|
|
var test = new LineMessagingClient();
|
|
|
|
var replyMessage = new LinePushMessage() { To = replyToken };
|
|
replyMessage.Messages = lineMessages;
|
|
|
|
try
|
|
{
|
|
//var wait= test.PushMessage(replyMessage).GetAwaiter();
|
|
//wait.GetResult();
|
|
await test.PushMessage(replyMessage);
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.loggingService.Error(ex, "ReplyLineMessage:84", JsonConvert.SerializeObject(replyMessage, Formatting.Indented));
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
public async Task<bool> AutoReply(LineWebhookContent content)
|
|
{
|
|
loggingService.Log("AutoReply", content);
|
|
try
|
|
{
|
|
|
|
foreach (var e in content.Events)
|
|
{
|
|
if (e.Message.Type == MessageType.Text)
|
|
{
|
|
List<string> textMessages = new List<string>();
|
|
string replyToken = e.ReplyToken;
|
|
string text = e.Message.Text;
|
|
string target = "";
|
|
switch (e.Source.Type)
|
|
{
|
|
case WebhookRequestSourceType.User:
|
|
target = e.Source.UserId;
|
|
break;
|
|
case WebhookRequestSourceType.Group:
|
|
target = e.Source.GroupId;
|
|
|
|
break;
|
|
case WebhookRequestSourceType.Room:
|
|
target = e.Source.RoomId;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if (!String.IsNullOrWhiteSpace(replyToken) && text.IndexOf("#") == 0)
|
|
{
|
|
text = text.ToLower().Substring(1);
|
|
var group = EnumHelper.GetEnumValueFromDescription<LineGroup>(target);
|
|
|
|
var autoReply = autoReplyCommands.Where(ar => ar.SupportGroups.Contains(group) && ar.Commands.Contains(text)).FirstOrDefault();
|
|
if (autoReply != null)
|
|
{
|
|
|
|
if (autoReply.LineMessage != null)
|
|
{
|
|
await ReplyLineMessage(target, autoReply.LineMessage);
|
|
}
|
|
else
|
|
{
|
|
await ReplyTextMessage(replyToken, autoReply.ReplyMessage);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
else if (text == "help" || text == "?")
|
|
{
|
|
StringBuilder commandText = new StringBuilder();
|
|
commandText.AppendLine("方舟資訊部 - 指令清單");
|
|
foreach (var ar in autoReplyCommands.Where(ar => ar.SupportGroups.Contains(group)))
|
|
{
|
|
commandText.AppendLine($"{string.Join(", ", ar.Commands.Select(s => $"#{s}"))} - {ar.Description}");
|
|
}
|
|
commandText.Append($"#help, #? - 顯示指令清單");
|
|
await ReplyTextMessage(replyToken, new string[] { commandText.ToString() });
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
this.LogLineMessage(e);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
loggingService.Error(ex, "AutoReply:188", JsonConvert.SerializeObject(content, Formatting.Indented));
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
public async Task<bool> PushCommandMessage(LineGroup group, string command)
|
|
{
|
|
try
|
|
{
|
|
|
|
if (command.IndexOf("#") == 0)
|
|
{
|
|
command = command.ToLower().Substring(1);
|
|
|
|
var autoReply = autoReplyCommands.Where(ar => ar.SupportGroups.Contains(group) && ar.Commands.Contains(command)).FirstOrDefault();
|
|
if (autoReply != null)
|
|
{
|
|
|
|
if (autoReply.LineMessage != null)
|
|
{
|
|
await ReplyLineMessage(group.EnumToDescriptionString(), autoReply.LineMessage);
|
|
}
|
|
else
|
|
{
|
|
await ReplyTextMessage(group.EnumToDescriptionString(), autoReply.ReplyMessage);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
else if (command == "help" || command == "?")
|
|
{
|
|
StringBuilder commandText = new StringBuilder();
|
|
commandText.AppendLine("方舟資訊部 - 指令清單");
|
|
foreach (var ar in autoReplyCommands.Where(ar => ar.SupportGroups.Contains(group)))
|
|
{
|
|
commandText.AppendLine($"{string.Join(", ", ar.Commands.Select(s => $"#{s}"))} - {ar.Description}");
|
|
}
|
|
commandText.Append($"#help, #? - 顯示指令清單");
|
|
await ReplyTextMessage(group.EnumToDescriptionString(), new string[] { commandText.ToString() });
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
loggingService.Error(ex, "AutoReply:188",command);
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private void LogLineMessage(LineWebhookContent.Event e)
|
|
{
|
|
return;
|
|
try
|
|
{
|
|
|
|
//var lineHookRequest = new LineMessaging.LineWebhookRequest("d23edf453427256a759d218ec8b6779f", request);
|
|
//var content = await lineHookRequest.GetContent();
|
|
string txtPath = ServerUtils.MapPath("App_Data/LinePostLog.txt");
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
stringBuilder.AppendLine($"==================={DateTime.Now.ToString("MM/dd/yyyy HH:mm")}=========================");
|
|
|
|
stringBuilder.AppendLine($"Message Type:{e.Type.ToString()}");
|
|
|
|
switch (e.Source.Type)
|
|
{
|
|
case WebhookRequestSourceType.User:
|
|
stringBuilder.AppendLine($"UserId:{e.Source.UserId}");
|
|
break;
|
|
case WebhookRequestSourceType.Group:
|
|
stringBuilder.AppendLine($"GroupId:{e.Source.GroupId}");
|
|
|
|
break;
|
|
case WebhookRequestSourceType.Room:
|
|
stringBuilder.AppendLine($"RoomId:{e.Source.RoomId}");
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
stringBuilder.AppendLine($"Reply Token:{e.ReplyToken}");
|
|
|
|
stringBuilder.AppendLine($"Message Type:{e.Message.Type.ToString()}");
|
|
switch (e.Message.Type)
|
|
{
|
|
case MessageType.Text:
|
|
stringBuilder.AppendLine($"Message:{e.Message.Text}");
|
|
break;
|
|
case MessageType.Image:
|
|
case MessageType.Video:
|
|
case MessageType.Audio:
|
|
case MessageType.File:
|
|
case MessageType.Location:
|
|
case MessageType.Sticker:
|
|
case MessageType.Imagemap:
|
|
case MessageType.Template:
|
|
default:
|
|
|
|
stringBuilder.AppendLine($"Message:{JsonConvert.SerializeObject(e.Message, Formatting.Indented)}");
|
|
break;
|
|
}
|
|
|
|
|
|
System.IO.File.AppendAllText(txtPath, stringBuilder.ToString());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|