Commit f1a03c51 authored by Melroy van den Berg's avatar Melroy van den Berg
Browse files

Fix index bot

parent 3aa922ae
......@@ -4,16 +4,19 @@ module.exports = {
node: true,
mocha: true
},
extends: 'eslint:recommended',
extends: [
'standard'
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly'
SharedArrayBuffer: 'readonly',
TelegramSecretHash: 'writable'
},
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module'
},
rules: {
"no-return-assign": "off", // Allow assignment operator in return statement
'no-return-assign': 'off' // Allow assignment operator in return statement
}
}
MIT License
Copyright (c) 2020 Melroy van den Berg
Copyright (c) 2020-2021 Melroy van den Berg
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
......
This diff is collapsed.
......@@ -25,19 +25,19 @@
"author": "Melroy van den Berg <melroy@melroy.org>",
"license": "MIT",
"dependencies": {
"axios": "^0.21.0",
"cors": "^2.8.5",
"axios": "^0.21.1",
"cron": "^1.8.2",
"express": "^4.17.1",
"fast-csv": "^4.1.3",
"node-telegram-bot-api": "^0.40.0",
"yaml": "^1.9.2"
"fast-csv": "^4.3.6",
"node-telegram-bot-api": "^0.52.0",
"yaml": "^1.10.2"
},
"devDependencies": {
"eslint": "^7.12.1",
"eslint-config-standard": "^16.0.1",
"eslint": "^7.23.0",
"eslint-config-standard": "^16.0.2",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.2"
"eslint-plugin-promise": "^4.3.1",
"eslint-plugin-standard": "^4.1.0"
}
}
......@@ -17,14 +17,14 @@ class Communicate {
this.prevLastCrossTime = 0
this.sendMessageOptions = { parse_mode: 'markdown', disable_web_page_preview: true }
// Notify channel about Bot booting-up (commented-out for now)
// this.sendTelegramMessage('Starting-up Bot... 🤓')
this.sendTelegramMessage('Starting-up Bot... 🤓')
}
/**
* Send message to Telegram channel about volatility (only when needed)
*
*
* TODO: Send link towards tradingview or something?
*
*
* @param {Object} result Volatility result structure
*/
sendVolatilityUpdate (result) {
......@@ -63,9 +63,9 @@ class Communicate {
/**
* Send message to Telegram channel about stock market (only when needed)
*
*
* TODO: Send link towards tradingview or something?
*
*
* @param {Object} result Stock market result structure
*/
sendStockMarketUpdate (result) {
......
......@@ -29,18 +29,18 @@ class DataProcessor {
*/
processVolatility (volatilityData) {
const result = {
alert: false,
level: AlertLevels.NO_ALERT,
percentage: 0,
latest_close_price: 0,
latest_time: 0,
all_points: false,
dual_alert: {
alert: false,
level: AlertLevels.NO_ALERT,
percentage: 0,
latest_close_price: 0,
latest_time: 0,
all_points: false,
dual_alert: {
alert: false,
level: AlertLevels.NO_ALERT,
percentage: 0
}
percentage: 0
}
}
// NYSE Marker opening hours: 9:30 - 16:00 = 6.5 hours open = 390 min.
// Max data points = 390 min. / 5 .min interval = 78
......@@ -111,7 +111,7 @@ class DataProcessor {
// Strip down the data series to just what is needed for warming-up fase + data period
let nrOfDataPoints = this.warmupPeriod + this.dataPeriod
let firstIndexUsed = (sp500Data.length -1) - (this.dataPeriod -1)
let firstIndexUsed = (sp500Data.length - 1) - (this.dataPeriod - 1)
if (sp500Data.length < nrOfDataPoints) {
console.error('ERROR: Not enough data received from API')
nrOfDataPoints = sp500Data.length
......
// NTBA = node-telegram-bot-api fixes
process.env['NTBA_FIX_319'] = 1
process.env['NTBA_FIX_350'] = 1
process.env.NTBA_FIX_319 = 1
process.env.NTBA_FIX_350 = 1
// Constants
const port = process.env.PORT || 3008
const express = require('express')
const cors = require('cors')
const fs = require('fs')
const YAML = require('yaml')
const Fetcher = require('./fetcher')
const DataProcessor = require('./dataProcessor')
const CronJob = require('cron').CronJob
const crypto = require('crypto')
global.TelegramSecretHash = crypto.randomBytes(20).toString('hex')
const TelegramBot = require('node-telegram-bot-api')
const express = require('express')
// const cors = require('cors')
const Communicate = require('./communicate')
const { version } = require('../package.json')
const app = express()
app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
const port = process.env.PORT || 3008
const telegramRouter = require('./routes/telegram')
const cfg = YAML.parse(fs.readFileSync('config.yml', 'utf8').toString())
if (!cfg) {
......@@ -32,17 +31,20 @@ console.log('INFO: ^VIX Cron time: \'' + cfg.tickers.volatility.cron_time + '\'
console.log('INFO: ^GSPC Cron time: \'' + cfg.tickers.stockmarket.cron_time + '\' with timezone: ' + cfg.tickers.stockmarket.cron_timezone)
const bot = new TelegramBot(cfg.telegram_settings.bot_token)
// Informs Telegram servers of the new webhook
bot.setWebHook(`${cfg.telegram_settings.public_url}/bot/${cfg.telegram_settings.bot_token}`).catch(error => {
console.log('ERROR: Telegram webhook setup failed: ' + error.message)
bot.setWebHook(`${cfg.telegram_settings.public_url}/telegram/bot${TelegramSecretHash}`)
bot.onText(/\/start/, (msg) => {
console.log('INFO: Set chat id: ' + msg.chat.id)
app.set('chat_id', msg.chat.id)
})
// app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.set('telegram_bot', bot)
app.get('/', (req, res) => res.send('Market data bot v' + version))
// We are receiving updates at the route below
app.post(`/bot/${cfg.telegram_settings.bot_token}`, (req, res) => {
bot.processUpdate(req.body)
res.sendStatus(200)
})
app.use('/telegram', telegramRouter)
app.listen(port, () => {
console.log(`INFO: Market Alert Bot v${version} is running on port ${port}!`)
})
......
const express = require('express')
const router = express.Router()
// We are receiving updates at the route below
router.post(`/bot${TelegramSecretHash}`, (req, res) => {
req.app.get('telegram_bot').processUpdate(req.body)
res.sendStatus(200)
})
module.exports = router
#!/usr/bin/env bash
docker run --restart=always -v $(pwd)/config.yml:/app/config.yml -d danger89/index-bot
docker run -p 3008:3008 --restart=always -v $(pwd)/config.yml:/app/config.yml -d danger89/index-bot
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment