Skip to content

Commit

Permalink
不用自己构建 tdlib了...
Browse files Browse the repository at this point in the history
  • Loading branch information
krau committed Jun 18, 2024
1 parent 63cfce1 commit a542e70
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
Binary file modified bun.lockb
Binary file not shown.
26 changes: 15 additions & 11 deletions helpers/tdlib/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
const path = require('path')
const tdl = require('tdl')
const { getTdjson } = require('prebuilt-tdlib')

const tdDirectory = path.resolve(__dirname, 'data')

tdl.configure({ libdir: tdDirectory, verbosityLevel: 0 })
tdl.configure({
tdjson: getTdjson(),
})

const tdDirectory = path.join(__dirname, 'data')

const client = tdl.createClient({
apiId: process.env.TELEGRAM_API_ID,
Expand All @@ -20,13 +24,13 @@ const client = tdl.createClient({
client.on('error', console.error)
client.loginAsBot(process.env.BOT_TOKEN)

function sendMethod (method, parm) {
function sendMethod(method, parm) {
return new Promise((resolve, reject) => {
client.invoke(Object.assign({ _: method }, parm)).then(resolve).catch(resolve)
})
}

function getUser (userID) {
function getUser(userID) {
return new Promise((resolve, reject) => {
sendMethod('getUser', {
user_id: userID
Expand All @@ -46,7 +50,7 @@ function getUser (userID) {
})
}

function getSupergroup (supergroupID) {
function getSupergroup(supergroupID) {
return new Promise((resolve, reject) => {
sendMethod('getSupergroup', {
supergroup_id: supergroupID
Expand All @@ -61,7 +65,7 @@ function getSupergroup (supergroupID) {
})
}

function getChat (chatID) {
function getChat(chatID) {
return new Promise((resolve, reject) => {
sendMethod('getChat', {
chat_id: chatID
Expand Down Expand Up @@ -112,7 +116,7 @@ function getChat (chatID) {
})
}

function decodeWaveform (wf) {
function decodeWaveform(wf) {
const bitsCount = wf.length * 8
const valuesCount = ~~(bitsCount / 5)

Expand All @@ -130,15 +134,15 @@ function decodeWaveform (wf) {
const lastByteIdx = ~~((lastIdx * 5) / 8)
const lastBitShift = (lastIdx * 5) % 8
const lastValue =
lastByteIdx === wf.length - 1
? wf[lastByteIdx]
: wf.readUInt16LE(lastByteIdx)
lastByteIdx === wf.length - 1
? wf[lastByteIdx]
: wf.readUInt16LE(lastByteIdx)
result[lastIdx] = (lastValue >> lastBitShift) & 0b11111

return result
}

function getMessages (chatID, messageIds) {
function getMessages(chatID, messageIds) {
const tdlibMessageIds = messageIds.map((id) => id * Math.pow(2, 20))

return new Promise((resolve, reject) => {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"ioredis": "^5.4.1",
"limax": "^4.1.0",
"mongoose": "^5.13.2",
"prebuilt-tdlib": "^0.1008030.0",
"prom-client": "^12.0.0",
"sharp": "^0.30.5",
"tdl": "^8.0.0",
Expand All @@ -42,4 +43,4 @@
"eslint-plugin-promise": "^4.3.1",
"eslint-plugin-standard": "^4.0.1"
}
}
}

0 comments on commit a542e70

Please sign in to comment.