-
Notifications
You must be signed in to change notification settings - Fork 39
/
bot.ts
273 lines (246 loc) · 8.97 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import { arrayBufferToBase64, Bot, Context, Dict, h, Quester, Schema, Time, Universal } from '@satorijs/satori'
import * as Telegram from './types'
import { decodeGuildMember, decodeUser } from './utils'
import { TelegramMessageEncoder } from './message'
import { HttpServer } from './server'
import { HttpPolling } from './polling'
import FileType from 'file-type'
export class SenderError extends Error {
constructor(args: Dict<any>, url: string, retcode: number, selfId: string) {
super(`Error when trying to send to ${url}, args: ${JSON.stringify(args)}, retcode: ${retcode}`)
Object.defineProperties(this, {
name: { value: 'SenderError' },
selfId: { value: selfId },
code: { value: retcode },
args: { value: args },
url: { value: url },
})
}
}
export interface TelegramResponse {
ok: boolean
result: any
}
export class TelegramBot<C extends Context = Context, T extends TelegramBot.Config = TelegramBot.Config> extends Bot<C, T> {
static MessageEncoder = TelegramMessageEncoder
static inject = ['http']
http: Quester
file: Quester
internal: Telegram.Internal
local?: boolean
server?: string
constructor(ctx: C, config: T) {
super(ctx, config, 'telegram')
this.selfId = config.token.split(':')[0]
this.local = config.files.local
this.http = this.ctx.http.extend({
...config,
endpoint: `${config.endpoint}/bot${config.token}`,
})
this.file = this.ctx.http.extend({
...config,
endpoint: `${config.files.endpoint || config.endpoint}/file/bot${config.token}`,
})
this.internal = new Telegram.Internal(this)
if (config.protocol === 'server') {
ctx.plugin(HttpServer, this)
} else if (config.protocol === 'polling') {
ctx.plugin(HttpPolling, this)
}
const selfUrl: string = config['selfUrl'] || ctx.get('server')?.config.selfUrl
if (config.files.server ?? selfUrl) {
const route = `/telegram/${this.selfId}`
this.server = selfUrl + route
ctx.get('server').get(route + '/:file+', async ctx => {
const { data, mime } = await this.$getFile(ctx.params.file)
ctx.set('content-type', mime)
ctx.body = data
})
}
}
async initialize(callback: (bot: this) => Promise<void>) {
await this.getLogin()
await callback(this)
this.logger.debug('connected to %c', 'telegram:' + this.selfId)
this.online()
}
async deleteMessage(chat_id: string, message_id: string | number) {
message_id = +message_id
await this.internal.deleteMessage({ chat_id, message_id })
}
async editMessage(chat_id: string, message_id: string | number, content: h.Fragment): Promise<void> {
message_id = +message_id
const payload: Telegram.EditMessageTextPayload = {
chat_id,
message_id,
parse_mode: 'html',
}
payload.text = h.normalize(content).join('')
await this.internal.editMessageText(payload)
}
static adaptGroup(data: Telegram.Chat): Universal.Guild {
data['guildId'] = data.id + ''
data['guildName'] = data.title
return data as any
}
async getGuild(chat_id: string): Promise<Universal.Guild> {
const data = await this.internal.getChat({ chat_id })
return TelegramBot.adaptGroup(data)
}
async getGuildMember(chat_id: string, user_id: string | number) {
user_id = +user_id
if (Number.isNaN(user_id)) return null
const data = await this.internal.getChatMember({ chat_id, user_id })
const member = decodeGuildMember(data)
await this.setAvatarUrl(member.user)
return member
}
async getGuildMemberList(chat_id: string) {
const data = await this.internal.getChatAdministrators({ chat_id })
const members = data.map(decodeGuildMember)
return { data: members }
}
async kickGuildMember(chat_id: string, user_id: string | number, permanent?: boolean) {
user_id = +user_id
await this.internal.banChatMember({
chat_id,
user_id,
until_date: Date.now() + (permanent ? 0 : Time.minute),
revoke_messages: true,
})
}
setGroupLeave(chat_id: string) {
return this.internal.leaveChat({ chat_id })
}
async handleGuildMemberRequest(messageId: string, approve: boolean, comment?: string) {
const [chat_id, user_id] = messageId.split('@')
const method = approve ? 'approveChatJoinRequest' : 'declineChatJoinRequest'
const success = await this.internal[method]({ chat_id, user_id: +user_id })
if (!success) throw new Error(`handel guild member request field ${success}`)
}
async getLogin() {
const data = await this.internal.getMe()
const user = decodeUser(data)
await this.setAvatarUrl(user)
this.user = user
return this.toJSON()
}
async $getFile(filePath: string) {
if (this.local) {
return await this.ctx.http.file(filePath)
} else {
return await this.file.file(`/${filePath}`)
}
}
async $getFileFromId(file_id: string) {
try {
const file = await this.internal.getFile({ file_id })
return await this.$getFileFromPath(file.file_path)
} catch (e) {
this.logger.warn('get file error', e)
}
}
async $getFileFromPath(filePath: string) {
if (this.server) {
return { src: `${this.server}/${filePath}` }
}
let { mime, data } = await this.$getFile(filePath)
if (mime === 'application/octet-stream') {
mime = (await FileType.fromBuffer(data))?.mime
}
const base64 = `data:${mime};base64,` + arrayBufferToBase64(data)
return { src: base64 }
}
private async setAvatarUrl(user: Universal.User) {
const { photos: [avatar] } = await this.internal.getUserProfilePhotos({ user_id: +user.id })
if (!avatar) return
const { file_id } = avatar[avatar.length - 1]
const file = await this.internal.getFile({ file_id })
if (this.server) {
user.avatar = `${this.server}/${file.file_path}`
} else {
const { endpoint } = this.file.config
user.avatar = `${endpoint}/${file.file_path}`
}
}
async getUser(userId: string, guildId?: string) {
const data = await this.internal.getChat({ chat_id: userId })
if (!data.photo?.big_file_id && !data.photo?.small_file_id) return decodeUser(data)
const { src } = await this.$getFileFromId(data.photo?.big_file_id || data.photo?.small_file_id)
return {
...decodeUser(data),
avatar: src,
}
}
async createDirectChannel(id: string) {
return { id, type: Universal.Channel.Type.DIRECT }
}
async updateCommands(commands: Universal.Command[]) {
if (!this.config.slash) return
const result = {} as Record<string, Telegram.BotCommand[]>
for (const cmd of commands) {
const { name, description } = cmd
const languages = {} as Record<string, string>
for (const locale in description) {
if (!locale || !description[locale]) continue
const lang = locale.slice(0, 2)
languages[lang] ||= description[locale]
}
for (const lang in languages) {
result[lang] ??= []
result[lang].push({ command: name, description: languages[lang] })
}
}
for (const lang in result) {
await this.internal.setMyCommands({
commands: result[lang],
language_code: lang,
})
}
await this.internal.setMyCommands({
commands: commands.map(({ name, description }) => ({
command: name,
description: description[''] || name,
})),
})
}
}
export namespace TelegramBot {
export interface BaseConfig extends Quester.Config {
protocol: 'server' | 'polling'
token: string
files?: Config.Files
slash?: boolean
}
export type Config = BaseConfig & (HttpServer.Options | HttpPolling.Options)
export namespace Config {
export interface Files {
endpoint?: string
local?: boolean
server?: boolean
}
}
export const Config: Schema<Config> = Schema.intersect([
Schema.object({
token: Schema.string().description('机器人的用户令牌。').role('secret').required(),
protocol: process.env.KOISHI_ENV === 'browser'
? Schema.const('polling').default('polling')
: Schema.union(['server', 'polling']).description('选择要使用的协议。').required(),
}),
Schema.union([
HttpServer.Options,
HttpPolling.Options,
]).description('推送设置'),
Schema.object({
slash: Schema.boolean().description('是否启用斜线指令。').default(true),
}).description('功能设置'),
Quester.createConfig('https://api.telegram.org'),
Schema.object({
files: Schema.object({
endpoint: Schema.string().description('文件请求的终结点。'),
local: Schema.boolean().description('是否启用 [Telegram Bot API](https://github.com/tdlib/telegram-bot-api) 本地模式。'),
server: Schema.boolean().description('是否启用文件代理。若开启将会使用 `selfUrl` 进行反代,否则会下载所有资源文件 (包括图片、视频等)。当配置了 `selfUrl` 时将默认开启。'),
}),
}).hidden(process.env.KOISHI_ENV === 'browser').description('文件设置'),
] as const)
}