-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbot.js
207 lines (164 loc) · 5.49 KB
/
bot.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// app dependency
const telegraf = require('telegraf');
const openrecord = require('openrecord/store/sqlite3');
const pinyin = require('pinyin');
const hepburn = require('hepburn');
const defaultSettings = require(__dirname + '/animeshot-example.json');
const customSettings = require(__dirname + '/animeshot.json');
const settings = Object.assign({}, defaultSettings, customSettings);
const i18n = require(__dirname + '/i18n.json');
settings.site.i18n = i18n;
// create bot
const bot = new telegraf(settings.bot.telegram);
// define db models
const db = new openrecord({
file: __dirname + '/database/animeshot.sqlite',
autoLoad: true,
autoConnect: true,
autoAttributes: true
});
db.Model('users', function () {
this.hasMany('shot', { model: 'shots', from: 'id', to: 'user_id' });
this.hasMany('bookmark', { model: 'bookmarks', from: 'id', to: 'user_id' });
});
db.Model('shots', function () {
this.belongsTo('user', { model: 'users', from: 'user_id', to: 'id' });
});
db.Model('bookmarks', function () {
this.belongsTo('user', { model: 'users', from: 'user_id', to: 'id' });
this.belongsTo('shot', { model: 'shots', from: 'shot_id', to: 'id' });
});
// helper function to romanize search input
function romanize (text) {
// romanize hanzi into phonetic notation
const textArray = pinyin(text, {
style: pinyin.STYLE_TONE2
});
// flatten array
// trim whitespace
// lowercase letters
// split non-han words
// convert kana into romaji
const textArrayFlatten = [];
textArray.forEach(a => {
if (a.length < 1) {
return;
}
let words = a[0].trim().split(' ');
words.forEach(s => {
if (s.length < 1) {
return;
}
if (!hepburn.containsKana(s)) {
textArrayFlatten.push(s.toLowerCase());
return;
}
textArrayFlatten.push(hepburn.fromKana(s).toLowerCase());
});
});
// join array into a string
const textRomanized = textArrayFlatten.join(' ');
return textRomanized;
}
// search for shots by romanized text
async function searchShots (text, limit = 0, offset = 0) {
const shotModel = db.Model('shots');
const shots = await shotModel.where({ romanized_like: text }).order('created', true).limit(limit, offset);
return shots;
}
async function findUserShots (user_id, limit = 0, offset = 0) {
const shotModel = db.Model('shots');
const shots = await shotModel.where({ user_id: user_id }).order('created', true).limit(limit, offset);
return shots;
}
async function findUserBookmarks (user_id, limit = 0, offset = 0) {
const bookModel = db.Model('bookmarks');
const books = await bookModel.where({ user_id: user_id }).order('created', true).limit(limit, offset).include('shot');
return books;
}
async function findUserByTelegramId (id) {
const userModel = db.Model('users');
const user = await userModel.where({ telegram_id: id }).first();
return user;
}
// bot logic
bot.on('inline_query', async ({ inlineQuery, answerInlineQuery }) => {
// query data
const offset = parseInt(inlineQuery.offset) || 0;
const search = inlineQuery.query;
let tid;
if (!inlineQuery.from || !inlineQuery.from.id) {
tid = -1;
} else {
tid = parseInt(inlineQuery.from.id);
}
await db.ready();
let shots;
if (search == 'my') {
// load user upload
const user = await findUserByTelegramId(tid);
if (user && user.id) {
shots = await findUserShots(user.id, settings.bot.result_count, offset);
}
} else if (search == 'bm') {
// load user bookmark
const user = await findUserByTelegramId(tid);
if (user && user.id) {
const books = await findUserBookmarks(user.id, settings.bot.result_count, offset);
// extract shot data from bookmark data
shots = [];
for (let i = 0; i < books.length; i++) {
const bookFlatten = books[i].toJson();
shots.push(bookFlatten.shot);
}
}
} else {
// search image by text
const text = romanize(search);
shots = await searchShots(text, settings.bot.result_count, offset);
}
// no result
if (!shots || shots.length == 0) {
return answerInlineQuery([], {
next_offset: '',
is_personal: settings.bot.is_personal,
cache_time: settings.bot.cache_time
});
}
// process data
let shotArray;
if (typeof shots.toJson == 'function') {
shotArray = shots.toJson();
} else {
shotArray = shots;
}
const results = shotArray.map((shot) => {
let output = {
type: 'photo',
id: shot.hash,
caption: shot.text
};
// legacy file support
if (!shot.legacy) {
output.photo_url = settings.site.meta.base_url + '/uploads/' + shot.hash.substring(shot.hash.length - 2) + '/' + shot.hash + '.1080p.jpg';
output.thumb_url = settings.site.meta.base_url + '/uploads/' + shot.hash.substring(shot.hash.length - 2) + '/' + shot.hash + '.720p.jpg';
output.photo_width = 1920;
output.photo_height = 1080;
} else {
output.photo_url = settings.site.meta.base_url + '/uploads/legacy/' + shot.hash.substring(shot.hash.length - 2) + '/' + shot.hash + '.1200.jpg';
output.thumb_url = settings.site.meta.base_url + "/uploads/legacy/" + shot.hash.substring(shot.hash.length - 2) + '/' + shot.hash + '.1200.jpg';
output.photo_width = 1200;
}
return output;
});
// result
return answerInlineQuery(results, {
next_offset: offset + settings.bot.result_count,
is_personal: settings.bot.is_personal,
cache_time: settings.bot.cache_time
});
});
bot.startPolling();
bot.catch((err) => {
console.error(err);
});