-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bot.cs
113 lines (96 loc) · 4.26 KB
/
Bot.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using Nagase.Data;
using Nagase.Services;
using Nagase.Threads;
using NLog;
namespace Nagase;
public class Bot
{
static Bot? instance;
static readonly object instanceLock = new();
readonly Logger logger;
List<ChatData> chatDataList;
Bot()
{
logger = LogManager.GetCurrentClassLogger();
chatDataList = new List<ChatData>();
}
public static Bot Instance
{
get
{
if (instance == null)
{
lock (instanceLock)
{
instance ??= new Bot();
}
}
return instance;
}
}
public void Start()
{
TelegramService.Instance.Init(HandleNewChat, HandleReply);
var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
OpenAIAPIThread.Instance.InitAndStart(cancellationToken, HandleOpenAIResponse);
TelegramAPIThread.Instance.InitAndStart(HandleTelegramSendMessageResponse, HandleTelegramEditMessageResponse,
cancellationToken);
ChatDataManageThread.Instance.InitAndStart(cancellationToken);
OpenAIAPIThread.Instance.Join();
TelegramAPIThread.Instance.Join();
ChatDataManageThread.Instance.Join();
}
void HandleOpenAIResponse(OpenAIChatCompletionResponse response)
{
TelegramAPIThread.Instance.AddEditMessageRequest(new TelegramEditMessageRequest(response.Request.ChatID,
response.Request.TelegramChatID, response.Request.TelegramMessageID, response.Text));
}
void HandleTelegramSendMessageResponse(TelegramSendMessageResponse response)
{
if (response.Request.IsErrorMessage)
{
return;
}
OpenAIAPIThread.Instance.AddRequest(new OpenAIChatCompletionRequest(response.Request.ChatID.Value,
response.Request.TelegramChatId, response.TelegramMessageID,
ChatDataManageThread.Instance.GenerateOpenAIChatMessages(response.Request.ChatID.Value)));
ChatDataManageThread.Instance.AddChatMessage(response.Request.ChatID.Value,
new ChatData.Message(string.Empty, false, response.TelegramMessageID, response.TelegramReplyToMessageID));
}
void HandleTelegramEditMessageResponse(TelegramEditMessageResponse response)
{
ChatDataManageThread.Instance.EditMessage(response.Request.ChatID, response.Request.TelegramMessageID,
response.Request.Text);
}
void HandleNewChat(long telegramChatID, int telegramMessageID, string senderFullName, string senderUsername,
long senderUserID, string text)
{
logger.Info(
$"Recived a new chat from {senderFullName} (Username : {senderUsername} ID : {senderUserID})\nMessage : {text}");
var id = ChatDataManageThread.Instance.CreateChatMessage(new ChatData.Message(text, true, telegramMessageID));
TelegramAPIThread.Instance.AddSendMessageRequest(new TelegramSendMessageRequest(id,
telegramChatID, "请等待……⏳", telegramMessageID));
}
void HandleReply(long telegramChatID, int telegramMessageID, int telegramReplyToMessageID, string senderFullName,
string senderUsername, long senderUserID, string text)
{
var chatID = ChatDataManageThread.Instance.GetIDByMessageID(telegramReplyToMessageID);
if (chatID.HasValue)
{
logger.Info(
$"Recived a reply from {senderFullName} (Username : {senderUsername} ID : {senderUserID})\nMessage : {text}");
ChatDataManageThread.Instance.AddChatMessage(chatID.Value,
new ChatData.Message(text, true, telegramMessageID, telegramReplyToMessageID));
TelegramAPIThread.Instance.AddSendMessageRequest(new TelegramSendMessageRequest(chatID.Value,
telegramChatID, "请等待……⏳", telegramMessageID));
}
else
{
logger.Warn(
$"{senderFullName} (Username : {senderUsername} ID : {senderUserID}) try to reply to a nonexistent talk.");
TelegramAPIThread.Instance.AddSendMessageRequest(new TelegramSendMessageRequest(null, telegramChatID,
"对话已超时。\n请重新开始新的对话。", telegramMessageID));
}
}
}