Skip to content

Commit

Permalink
feat: 使用 channel 显示表情
Browse files Browse the repository at this point in the history
  • Loading branch information
clansty committed Dec 28, 2024
1 parent 001a354 commit 9467e04
Show file tree
Hide file tree
Showing 3 changed files with 299 additions and 1 deletion.
241 changes: 241 additions & 0 deletions main/src/constants/qfaceChannelMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
export default {
'0': 3,
'1': 5,
'10': 7,
'100': 9,
'101': 11,
'102': 13,
'103': 15,
'104': 17,
'105': 19,
'106': 21,
'107': 23,
'108': 25,
'109': 27,
'11': 29,
'110': 31,
'111': 33,
'112': 35,
'114': 37,
'116': 39,
'118': 41,
'119': 45,
'12': 47,
'120': 49,
'121': 51,
'123': 53,
'124': 55,
'125': 57,
'129': 59,
'13': 61,
'137': 63,
'14': 65,
'144': 67,
'146': 69,
'147': 71,
'15': 73,
'16': 75,
'169': 77,
'171': 79,
'172': 81,
'173': 83,
'174': 85,
'175': 87,
'176': 89,
'177': 91,
'178': 93,
'179': 95,
'18': 97,
'181': 99,
'182': 101,
'183': 103,
'185': 105,
'187': 107,
'19': 109,
'2': 111,
'20': 113,
'201': 115,
'21': 117,
'212': 119,
'22': 121,
'23': 123,
'24': 125,
'25': 127,
'26': 129,
'262': 131,
'263': 133,
'264': 135,
'265': 137,
'266': 139,
'267': 141,
'268': 143,
'269': 145,
'27': 147,
'270': 149,
'271': 151,
'272': 153,
'273': 155,
'277': 157,
'28': 159,
'281': 161,
'282': 163,
'283': 165,
'284': 167,
'285': 169,
'286': 171,
'287': 173,
'289': 175,
'29': 177,
'293': 179,
'294': 181,
'295': 183,
'297': 185,
'298': 187,
'299': 189,
'3': 191,
'30': 193,
'300': 195,
'302': 197,
'303': 199,
'305': 201,
'306': 203,
'307': 205,
'31': 207,
'311': 209,
'312': 211,
'314': 213,
'317': 215,
'318': 217,
'319': 219,
'32': 221,
'320': 223,
'323': 225,
'324': 227,
'325': 229,
'326': 231,
'33': 233,
'332': 235,
'333': 237,
'334': 239,
'336': 241,
'337': 243,
'338': 245,
'339': 247,
'34': 249,
'341': 251,
'342': 253,
'343': 255,
'344': 257,
'345': 259,
'346': 261,
'347': 263,
'349': 265,
'35': 267,
'350': 269,
'351': 271,
'352': 273,
'353': 275,
'354': 277,
'355': 279,
'356': 281,
'357': 283,
'358': 285,
'359': 287,
'36': 289,
'360': 291,
'361': 293,
'362': 295,
'363': 297,
'364': 299,
'365': 301,
'366': 303,
'367': 305,
'368': 307,
'369': 309,
'37': 311,
'370': 313,
'371': 315,
'372': 317,
'373': 319,
'374': 321,
'375': 323,
'379': 325,
'38': 327,
'382': 329,
'384': 331,
'385': 333,
'386': 335,
'387': 337,
'388': 339,
'389': 341,
'39': 343,
'390': 345,
'391': 347,
'392': 349,
'393': 351,
'394': 353,
'395': 355,
'396': 357,
'397': 359,
'398': 361,
'399': 364,
'4': 366,
'400': 368,
'401': 370,
'402': 372,
'403': 374,
'404': 376,
'405': 378,
'406': 380,
'407': 383,
'408': 385,
'409': 387,
'41': 389,
'410': 391,
'411': 393,
'412': 395,
'413': 397,
'415': 399,
'416': 401,
'417': 403,
'419': 405,
'42': 407,
'420': 409,
'421': 411,
'422': 413,
'423': 415,
'424': 417,
'425': 419,
'426': 421,
'427': 423,
'428': 425,
'43': 427,
'46': 429,
'49': 431,
'5': 433,
'53': 435,
'56': 437,
'59': 439,
'6': 441,
'60': 443,
'63': 445,
'64': 447,
'66': 449,
'67': 451,
'7': 453,
'74': 455,
'75': 457,
'76': 459,
'77': 461,
'78': 463,
'79': 465,
'8': 467,
'85': 469,
'86': 471,
'89': 473,
'9': 475,
'96': 477,
'97': 479,
'98': 481,
'99': 483
}
8 changes: 7 additions & 1 deletion main/src/services/ForwardService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { NapCatClient } from '../client/NapCatClient';
import fsP from 'fs/promises';
import regExps from '../constants/regExps';
import qface from '../constants/qface';
import qfaceChannelMap from '../constants/qfaceChannelMap';

const NOT_CHAINABLE_ELEMENTS = ['flash', 'record', 'video', 'location', 'share', 'json', 'xml', 'poke'];
const IMAGE_MIMES = ['image/jpeg', 'image/png', 'image/apng', 'image/webp', 'image/gif', 'image/bmp', 'image/tiff', 'image/x-icon', 'image/avif', 'image/heic', 'image/heif'];
Expand Down Expand Up @@ -300,7 +301,12 @@ export default class ForwardService {
elem.text = '表情:' + elem.id;
}
}
message += `[<i>${helper.htmlEscape(elem.text)}</i>]`;
if (qfaceChannelMap[elem.id]) {
message += `[<i><a href="https://t.me/qq_face/${qfaceChannelMap[elem.id]}">${helper.htmlEscape(elem.text)}</a></i>]`;
}
else {
message += `[<i>${helper.htmlEscape(elem.text)}</i>]`;
}
break;
}
case 'bface': {
Expand Down
51 changes: 51 additions & 0 deletions main/tools/mkQFaceChannel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import fsP from 'fs/promises';
import qface from '../src/constants/qface';
import path from 'path';

const BOT_TOKEN = process.argv[2];
const DIR = process.argv[3];
const CHANNEL = -1002431668959;

(async () => {
for (const file of await fsP.readdir(DIR)) {
if (!file.endsWith('.webm')) continue;

const id = file.replace(/\.webm$/, '');
const name = qface[id];

const resTitle = await wrap429(`https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
chat_id: CHANNEL,
text: `${id}: ${name}`,
}),
});
const titleId = resTitle.result.message_id;
console.log(`'${id}': ${titleId}`);

const frm = new FormData();
frm.append('chat_id', CHANNEL.toString());
frm.append('reply_parameters', JSON.stringify({
message_id: titleId,
}));
frm.append('sticker', new Blob([await fsP.readFile(path.join(DIR, file))]), 'sticker.webm');
await wrap429(`https://api.telegram.org/bot${BOT_TOKEN}/sendSticker`, {
method: 'POST',
body: frm,
})
}
})();

const wrap429 = async (url: string, ext: any) => {
const req = await fetch(url, ext);
const res = await req.json();
if (res.ok) return res;
if (res.error_code !== 429) throw res;
const wait = res.parameters.retry_after;
console.log(`429: waiting ${wait} seconds`);
await new Promise(resolve => setTimeout(resolve, wait * 1000));
return wrap429(url, ext);
};

0 comments on commit 9467e04

Please sign in to comment.