/
Bot.ts
168 lines (150 loc) · 4.09 KB
/
Bot.ts
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import invariant from 'invariant';
import type {
MachinatNode,
MachinatBot,
InitScopeFn,
DispatchWrapper,
} from '@machinat/core';
import Renderer from '@machinat/core/renderer';
import Queue from '@machinat/core/queue';
import Engine, { DispatchError } from '@machinat/core/engine';
import ModuleUtilitiesI from '@machinat/core/base/ModuleUtilities';
import { makeClassProvider } from '@machinat/core/service';
import { createChatJobs, createMulticastJobs } from './job';
import generalElementDelegate from './components/general';
import LineWorker from './Worker';
import LineChat from './Chat';
import { ConfigsI, PlatformUtilitiesI } from './interface';
import { LINE } from './constant';
import type {
LineSource,
LineSegmentValue,
LineComponent,
LineJob,
LineResult,
LineDispatchFrame,
LineDispatchResponse,
MessagingApiResult,
} from './types';
type LineBotOptions = {
providerId: string;
channelId: string;
accessToken: string;
maxRequestConnections?: number;
initScope?: InitScopeFn;
dispatchWrapper?: DispatchWrapper<LineJob, LineDispatchFrame, LineResult>;
};
/**
* @category Provider
*/
export class LineBot implements MachinatBot<LineChat, LineJob, LineResult> {
providerId: string;
channelId: string;
maxRequestConnections: number;
engine: Engine<
LineChat,
LineSegmentValue,
LineComponent<unknown>,
LineJob,
LineResult
>;
platform = LINE;
constructor({
providerId,
channelId,
accessToken,
maxRequestConnections = 100,
initScope,
dispatchWrapper,
}: LineBotOptions) {
invariant(accessToken, 'configs.accessToken should not be empty');
invariant(providerId, 'configs.providerId should not be empty');
invariant(channelId, 'configs.channelId should not be empty');
this.providerId = providerId;
this.channelId = channelId;
this.maxRequestConnections = maxRequestConnections;
const queue = new Queue<LineJob, LineResult>();
const worker = new LineWorker(accessToken, maxRequestConnections);
const renderer = new Renderer<LineSegmentValue, LineComponent<unknown>>(
LINE,
generalElementDelegate
);
this.engine = new Engine(
LINE,
renderer,
queue,
worker,
initScope,
dispatchWrapper
);
}
async start(): Promise<void> {
this.engine.start();
}
async stop(): Promise<void> {
this.engine.stop();
}
render(
source: string | LineSource | LineChat,
message: MachinatNode,
options?: { replyToken?: string }
): Promise<null | LineDispatchResponse> {
const channel =
source instanceof LineChat
? source
: typeof source === 'string'
? new LineChat(this.channelId, 'user', source)
: LineChat.fromMessagingSource(this.channelId, source);
return this.engine.render(
channel,
message,
createChatJobs(options && options.replyToken)
);
}
renderMulticast(
targets: string[],
message: MachinatNode
): Promise<null | LineDispatchResponse> {
return this.engine.render(null, message, createMulticastJobs(targets));
}
async makeApiCall<ResBody extends MessagingApiResult>(
method: 'GET' | 'POST' | 'PUT' | 'DELETE',
path: string,
body?: unknown
): Promise<ResBody> {
try {
const response = await this.engine.dispatchJobs(null, [
{ method, path, body, executionKey: undefined },
]);
return response.results[0].body as ResBody;
} catch (err) {
if (err instanceof DispatchError) {
throw err.errors[0];
}
throw err;
}
}
}
const BotP = makeClassProvider({
lifetime: 'singleton',
deps: [
ConfigsI,
{ require: ModuleUtilitiesI, optional: true },
{ require: PlatformUtilitiesI, optional: true },
],
factory: (
{ providerId, channelId, accessToken, maxRequestConnections },
moduleUtils,
platformUtils
) =>
new LineBot({
providerId,
channelId,
accessToken,
maxRequestConnections,
initScope: moduleUtils?.initScope,
dispatchWrapper: platformUtils?.dispatchWrapper,
}),
})(LineBot);
type BotP = LineBot;
export default BotP;