move tgBot to own repo
This commit is contained in:
106
tgBot.js
106
tgBot.js
@@ -1,106 +0,0 @@
|
|||||||
const fetch = require("node-fetch");
|
|
||||||
var mysql = require('mysql');
|
|
||||||
const TelegramBot = require('node-telegram-bot-api');
|
|
||||||
|
|
||||||
const tgInfo = require('./telegram-credentials');
|
|
||||||
if (!tgInfo.token) {
|
|
||||||
console.error("ERR: credentials not set.")
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var connection = mysql.createPool({
|
|
||||||
host: 'localhost',
|
|
||||||
user: 'simcompanies',
|
|
||||||
password: '',
|
|
||||||
database: 'simcompanies'
|
|
||||||
});
|
|
||||||
|
|
||||||
function twoDigits(d) {
|
|
||||||
if (0 <= d && d < 10) return "0" + d.toString();
|
|
||||||
if (-10 < d && d < 0) return "-0" + (-1 * d).toString();
|
|
||||||
return d.toString();
|
|
||||||
}
|
|
||||||
Date.prototype.toMysqlFormat = function () {
|
|
||||||
return this.getFullYear() + "-" + twoDigits(1 + this.getMonth()) + "-" + twoDigits(this.getDate()) + " " + twoDigits(this.getHours()) + ":" + twoDigits(this.getMinutes()) + ":" + twoDigits(this.getSeconds());
|
|
||||||
};
|
|
||||||
|
|
||||||
const init = async () => {
|
|
||||||
const resourceAPI = "https://www.simcompanies.com/api/v3/en/encyclopedia/resources/";
|
|
||||||
var resourceList = await fetch(resourceAPI);
|
|
||||||
resourceList = await resourceList.json();
|
|
||||||
|
|
||||||
const tgBot = new TelegramBot(tgInfo.token, { polling: true });
|
|
||||||
|
|
||||||
tgBot.onText(/\/resource (.+)/, (msg, match) => {
|
|
||||||
const chatId = msg.chat.id;
|
|
||||||
var resource = match[1];
|
|
||||||
resource = resourceList.find(function (elem) {
|
|
||||||
return elem["name"].toUpperCase() === resource.toUpperCase();
|
|
||||||
});
|
|
||||||
if (!resource)
|
|
||||||
return tgBot.sendMessage(chatId, `DASH - Simcompany bot made by OTB\nThe resource you are looking for could not be found. Please make sure, you spelled the name correctly. eg. "Mining Research."`);
|
|
||||||
|
|
||||||
tgBot.sendMessage(chatId, `DASH - Simcompany bot made by OTB\nI see you are looking for some information about ${resource["name"]}.\nPlease tell me which quality you are looking for:`, {
|
|
||||||
reply_markup: {
|
|
||||||
inline_keyboard: [[
|
|
||||||
{
|
|
||||||
text: 'q0',
|
|
||||||
callback_data: resource["db_letter"] + '|0|' + resource["name"]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
text: 'q1',
|
|
||||||
callback_data: resource["db_letter"] + '|1|' + resource["name"]
|
|
||||||
}], [
|
|
||||||
{
|
|
||||||
text: 'q2',
|
|
||||||
callback_data: resource["db_letter"] + '|2|' + resource["name"]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
text: 'q3',
|
|
||||||
callback_data: resource["db_letter"] + '|3|' + resource["name"]
|
|
||||||
}], [
|
|
||||||
{
|
|
||||||
text: 'q4',
|
|
||||||
callback_data: resource["db_letter"] + '|4|' + resource["name"]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
text: 'q5',
|
|
||||||
callback_data: resource["db_letter"] + '|5|' + resource["name"]
|
|
||||||
}], [
|
|
||||||
{
|
|
||||||
text: 'q6',
|
|
||||||
callback_data: resource["db_letter"] + '|6|' + resource["name"]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
text: 'q7',
|
|
||||||
callback_data: resource["db_letter"] + '|7|' + resource["name"]
|
|
||||||
},
|
|
||||||
]]
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
tgBot.on("callback_query", (data) => {
|
|
||||||
const msgData = data.data.split("|");
|
|
||||||
var dayend = new Date().toMysqlFormat();
|
|
||||||
var daybegin = new Date();
|
|
||||||
daybegin.setDate(daybegin.getDate() - 1);
|
|
||||||
daybegin = daybegin.toISOString().slice(0, 19).replace('T', ' ');
|
|
||||||
|
|
||||||
const querystring = `SELECT AVG(price) as avg, MIN(price) as min, MAX(price) as max FROM marketv2 WHERE kind = ${mysql.escape(msgData[0])} AND time > "${daybegin}" AND time < "${dayend}" AND quality = ${mysql.escape(msgData[1])} GROUP BY quality`;
|
|
||||||
|
|
||||||
connection.query(querystring, function (error, results, fields) {
|
|
||||||
if (error) {
|
|
||||||
console.error(error);
|
|
||||||
return tgBot.sendMessage(data.message.chat.id, "ERR: sorry, I had trouble querying the database.")
|
|
||||||
}
|
|
||||||
if (!results[0]) {
|
|
||||||
return tgBot.sendMessage(data.message.chat.id, "WRN: sorry, I didn't find any data matching your request.")
|
|
||||||
}
|
|
||||||
tgBot.sendMessage(data.message.chat.id, `DASH - Simcompany bot made by OTB\nInformation displayed for ${msgData[2]} q${msgData[1]}\nHighest price (24h): ${results[0]["max"]}\nAverage price (24h): ${results[0]["avg"]}\nLowest price (24h): ${results[0]["min"]}\n`)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
init();
|
|
||||||
Reference in New Issue
Block a user