Created
March 23, 2018 14:29
-
-
Save temamagic/4c9baca8579fb77ade2cd35f0ec8b142 to your computer and use it in GitHub Desktop.
Тестирование бота на JS с помощью TDLib Telegram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Автор https://t.me/johnnyidoe | |
*/ | |
const {Client} = require('tglib'); | |
const client = new Client({ | |
apiId: 'apiId', | |
apiHash: 'apiHash', | |
phoneNumber: '7XXXXXXXXXX' | |
}); | |
var answers = []; | |
const bot_id = XXXXXXXXX; // Bot ID | |
async function sendText(text){ | |
return await client._send({'@type': 'sendMessage','chat_id': bot_id,'input_message_content': {'@type': 'inputMessageText','text': {'@type': 'formattedText','text': text}}}); | |
} | |
async function main() { | |
client.on('_update', (update) => { | |
if ((update['@type'] == "updateChatLastMessage")&&(update.last_message!=undefined)) { | |
if (update.last_message.sender_user_id == bot_id) { | |
if (update.last_message.content['@type'] == 'messageText') { | |
console.log('Got update:', JSON.stringify(update, null, 2)) | |
answers.push(update.last_message.content.text.text); // ответ бота в очередь ответов | |
} | |
} | |
} | |
}) | |
client.on('_error', (update) => { console.error('Got error:', JSON.stringify(update, null, 2))}) | |
await client.connect(); | |
} | |
setInterval(function() { | |
// отправляем текст боту | |
sendText('test bot'); | |
},10000); | |
setInterval(function() { | |
if (answers.length>0) { | |
// читаем ответ из очереди ответов | |
answers.shift(); | |
} | |
},5000); | |
main(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment