Note: these directions come without much conversation, let the hacking begin!
Set up a Bot with @BotFather
Create a package.json
{
"name": "tg-bot-now",
"version": "0.2.0",
"description": "A sample Telegram bot via now.js",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"dependencies": {
"axios": "", "body-parser": "",
"express": "*",
"request": "^2.88.0",
"requests": "^0.3.0"
},
"license": "MIT"
}
Create a now.json
{
"version": 2,
"builds": [
{ "src": "app.js", "use": "@now/node-server" }
],
"routes": [
{ "src": "/(.*)", "dest": "app.js" }
]
}
Create an app.js
var express = require("express");
var app = express();
var request = require('request');
var bodyParser = require("body-parser");
const axios = require("axios");
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true
})
);
console.log("run");
function sendMessage(url, message, reply, res) {
axios.post(url, {
chat_id: message.chat.id,
text: reply
}).then(response => {
console.log("Message posted");
res.end("ok");
}).catch(error => {
console.log(error);
});
}
app.get('/', function(req, res) {
res.send('hello');
})
var bots = ["http://patbot1.duckdns.org/update", "http://coindexbot.duckdns.org:8080/update"]
app.post("/startbot", function(req, res) {
console.log("startbot");
const {
message
} = req.body;
let reply = "Welcome to Jare's bot bot";
if (message.text != undefined) {
if (message.text.toLowerCase().indexOf("statusupdate") != -1) {
reply = ""
for (var b in bots) {
r = request.get(bots[b], function(e, r, d) {
j = JSON.parse(d)
start = j.btc[0][0]
btc = j.btc[j.btc.length - 1][1]
end = j.btc[j.btc.length - 1][0]
diff = end - start
var s = diff / 1000
var m = s / 60
var h = m / 60
var d = h / 24
var y = d / 365
var apr = (j.btc[j.btc.length - 1][1] - j.btc[0][1]) / y
reply += r.request.uri.href.substring(0, r.request.uri.href.length - 7) + '\n'
reply += "Start: " + new Date(start) + '\n'
reply += "Now: " + new Date(end) + "\n"
reply += "BTC Notional traded recent memory: " + j.qty + "\n"
reply += "Current position: " + j.pos + "\n"
reply += "BTC % Delta: " + btc.toPrecision(4) + '%\n'
reply += "BTC % ROI / day projected: " + (apr / 365).toPrecision(4) + '%\n'
reply += "BTC % APR projected: " + (apr).toPrecision(4) + '%\n\n'
})
}
setTimeout(function() {
sendMessage(telegram_url, message, reply, res);
}, 2000);
} else {
res.end("ok");
}
}
});
let telegram_url = "https://api.telegram.org/bot" + process.env.key + "/sendMessage";
app.listen(8000, () => console.log("Telegram bot is listening on port 8000!"));
Note: it’s important to send a
res.end
in every case, or else your bot will get hung up on old messages it couldn’t process and no res.end
was sent – putting it in a forever loop of never sending a response!1. set your env key
export key="whateverbotfathergaveyou"
2. install
npm i
3. install now
npm i -g now
4. run now
now
5. output
Register your POST callback wtih Telegram’s grandmasters
curl -F "url=https://URL_FROM_NOW/startbot"
https://api.telegram.org/botYOUR_TELEGRAM_API_TOKEN/setWebhook
Congrats!
!! Click here to subscribe: http://eepurl.com/gIykNL
Be notified about new articles? Welcome to the Jarett Dunn email subscriber list! Each new signup will receive a link to download the Coindex Labs non-NDA teaser, which includes information about the value proposition for my organization – where we’re setting our sights first on a money-printing machine, then returning later as conquering heroes in order to then defeat the world’s greater humanitarian issues.
Remember to CLICK, subscribe: http://eepurl.com/gIykNL !!
(Disclaimer: The Author is the Chief Liquidity Officer at Coindex)
See this article in it’s original form on dunncreativess.github.io!
Try this bot on Telegram! https://t.me/JareBotBot!