From 301b3811c36a62656db69bf283520d0ddd998ea2 Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Wed, 9 Sep 2020 11:43:54 -0700 Subject: [PATCH] btc price bot test --- api/bots/btc.ts | 72 +++++++++++++++++++++++++++++++++++ api/bots/welcome.ts | 2 +- api/utils/tables.sql | 14 +++++++ dist/api/bots/btc.js | 81 ++++++++++++++++++++++++++++++++++++++++ dist/api/bots/btc.js.map | 1 + dist/api/bots/welcome.js | 2 +- 6 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 api/bots/btc.ts create mode 100644 api/utils/tables.sql create mode 100644 dist/api/bots/btc.js create mode 100644 dist/api/bots/btc.js.map diff --git a/api/bots/btc.ts b/api/bots/btc.ts new file mode 100644 index 0000000..ae65908 --- /dev/null +++ b/api/bots/btc.ts @@ -0,0 +1,72 @@ +import * as Sphinx from 'sphinx-bot' +import { finalAction } from '../controllers/actions' +import * as fetch from 'node-fetch' +const msg_types = Sphinx.MSG_TYPE + +let initted = false + +/* +curl -H "X-CMC_PRO_API_KEY: 2fddf900-9114-471c-849d-cad3e3923e5b" -H "Accept: application/json" -d "symbol=BTC&convert=USD" -G https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest +*/ + +const token = '2fddf900-9114-471c-849d-cad3e3923e5b' +const url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest' + +export function init() { + if(initted) return + initted = true + + const client = new Sphinx.Client() + client.login('_', finalAction) + + client.on(msg_types.MESSAGE, async (message: Sphinx.Message) => { + const arr = message.content.split(' ') + if (arr.length < 2) return + if (arr[0]!=='/btc') return + const cmd = arr[1] + + switch (cmd) { + + case 'price': + if (arr.length < 3) return + console.log("price", arr[2]) + try { + const r = await fetch(url+'?symbol=BTC&convert=USD',{ + headers:{'X-CMC_PRO_API_KEY': token, 'Accept': 'application/json'} + }) + if (!r.ok) return + const j = await r.json() + const price = j.data.BTC.quote.USD.price.toFixed(2) + const percentChange24 = j.data.BTC.quote.USD.percent_change_24h.toFixed(2) + const embed = new Sphinx.MessageEmbed() + .setAuthor('BitcoinBot') + .setTitle('Bitcoin Price:') + .addFields([ + { name: 'Price:', value: price }, + { name: '24 Hour Change', value: percentChange24+'%' } + ]) + .setThumbnail(botSVG) + message.channel.send({ embed }) + } catch(e){ + console.log('BTC bot error',e) + } + return + + default: + const embed = new Sphinx.MessageEmbed() + .setAuthor('BitcoinBot') + .setTitle('BitcoinBot Commands:') + .addFields([ + { name: 'Print BTC price', value: '/btc price' }, + { name: 'Help', value: '/btc help' } + ]) + .setThumbnail(botSVG) + message.channel.send({ embed }) + return + } + }) +} + +const botSVG = ` + +` \ No newline at end of file diff --git a/api/bots/welcome.ts b/api/bots/welcome.ts index 074e5d1..247155f 100644 --- a/api/bots/welcome.ts +++ b/api/bots/welcome.ts @@ -71,6 +71,6 @@ export function init() { }) } -const botSVG = ` +const botSVG = ` ` \ No newline at end of file diff --git a/api/utils/tables.sql b/api/utils/tables.sql new file mode 100644 index 0000000..6ea9a7a --- /dev/null +++ b/api/utils/tables.sql @@ -0,0 +1,14 @@ + + +CREATE TABLE sphinx_chat_members ( + id INT, + chat_id INT, + contact_id INT, + role INT, + total_spent INT, + total_messages INT, + last_active DATETIME, + status INT, + created_at DATETIME, + updated_at DATETIME +); \ No newline at end of file diff --git a/dist/api/bots/btc.js b/dist/api/bots/btc.js new file mode 100644 index 0000000..f606c08 --- /dev/null +++ b/dist/api/bots/btc.js @@ -0,0 +1,81 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const Sphinx = require("sphinx-bot"); +const actions_1 = require("../controllers/actions"); +const fetch = require("node-fetch"); +const msg_types = Sphinx.MSG_TYPE; +let initted = false; +/* +curl -H "X-CMC_PRO_API_KEY: 2fddf900-9114-471c-849d-cad3e3923e5b" -H "Accept: application/json" -d "symbol=BTC&convert=USD" -G https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest +*/ +const token = '2fddf900-9114-471c-849d-cad3e3923e5b'; +const url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest'; +function init() { + if (initted) + return; + initted = true; + const client = new Sphinx.Client(); + client.login('_', actions_1.finalAction); + client.on(msg_types.MESSAGE, (message) => __awaiter(this, void 0, void 0, function* () { + const arr = message.content.split(' '); + if (arr.length < 2) + return; + if (arr[0] !== '/btc') + return; + const cmd = arr[1]; + switch (cmd) { + case 'price': + if (arr.length < 3) + return; + console.log("price", arr[2]); + try { + const r = yield fetch(url + '?symbol=BTC&convert=USD', { + headers: { 'X-CMC_PRO_API_KEY': token, 'Accept': 'application/json' } + }); + if (!r.ok) + return; + const j = yield r.json(); + const price = j.data.BTC.quote.USD.price.toFixed(2); + const percentChange24 = j.data.BTC.quote.USD.percent_change_24h.toFixed(2); + const embed = new Sphinx.MessageEmbed() + .setAuthor('BitcoinBot') + .setTitle('Bitcoin Price:') + .addFields([ + { name: 'Price:', value: price }, + { name: '24 Hour Change', value: percentChange24 + '%' } + ]) + .setThumbnail(botSVG); + message.channel.send({ embed }); + } + catch (e) { + console.log('BTC bot error', e); + } + return; + default: + const embed = new Sphinx.MessageEmbed() + .setAuthor('BitcoinBot') + .setTitle('BitcoinBot Commands:') + .addFields([ + { name: 'Print BTC price', value: '/btc price' }, + { name: 'Help', value: '/btc help' } + ]) + .setThumbnail(botSVG); + message.channel.send({ embed }); + return; + } + })); +} +exports.init = init; +const botSVG = ` + +`; +//# sourceMappingURL=btc.js.map \ No newline at end of file diff --git a/dist/api/bots/btc.js.map b/dist/api/bots/btc.js.map new file mode 100644 index 0000000..bdfc09d --- /dev/null +++ b/dist/api/bots/btc.js.map @@ -0,0 +1 @@ +{"version":3,"file":"btc.js","sourceRoot":"","sources":["../../../api/bots/btc.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,qCAAoC;AACpC,oDAAoD;AACpD,oCAAmC;AACnC,MAAM,SAAS,GAAG,MAAM,CAAC,QAAQ,CAAA;AAEjC,IAAI,OAAO,GAAG,KAAK,CAAA;AAEnB;;EAEE;AAEF,MAAM,KAAK,GAAG,sCAAsC,CAAA;AACpD,MAAM,GAAG,GAAG,mEAAmE,CAAA;AAE/E,SAAgB,IAAI;IAClB,IAAG,OAAO;QAAE,OAAM;IAClB,OAAO,GAAG,IAAI,CAAA;IAEd,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,MAAM,EAAE,CAAA;IAClC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,qBAAW,CAAC,CAAA;IAE9B,MAAM,CAAC,EAAE,CAAC,SAAS,CAAC,OAAO,EAAE,CAAO,OAAuB,EAAE,EAAE;QAC7D,MAAM,GAAG,GAAG,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QACtC,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC;YAAE,OAAM;QAC1B,IAAI,GAAG,CAAC,CAAC,CAAC,KAAG,MAAM;YAAE,OAAM;QAC3B,MAAM,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC,CAAA;QAElB,QAAQ,GAAG,EAAE;YAEX,KAAK,OAAO;gBACV,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC;oBAAE,OAAM;gBAC1B,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;gBAC5B,IAAI;oBACA,MAAM,CAAC,GAAG,MAAM,KAAK,CAAC,GAAG,GAAC,yBAAyB,EAAC;wBAChD,OAAO,EAAC,EAAC,mBAAmB,EAAE,KAAK,EAAE,QAAQ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AC,CAAA;gBACvB,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,CAAC,CAAA;gBAC/B,OAAM;SACT;IACH,CAAC,CAAA,CAAC,CAAA;AACJ,CAAC;AArDD,oBAqDC;AAED,MAAM,MAAM,GAAG;;OAER,CAAA"} \ No newline at end of file diff --git a/dist/api/bots/welcome.js b/dist/api/bots/welcome.js index e6f0056..4ed0425 100644 --- a/dist/api/bots/welcome.js +++ b/dist/api/bots/welcome.js @@ -81,7 +81,7 @@ function init() { })); } exports.init = init; -const botSVG = ` +const botSVG = ` `; //# sourceMappingURL=welcome.js.map \ No newline at end of file