Skip to content

Instantly share code, notes, and snippets.

@temamagic
Created March 23, 2018 14:29
Show Gist options
  • Save temamagic/4c9baca8579fb77ade2cd35f0ec8b142 to your computer and use it in GitHub Desktop.
Save temamagic/4c9baca8579fb77ade2cd35f0ec8b142 to your computer and use it in GitHub Desktop.
Тестирование бота на JS с помощью TDLib Telegram
/*
* Автор 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