-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker.js
193 lines (187 loc) · 6.83 KB
/
worker.js
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
const TOKEN = '114514:TELEGRAM-BOT_TOKEN';
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
});
async function handleRequest(request) {
const update = await request.json();
const message = update.message;
const bot = new Telegram(TOKEN);
let smms
if (message) {
smms = new Smms(await USERTOKEN.get(message.from.id));
} else {
smms = new Smms();
}
// 命令
if (message !== undefined && message.text !== undefined) {
const command = message.text.split(' ')
switch (command[0]) {
case '/get':
const usertoken = await USERTOKEN.get(message.from.id)
return await bot.sendMessage(message.from.id, '`' + usertoken + '`', 'markdown', true, true, message.message_id)
case '/set':
if (command.length == 1) {
return await bot.sendMessage(message.from.id, 'Format: /set xxxxxx', 'markdown', true, true, message.message_id)
}
if (command[1].length == 32) {
await USERTOKEN.put(message.from.id, command[1])
return await bot.sendMessage(message.from.id, 'API token saved.', 'markdown', true, true, message.message_id)
}
return await bot.sendMessage(message.from.id, 'API token length invalid.', 'markdown', true, true, message.message_id)
case '/del':
await USERTOKEN.put(message.from.id, command[1])
return await bot.sendMessage(message.from.id, 'API token deleted.', 'markdown', true, true, message.message_id)
default:
return await bot.sendMessage(message.from.id, 'Unknown command', 'markdown', true, true, message.message_id)
}
}
// 文件形式的图片
if (message !== undefined && message.document !== undefined) {
if (!message.document.mime_type.startsWith('image/')) {
return await bot.sendMessage(message.from.id, 'File has an invalid extension.', 'markdown', true, true, message.message_id)
}
const fileID = message.document.file_id
const filePath = await bot.getFile(fileID)
const filePathJSON = await filePath.json()
const url = `https://api.telegram.org/file/bot${TOKEN}/${filePathJSON.result.file_path}`
const image = await fetch(url)
const result = await smms.upload(await image.arrayBuffer())
const resultJSON = await result.json()
if (resultJSON.success) {
return await bot.sendMessage(message.from.id, '`' + resultJSON.data.url + '`', 'markdown', true, true, message.message_id, {
inline_keyboard: [[{
text: resultJSON.data.hash,
callback_data: resultJSON.data.hash
}]]
})
}
return await bot.sendMessage(message.from.id, '`' + resultJSON.message + '`', 'markdown', true, true, message.message_id)
}
// 图片
if (message !== undefined && message.photo !== undefined) {
const fileID = message.photo[message.photo.length - 1].file_id
const filePath = await bot.getFile(fileID)
const filePathJSON = await filePath.json()
const url = `https://api.telegram.org/file/bot${TOKEN}/${filePathJSON.result.file_path}`
const image = await fetch(url)
const result = await smms.upload(await image.arrayBuffer())
const resultJSON = await result.json()
if (resultJSON.success) {
return await bot.sendMessage(message.from.id, '`' + resultJSON.data.url + '`', 'markdown', false, false, message.message_id, {
inline_keyboard: [[{
text: resultJSON.data.hash,
callback_data: resultJSON.data.hash
}]]
})
}
return await bot.sendMessage(message.from.id, '`' + resultJSON.message + '`', 'markdown', false, false, message.message_id)
}
// Callback 删除图片
if (update.callback_query !== undefined) {
smms = new Smms(await USERTOKEN.get(update.callback_query.from.id));
await smms.delete(update.callback_query.data)
return await bot.editMessageText(update.callback_query.message.chat.id, update.callback_query.message.message_id, null, "Photo Deleted!")
}
return new Response()
}
class Telegram {
constructor(token) {
this.api = 'https://api.telegram.org/bot' + token;
}
async sendMessage(chat_id, text, parse_mode, disable_web_page_preview, disable_notification,
reply_to_message_id, reply_markup) {
return await fetch(this.api + '/sendMessage', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
chat_id: chat_id,
text: text,
parse_mode: parse_mode,
disable_web_page_preview: disable_web_page_preview,
disable_notification: disable_notification,
reply_to_message_id: reply_to_message_id,
reply_markup: reply_markup
})
})
}
async editMessageText(chat_id, message_id, inline_message_id, text, parse_mode, disable_web_page_preview,
reply_markup) {
return await fetch(this.api + '/editMessageText', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
chat_id: chat_id,
message_id: message_id,
inline_message_id: inline_message_id,
text: text,
parse_mode: parse_mode,
disable_web_page_preview: disable_web_page_preview,
reply_markup: reply_markup
})
})
}
async getFile(file_id) {
return await fetch(this.api + '/getFile', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
file_id: file_id
})
})
}
}
class Smms {
constructor(token) {
this.token = token;
this.api = 'https://sm.ms/api/v2'
}
async upload(image) {
const f = fd(image);
const headers = new Headers({
'Content-Type': 'multipart/form-data; boundary=-114514',
'User-Agent': 'sm_ms_bot/cloudflareworkers (https://github.com/imlonghao/smms-bot)'
});
if (this.token !== null) {
headers.append('Authorization', this.token)
}
return await fetch(this.api + '/upload', {
method: 'POST',
headers: headers,
body: f
})
}
async delete(hash) {
const headers = new Headers({
'User-Agent': 'sm_ms_bot/cloudflareworkers (https://github.com/imlonghao/smms-bot)'
});
if (this.token !== null) {
headers.append('Authorization', this.token)
}
return await fetch(this.api + '/delete/' + hash, {
headers: headers
})
}
}
var _appendBuffer = function (buffer1, buffer2) {
var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(new Uint8Array(buffer1), 0);
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
return tmp.buffer;
};
function fd(image) {
const enc = new TextEncoder();
let txt = '---114514\r\n';
txt += 'Content-Disposition: form-data; name="smfile"; filename="uploaded_by_sm_ms_bot"\r\n';
txt += '\r\n';
const p1 = enc.encode(txt);
txt = '\r\n';
txt += '---114514--\r\n';
const p2 = enc.encode(txt);
return _appendBuffer(p1, _appendBuffer(image, p2))
}