tdlight/example/javascript/example.js
levlam 384e96b79e Remove td_log methods from JavaScriot example.
GitOrigin-RevId: 3a0e12c648f154efbb6fb5dc212f1e517c58b838
2019-03-07 01:32:56 +03:00

61 lines
1.4 KiB
JavaScript

const ffi = require('ffi-napi')
const ref = require('ref-napi')
function buildQuery (query) {
const buffer = Buffer.from(JSON.stringify(query) + '\0', 'utf-8')
buffer.type = ref.types.CString
return buffer
}
const PATH_TO_LIBRARY_FILE = 'libtdjson'
const tdlib = ffi.Library(
PATH_TO_LIBRARY_FILE,
{
'td_json_client_create' : ['pointer', []],
'td_json_client_send' : ['void' , ['pointer', 'string']],
'td_json_client_receive' : ['string' , ['pointer', 'double']],
'td_json_client_execute' : ['string' , ['pointer', 'string']],
'td_json_client_destroy' : ['void' , ['pointer']],
}
)
// Create client
const client = tdlib.td_json_client_create()
function send (query) {
tdlib.td_json_client_send(client, buildQuery(query))
}
function execute (query) {
return JSON.parse(
tdlib.td_json_client_execute(client, buildQuery(query))
)
}
function receive () {
const timeout = 2
return JSON.parse(
tdlib.td_json_client_receive(client, timeout)
)
}
function destroy () {
tdlib.td_json_client_destroy(client)
}
// Testing execute
console.log('execute', execute({
'@type': 'getTextEntities',
'text': '@telegram /test_command https://telegram.org telegram.me'
}))
// Testing send
send({ '@type': 'getAuthorizationState' })
// Testing receive
console.log('receive', receive())
// Destroy client
destroy()