Add JavaScript example
This commit is contained in:
parent
0ff0e1003d
commit
69cb9028b8
11
example/javascript/README.md
Normal file
11
example/javascript/README.md
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#### Requirements
|
||||||
|
|
||||||
|
- node.js v9.0.0+
|
||||||
|
- libtdjson binary
|
||||||
|
|
||||||
|
#### Run
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ npm install
|
||||||
|
$ node example.js
|
||||||
|
```
|
63
example/javascript/example.js
Normal file
63
example/javascript/example.js
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
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']],
|
||||||
|
'td_set_log_file_path' : ['int' , ['string']],
|
||||||
|
'td_set_log_verbosity_level' : ['void' , ['int']],
|
||||||
|
'td_set_log_fatal_error_callback': ['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()
|
17
example/javascript/package.json
Normal file
17
example/javascript/package.json
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
{
|
||||||
|
"name": "tdlib-js-example",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"private": true,
|
||||||
|
"main": "example.js",
|
||||||
|
"scripts": {
|
||||||
|
"start": "node example.js"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 9.0.0"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"ffi-napi": "^2.4.3",
|
||||||
|
"ref-napi": "^1.4.0"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user