Update API
This commit is contained in:
@@ -16,10 +16,9 @@ namespace WebAPI.Services.AutoReplyCommands
|
||||
public class ArArkCellGroupDinner : IAutoReplyCommand
|
||||
{
|
||||
|
||||
public ArArkCellGroupDinner(ChurchNetContext context,
|
||||
public ArArkCellGroupDinner(
|
||||
CellGroupLogic logic)
|
||||
{
|
||||
this._context = context;
|
||||
this.logic = logic;
|
||||
}
|
||||
private static readonly string[] COMMANDS = { "晚餐", "dinner" };
|
||||
@@ -28,57 +27,12 @@ namespace WebAPI.Services.AutoReplyCommands
|
||||
LineGroup.Ark,
|
||||
LineGroup.Chris,
|
||||
};
|
||||
private readonly ChurchNetContext _context;
|
||||
private readonly CellGroupLogic logic;
|
||||
|
||||
public string Description => "顯示方舟小組聚會晚餐";
|
||||
public IEnumerable<string> Commands => COMMANDS;
|
||||
public IEnumerable<LineGroup> SupportGroups => GROUPS;
|
||||
public IEnumerable<string> ReplyMessage
|
||||
{
|
||||
get
|
||||
{
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
StringBuilder comments = new StringBuilder();
|
||||
sb.AppendLine("又到了令人期待的小組日~~~");
|
||||
var _event = _context.CellGroupRoutineEvents.Where(e => e.Time >= DateTime.Today)
|
||||
.Include(e => e.Attendees).FirstOrDefault();
|
||||
if (_event == null)
|
||||
{
|
||||
_event = new CellGroupRoutineEvent()
|
||||
{
|
||||
Id = Format.Get33BaseGuid(),
|
||||
Time = DateTimeHelper.GetNextWeekday(DateTime.Today, DayOfWeek.Friday).AddHours(19).AddMinutes(30),
|
||||
Address = "1881 Forest Dr., Azusa, CA 91702",
|
||||
Attendees = new List<CellGroupRoutineEventAttendee>()
|
||||
};
|
||||
_context.Add(_event);
|
||||
_context.SaveChanges();
|
||||
}
|
||||
|
||||
|
||||
sb.AppendLine($"{_event.Time.ToString("MM/dd HH:mm tt")} 開飯~");
|
||||
|
||||
sb.AppendLine("======= 晚宴清單 =======");
|
||||
foreach (var a in _event.Attendees)
|
||||
{
|
||||
sb.AppendLine($"{a.Name} - {string.Join(", ", a.PotluckItem.Split('|'))}");
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(a.Comment))
|
||||
{
|
||||
comments.AppendLine($"{a.Name}:{a.Comment}");
|
||||
}
|
||||
}
|
||||
|
||||
if (comments.Length > 0)
|
||||
{
|
||||
sb.AppendLine("========= 備註 =========");
|
||||
sb.Append(comments.ToString());
|
||||
}
|
||||
return new string[] { sb.ToString() };
|
||||
}
|
||||
}
|
||||
public IEnumerable<string> ReplyMessage => null;
|
||||
|
||||
public IEnumerable<ILineMessage> LineMessage
|
||||
{
|
||||
|
||||
@@ -231,7 +231,8 @@ namespace WebAPI.Services.AutoReplyCommands
|
||||
{
|
||||
Size = FlexObjectSize.sm,
|
||||
Color = "#666666",
|
||||
Flex = 5
|
||||
Flex = 5,
|
||||
Wrap = true
|
||||
});
|
||||
comments.Add(commentLineBox);
|
||||
}
|
||||
|
||||
@@ -127,12 +127,12 @@ namespace WebAPI.Services
|
||||
EventLog.WriteEntry("BeyondAPI", $"Can Not Append Error Log:{ex.Message}", EventLogEntryType.Error);
|
||||
}
|
||||
|
||||
public void Log(string message, object detail)
|
||||
public void Log(string message, object detail = null)
|
||||
{
|
||||
LogInfo log = new LogInfo(LogLevel.Info)
|
||||
{
|
||||
Message = message,
|
||||
DetailMessage = JsonConvert.SerializeObject(detail,Formatting.Indented) ,
|
||||
DetailMessage = JsonConvert.SerializeObject(detail, Formatting.Indented),
|
||||
//Source = exception.Source,
|
||||
//StackTrace = exception.StackTrace
|
||||
};
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
using Church.Net.Entity;
|
||||
using Church.Net.Utility;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace WebAPI.Services
|
||||
{
|
||||
public class IdentityService
|
||||
{
|
||||
public string UserAccessToken { get; set; }
|
||||
public string UserId { get; set; }
|
||||
|
||||
public PastoralDomain CellGroup
|
||||
{
|
||||
get {
|
||||
|
||||
TokenHelper.GetUserIdFromToken
|
||||
return null; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ namespace WebAPI.Services.Interfaces
|
||||
{
|
||||
public interface ILoggingService
|
||||
{
|
||||
void Log(string message, object detail);
|
||||
void Log(string message, object detail=null);
|
||||
void Warning(string message, object detail);
|
||||
int Error(Exception exception);
|
||||
int Error(Exception exception, string from);
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
using LineMessaging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WebAPI.Services.Interfaces
|
||||
{
|
||||
public interface IScheduledTask
|
||||
{
|
||||
string Description { get; }
|
||||
bool CheckTime(DateTime time);
|
||||
Task<bool> RunTask();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Church.Net.Utility;
|
||||
using Church.Net.Entity.Messenger;
|
||||
using Church.Net.Utility;
|
||||
using LineMessaging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Newtonsoft.Json;
|
||||
@@ -8,6 +9,7 @@ using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WebAPI.Logics.Interface;
|
||||
using WebAPI.Services.Interfaces;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
|
||||
@@ -27,14 +29,17 @@ namespace WebAPI.Services
|
||||
{
|
||||
private readonly IEnumerable<IAutoReplyCommand> autoReplyCommands;
|
||||
private readonly ILoggingService loggingService;
|
||||
private readonly ICrudLogic<LineMessageClient> clientLogic;
|
||||
|
||||
public LineAutoBotService(
|
||||
IEnumerable<IAutoReplyCommand> autoReplyCommands,
|
||||
ILoggingService loggingService
|
||||
ILoggingService loggingService,
|
||||
ICrudLogic<LineMessageClient> clientLogic
|
||||
)
|
||||
{
|
||||
this.autoReplyCommands = autoReplyCommands;
|
||||
this.loggingService = loggingService;
|
||||
this.clientLogic = clientLogic;
|
||||
}
|
||||
public void SendTextMessage(string text, LineGroup target)
|
||||
{
|
||||
@@ -72,7 +77,12 @@ namespace WebAPI.Services
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.loggingService.Error(ex, "ReplyTextMessage:68", JsonConvert.SerializeObject(replyMessage, Formatting.Indented));
|
||||
this.loggingService.Error(ex, "ReplyTextMessage:75", JsonConvert.SerializeObject(replyMessage, Formatting.Indented));
|
||||
|
||||
if (ex.Message == "You have reached your monthly limit.")
|
||||
{
|
||||
this.SendTextMessage("Line Bot Exist Monthly Limit!!!", LineGroup.Chris);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -92,7 +102,11 @@ namespace WebAPI.Services
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.loggingService.Error(ex, "ReplyLineMessage:84", JsonConvert.SerializeObject(replyMessage, Formatting.Indented));
|
||||
this.loggingService.Error(ex, "ReplyLineMessage:95", JsonConvert.SerializeObject(replyMessage, Formatting.Indented));
|
||||
if (ex.Message == "You have reached your monthly limit.")
|
||||
{
|
||||
this.SendTextMessage("Line Bot Exist Monthly Limit!!!", LineGroup.Chris);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -112,10 +126,13 @@ namespace WebAPI.Services
|
||||
string replyToken = e.ReplyToken;
|
||||
string text = e.Message.Text;
|
||||
string target = "";
|
||||
bool isGroup = true;
|
||||
var client = new LineMessagingClient();
|
||||
switch (e.Source.Type)
|
||||
{
|
||||
case WebhookRequestSourceType.User:
|
||||
target = e.Source.UserId;
|
||||
isGroup = false;
|
||||
break;
|
||||
case WebhookRequestSourceType.Group:
|
||||
target = e.Source.GroupId;
|
||||
@@ -127,6 +144,33 @@ namespace WebAPI.Services
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (isGroup)
|
||||
{
|
||||
if (clientLogic.First(c => c.ClientId == e.Source.GroupId) == null)
|
||||
{
|
||||
clientLogic.CreateOrUpdate(new LineMessageClient()
|
||||
{
|
||||
ClientId = target,
|
||||
Name = client.GetGroupProfile(e.Source.GroupId).Result.GroupName,
|
||||
IsGroup = true
|
||||
}, out string id);
|
||||
}
|
||||
//TODO:Get user by user id under group
|
||||
}
|
||||
else
|
||||
{
|
||||
if (clientLogic.First(c => c.ClientId == e.Source.UserId) == null)
|
||||
{
|
||||
|
||||
clientLogic.CreateOrUpdate(new LineMessageClient()
|
||||
{
|
||||
ClientId = target,
|
||||
Name = client.GetProfile(e.Source.UserId).Result.DisplayName,
|
||||
IsGroup = false
|
||||
}, out string id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!String.IsNullOrWhiteSpace(replyToken) && text.IndexOf("#") == 0)
|
||||
{
|
||||
@@ -166,8 +210,12 @@ namespace WebAPI.Services
|
||||
|
||||
|
||||
}
|
||||
else if (e.Message.Type == MessageType.Sticker && e.Source.GroupId == LineGroup.Ark.EnumToDescriptionString() && e.Message.PackageId == "1011092" && e.Message.StickerId == "510712")
|
||||
{
|
||||
|
||||
await ReplyLineMessage(e.Source.GroupId, autoReplyCommands.Where(ar => ar.Commands.Contains("pray")).FirstOrDefault().LineMessage);
|
||||
}
|
||||
|
||||
this.LogLineMessage(e);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -224,7 +272,7 @@ namespace WebAPI.Services
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
loggingService.Error(ex, "AutoReply:188",command);
|
||||
loggingService.Error(ex, "AutoReply:188", command);
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -232,64 +280,5 @@ namespace WebAPI.Services
|
||||
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)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
using Church.Net.Utility;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using WebAPI.Services.Interfaces;
|
||||
|
||||
namespace WebAPI.Services.ScheduledTask
|
||||
{
|
||||
public class MorningPrayer : IScheduledTask
|
||||
{
|
||||
private readonly LineAutoBotService lineAutoBotService;
|
||||
private readonly ILoggingService loggingService;
|
||||
private DateTime? nextRunningTime = null;
|
||||
public MorningPrayer(
|
||||
LineAutoBotService lineAutoBotService,
|
||||
ILoggingService loggingService
|
||||
)
|
||||
{
|
||||
this.lineAutoBotService = lineAutoBotService;
|
||||
this.loggingService = loggingService;
|
||||
this.SetNextRunningTime();
|
||||
}
|
||||
public string Description => "Sent out Ark Morning Prayer";
|
||||
|
||||
public bool CheckTime(DateTime time)
|
||||
{
|
||||
if(nextRunningTime == null)
|
||||
{
|
||||
this.SetNextRunningTime();
|
||||
return true;
|
||||
}
|
||||
|
||||
return time >= nextRunningTime.Value;
|
||||
}
|
||||
|
||||
public Task<bool> RunTask()
|
||||
{
|
||||
SetNextRunningTime();
|
||||
return lineAutoBotService.PushCommandMessage(LineGroup.Ark, "#pray");
|
||||
}
|
||||
private void SetNextRunningTime()
|
||||
{
|
||||
nextRunningTime = DateTimeHelper.Today().AddDays(1).AddHours(8);
|
||||
loggingService.Log($"Scheduled Task {this.Description}", nextRunningTime.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using System;
|
||||
using WebAPI.Services.Interfaces;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Church.Net.Utility;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
|
||||
namespace WebAPI.Services
|
||||
{
|
||||
public class WorkerService : BackgroundService
|
||||
{
|
||||
private IEnumerable<IScheduledTask> scheduledTasks;
|
||||
private ILoggingService loggingService;
|
||||
private readonly IWebHostEnvironment env;
|
||||
private static bool initialized = false;
|
||||
|
||||
public WorkerService(
|
||||
IEnumerable<IScheduledTask> scheduledTasks,
|
||||
ILoggingService loggingService
|
||||
)
|
||||
{
|
||||
this.scheduledTasks = scheduledTasks;
|
||||
this.loggingService = loggingService;
|
||||
}
|
||||
private const int generalDelay = 5 * 60 * 1000; // 10 seconds
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
|
||||
//await DoBackupAsync();
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
await Task.Delay(generalDelay, stoppingToken);
|
||||
await DoBackupAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<string> DoBackupAsync()
|
||||
{
|
||||
// here i can write logic for taking backup at midnight
|
||||
//if (IsMidnight())
|
||||
//{
|
||||
// Console.WriteLine("Executing background task");
|
||||
// TakeBackup();
|
||||
//}
|
||||
|
||||
var now = DateTimeHelper.Now();
|
||||
foreach (var worker in scheduledTasks)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (worker.CheckTime(now))
|
||||
{
|
||||
loggingService.Log($"Running {worker.Description}");
|
||||
await worker.RunTask();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
loggingService.Error(ex, worker.Description);
|
||||
}
|
||||
}
|
||||
|
||||
return "Done";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user