Skip to content

Instantly share code, notes, and snippets.

@fayazara
Last active May 13, 2020 10:16
Show Gist options
  • Select an option

  • Save fayazara/2caabbb8519f2a93a4f8912dc5771507 to your computer and use it in GitHub Desktop.

Select an option

Save fayazara/2caabbb8519f2a93a4f8912dc5771507 to your computer and use it in GitHub Desktop.
Get Bitcoin price and send data to a telegram bot
const express = require("express");
const app = express();
const axios = require("axios");
app.get("/send-btc-price", async (request, response) => {
try {
const btc = await axios.get('https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT');
var str = `<b>Bitcoin Price</b>: $${parseInt(btc.data.price).toFixed(2)}`;
var telegram_url = encodeURI(
`https://api.telegram.org/bot${process.env.BOT_TOKEN}/sendMessage?chat_id=${process.env.USER_ID}&text=${str}&parse_mode=HTML`
);
const { data } = await axios.get(telegram_url);
if (data.ok) {
response.send({
status: "Message sent succesfully",
details: data
});
} else throw "Something went wrong, please try again";
} catch (err) {
response.status(400).send({
status: "Failed",
details: err
});
}
});
// listen for requests :)
const listener = app.listen(process.env.PORT, () => {
console.log("Your app is listening on port " + listener.address().port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment