Skip to content

Commit

Permalink
tested full example
Browse files Browse the repository at this point in the history
first push
  • Loading branch information
mortezaataiy committed Jan 22, 2019
1 parent 43d86be commit 22b2e7e
Show file tree
Hide file tree
Showing 6 changed files with 363 additions and 2 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,13 @@ typings/

# next.js build output
.next

# config file
config.js

# tdlib directories
user/
api_bot/

# ForNeVeR/tdlib.native's dll's
*.dll
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
# nodejs-tdlib-login-example
TDLib (Telegram Database library) authentication example by tdl (Node.js pakage)
[TDLib][tdlib-getting-started] (Telegram Database library) authentication example by [tdl](https://github.com/Bannerets/tdl) (Node.js pakage)
[tdlib-getting-started]: https://core.telegram.org/tdlib/getting-started

### Table of Contents

- [Getting started](#getting-started)
- [Test](#test)

---

<a name="getting-started"></a>
### Getting started

1. Build the binary (https://github.com/tdlib/td#building)
> You can also use prebuilt binaries: [tdlib.native](https://github.com/ForNeVeR/tdlib.native/releases)
2. `git clone https://github.com/mortezaataiy/nodejs-tdlib-login-example.git`
3. `npm install`
4. `npm start`
---

<a name="test"></a>
### Test

1...
5 changes: 5 additions & 0 deletions config-example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
API_ID: your-api-id, // get it from https://my.telegram.org/
API_HASH: 'your-api-hash', // get it from https://my.telegram.org/
BOT_TOKEN: 'your-api-bot-token' // get it from @BotFather in telegram
}
223 changes: 223 additions & 0 deletions nodejs-tdlib-login-example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
/*
* TDLib (Telegram Database library) authentication example
* with Node.js by [tdl](https://github.com/Bannerets/tdl) pakage.
*
* by mortezaataiy
*
*/

const { Client } = require('tdl')
const { TDLib } = require('tdl-tdlib-ffi')
const {API_ID, API_HASH, BOT_TOKEN} = require('./config.js');

const myDebug = false; // if you want to see logs change this to true
const showSommeryLogs = true;
function myLog(msg,msg2){
if(myDebug)
console.log(msg,msg2);
}
function sommeryLogs(msg,msg2){
if(showSommeryLogs)
console.log(msg,msg2);
}
function myError(msg,msg2){
if(myDebug)
console.error(msg,msg2);
}
var auth1state = null;
// auth1state = 'waitPhoneNumber'
// auth1state = 'waitAuthCode'
// auth1state = 'waitAuthPass'

const API_BOT_AUTH = {
type: 'bot',
token: BOT_TOKEN, // in document say write this line but
getToken: () => BOT_TOKEN // if this line dont set pakase get error and dont work
}

const tdlib = new TDLib()

// BotClient:
const BotClient = new Client(tdlib, {
apiId: API_ID,
apiHash: API_HASH,
databaseDirectory: 'api_bot/_td_database',
filesDirectory: 'api_bot/_td_files'
})
BotClient.connect()
BotClient.login(() => API_BOT_AUTH)
BotClient.on('error', e => myError('Bot error', e))
BotClient.on('update', u => {
if(u['_'] == 'updateNewMessage'
&& u.message
&& u.message.content
&& u.message.content.text
&& u.message.content.text.text){
var txt = u.message.content.text.text;
sommeryLogs('recived from api bot:',txt)
if(txt == '/start'){
if(!UserClientstarted){
startUserClient(u.message.chat_id);
UserClientstarted = true;
myLog('#######UserClientstart ')
}
}
if(txt && txt.indexOf('/send')>=0){
txt = txt.split(' c')[1];
recivedAuthFromUser(txt)
}
}
//else
myLog('Bot update', u)
})
sommeryLogs('api bot started. send /start in bot')
function botSendMessage(text,user_id) {
sommeryLogs('send with api bot:', text)
BotClient.invoke({
'_': "sendMessage",
chat_id: user_id,
disable_notification: false,
from_background: true,
input_message_content: {
'_': "inputMessageText",
text: {text: text},
disable_web_page_preview: false,
clear_draft: false
}
})
}

// UserClient:
var UserClientstarted = false;
var UserId = 0;
const UserClient = new Client(tdlib, {
apiId: API_ID,
apiHash: API_HASH,
databaseDirectory: 'user/_td_database',
filesDirectory: 'user/_td_files',
})
function startUserClient(user_id){ // user_id is user that start api bot
sommeryLogs('UserClient start');
UserClient.on('error', e => myError('UserClient error', e))
UserClient.on('update', u => {
if(u['_']=='updateAuthorizationState'){
if(u.authorization_state['_'] == 'authorizationStateWaitPhoneNumber'){
myLog('####### my authorizationStateWaitPhoneNumber');
auth1state = 'waitPhoneNumber';
getAuthFromUser(auth1state,user_id);
return;
}
else if(u.authorization_state['_'] == 'authorizationStateWaitCode'){
myLog('####### my authorizationStateWaitPhoneNumber');
auth1state = 'waitAuthCode';
getAuthFromUser(auth1state,user_id);
return;
}
else if(u.authorization_state['_'] == 'authorizationStateWaitPassword'){
myLog('####### my authorizationStateWaitPhoneNumber');
auth1state = 'waitAuthPass';
getAuthFromUser(auth1state,user_id);
return;
}
else if(u.authorization_state['_'] == 'authorizationStateReady'){
myLog('####### my authorizationStateReady %%%%%%%%%% :))))) ');
auth1state = 'Ready';
botSendMessage(auth1state,user_id);
var objTemp = {
'_': 'getMe'
};

UserClientAsyncInvode(objTemp)
.then(result => {
myLog('####### my GetMe $ output:', result);
UserId = result.id;
})

return;
}
else{
myError('######UserClient updateAuthentication', u);
}
return;
}
else if(UserId
&& u['_'] == 'updateNewMessage'
&& u.message.chat_id == UserId
&& u.message.content
&& u.message.content.text){
myLog("#####$$$$$$$$############",u.message.content.text)
if(u.message.content.text.text){

}
}
myError('UserClient update', u);
})

UserClient.connect()
UserClient.login(() => ({ type: 'user' }))
}
function UserClientInvode(objTemp){
myLog('#######invoke',objTemp)
UserClient.invoke(objTemp)
}
function UserClientAsyncInvode(objTemp){
return new Promise(function(res,rej){
async function AsyncInvode(objTemp){
myLog('#######invoke',objTemp)
res(await UserClient.invoke(objTemp));
}
AsyncInvode(objTemp);
})
}
function UserClientRecivedText(text,user_id){
sommeryLogs('recived text with UserClient:', text)
if(text.toUpperCase() == 'PING' && user_id == UserId)
UserClientSendMessage('Pong',UserId);
}


function UserClientSendMessage(text,user_id){
sommeryLogs('send with UserClient:', text)
UserClient.invoke({
_: 'sendMessage',
chat_id: user_id,
input_message_content: {
_: 'inputMessageText',
text: {
_: 'formattedText',
text: text
}
}
})
}
// send what auth need from api bot to user
function getAuthFromUser(auth1state, user_id){
botSendMessage(auth1state, user_id);
}
// proccess recived auth data from api bot
function recivedAuthFromUser(input){
var type = '';
var dataType = '';
switch (auth1state) {
case 'waitPhoneNumber':
type = 'setAuthenticationPhoneNumber';
dataType = 'phone_number';
break;
case 'waitAuthCode':
type = 'checkAuthenticationCode';
dataType = 'code';
break;
case 'waitAuthPass':
type = 'checkAuthenticationPassword';
dataType = 'password';
break;
default:

}
var objTemp = {
_: type
};
objTemp[dataType]= input;
UserClientInvode(objTemp);

}
82 changes: 82 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "nodejs-tdlib-login-example",
"version": "1.0.0",
"description": "",
"private": true,
"main": "nodejs-tdlib-login-example.js",
"scripts": {
"start": "node nodejs-tdlib-login-example.js"
},
"engines": {
"node": ">= 9.0.0"
},
"dependencies": {
"ffi-napi": "^2.4.3",
"ref-napi": "^1.4.0",
"tdl": "^6.0.0-rc.2",
"tdl-tdlib-ffi": "^0.2.0"
}
}

0 comments on commit 22b2e7e

Please sign in to comment.