From 6a00759424b16222ee312f4769581b5a4bddfcf9 Mon Sep 17 00:00:00 2001 From: Piyush Dixit <79581397+PiyushDixit96@users.noreply.github.com> Date: Wed, 12 May 2021 20:39:31 +0530 Subject: Update index.js --- index.js | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index b085ef2..54bc2f2 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ #!/usr/bin/env node +const Request = require('request-promise'); const dotenv = require('dotenv'); dotenv.config(); @@ -43,11 +44,11 @@ try { }, 60000).then(ws => { console.log(`${event} - Monitoring Spot User Order Data for binance.com`); - toTelegram(`Binance Spot Order Monitor Started\nthis message shows that you or heroku(if your are using) restart the bot.`) + sendMessage(`Binance Spot Order Monitor Started\nthis message shows that you or heroku(if your are using) restart the bot.`) }) } catch (err) { console.error(`${event} - ${err}`) - toTelegram(err.toString()) + sendMessage(err.toString()) } function fixFloat(floatNum, Precision = 8) { @@ -82,22 +83,39 @@ function process_data(data) { } else { txt = `⚠️ ⚠️ ⚠️\nUndefined\nExecution Type: ${data.executionType}\nOrder Status ${data.orderStatus}\nFull Details:\n${msg}` } - toTelegram(txt) + sendMessage(txt) } } -function toTelegram(text) { - let parse_mode = { +function sendMessage(text) { + let params = { + chat_id: chat_id, + text: text, parse_mode: 'html' }; - bot.sendMessage(chat_id, text, parse_mode) - .then(message => { - //console.log(message); - if (message['ok'] === true) { - console.log(`${event} - Message send to ${message['result']['chat']['first_name']} via Telegram`) - } else if (message['ok'] === false) { - console.log(`${event} - something went wrong while sending message to telegram see detailed error below.`) - console.error(message) + let options = { + uri: 'https://api.telegram.org/bot' + token + '/' + 'sendMessage', + qs: params, + simple: false, + resolveWithFullResponse: true, + forever: true + }; + return Request(options) + .then(resp => { + if (resp.statusCode !== 200) { + throw new Error(resp.statusCode + ':\n' + resp.body); + } + let updates = JSON.parse(resp.body); + if (updates.ok) { + console.log("Message send via Telegram") + return updates; + } else { + console.log(`something went wrong while sending message to telegram see detailed error below.`) + console.error(updates) + return null; } + }) + .catch(error => { + throw error; }); } -- cgit v1.2.3