Skip to content

Commit

Permalink
Merge pull request #2 from henry-arcu89/add_typescript
Browse files Browse the repository at this point in the history
Add typescript, emojies & message for stop
  • Loading branch information
henry-arcu89 authored Oct 9, 2021
2 parents 96ce1f8 + 29df704 commit 5d4a2d7
Show file tree
Hide file tree
Showing 5 changed files with 589 additions and 212 deletions.
65 changes: 29 additions & 36 deletions index.js → index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,26 @@ dotenv.config();
const token = process.env.TELEGRAM_TOKEN_BOT;
const bot = new TelegramBot(token, { polling: true });

const MINUTES = process.env.MINUTES;
const MINUTES = Number(process.env.MINUTES);

let subscribed = [];
type Chatconfig = {
id: number,
status: boolean,
}

let subscribed = [] as Chatconfig[];

void (async function main() {
subscribed = await loadData();
})();

bot.onText(/\/echo (.+)/, (msg, match) => {
// 'msg' is the received Message from Telegram
// 'match' is the result of executing the regexp above on the text content
// of the message

const chatId = msg.chat.id;
const resp = match[1]; // the captured "whatever"

// send back the matched "whatever" to the chat
bot.sendMessage(chatId, resp);
});

let the_interval = MINUTES * 60 * 1000;
let loop = 0;
setInterval(function () {
axios
.get("https://api.alternative.me/fng/")
.then(function (response) {
const fear = response.data.data[0].value;
const fear: number = response.data.data[0].value;
const message = getMessage(fear);

if (isTheMomentForSendIt(fear, loop)) {
Expand All @@ -47,21 +40,21 @@ setInterval(function () {
}, the_interval);

bot.on("message", (msg) => {
const chatId = msg.chat.id;
const chatId: number = msg.chat.id;

if (
msg.text.toLowerCase() === "start" ||
msg.text.toLowerCase() === "/start"
) {
if (!isSubscribed(chatId)) {
bot.sendMessage(chatId, "Subscribed, thanks!");
bot.sendMessage(chatId, "Subscribed, thanks! \u{2705}");
subscribed.push({
id: chatId,
status: true,
});
saveData();
} else {
bot.sendMessage(chatId, "You are already subscribed.");
bot.sendMessage(chatId, "You are already subscribed. \u{2705}");
}
} else if (
msg.text.toLowerCase() === "now" ||
Expand All @@ -70,7 +63,7 @@ bot.on("message", (msg) => {
axios
.get("https://api.alternative.me/fng/")
.then(function (response) {
const fear = response.data.data[0].value;
const fear: number = response.data.data[0].value;
const message = getMessage(fear);
if (message) {
bot.sendMessage(chatId, message);
Expand All @@ -92,41 +85,41 @@ bot.on("message", (msg) => {
if (index > -1) {
subscribed.splice(index, 1);
}
bot.sendMessage(chatId, "Ok, now you are unsubscribed.");
bot.sendMessage(chatId, "Ok, now you are unsubscribed. \u{274C}");
saveData();
} else {
bot.sendMessage(
chatId,
"For get current Fear Index, you can send: /now, NOW, Now or now\n\nFor subscribe to the chanel, you can send: /start, Start, START or start"
"For get current Fear Index, you can send: /now, NOW, Now or now\n\nFor subscribe to the chanel, you can send: /start, Start, START or start\n\nFor unsubscribe to the chanel, you can send: /stop, Stop, STOP or stop"
);
}
});

function getMessage(fear) {
function getMessage(fear: number): string {
if (fear >= 80) {
return (
"OMG SELL NOW, It's a bubble: The Fear & Greed Index of Bitcoin is " +
"OMG SELL NOW, It's a bubble: \u{1F6A8}\u{1F6A8}\u{1F6A8} The Fear & Greed Index of Bitcoin is " +
fear
);
} else if (fear >= 75 && fear < 80) {
return "SELL: The Fear & Greed Index of Bitcoin is " + fear;
return "SELL: \u{1F6A8}\u{1F6A8} The Fear & Greed Index of Bitcoin is " + fear;
} else if (fear >= 70 && fear < 75) {
return "Attention: The Fear & Greed Index of Bitcoin is " + fear;
return "Attention: \u{1F6A8} The Fear & Greed Index of Bitcoin is " + fear;
} else if (fear <= 30 && fear > 25) {
return "Attention: The Fear & Greed Index of Bitcoin is " + fear;
return "Attention: \u{1F6A8} The Fear & Greed Index of Bitcoin is " + fear;
} else if (fear <= 25 && fear > 20) {
return "BUY: The Fear & Greed Index of Bitcoin is " + fear;
return "BUY: \u{1F6A8}\u{1F6A8} The Fear & Greed Index of Bitcoin is " + fear;
} else if (fear <= 20) {
return (
"OMG BUY NOW, It's very cheap: The Fear & Greed Index of Bitcoin is " +
"OMG BUY NOW, It's very cheap: \u{1F6A8}\u{1F6A8}\u{1F6A8} The Fear & Greed Index of Bitcoin is " +
fear
);
} else {
return false;
return null;
}
}

function sendMessageSubscription(message) {
function sendMessageSubscription(message: string) {
if (message) {
subscribed.forEach((chatId) => {
if (chatId.status) {
Expand All @@ -136,7 +129,7 @@ function sendMessageSubscription(message) {
}
}

function isTheMomentForSendIt(fear, loop) {
function isTheMomentForSendIt(fear: number, loop: number): boolean {
if ((fear >= 80 || fear <= 20) && loop % 1 == 0) {
return true;
}
Expand All @@ -158,7 +151,7 @@ function isTheMomentForSendIt(fear, loop) {
return false;
}

async function loadData() {
async function loadData(): Promise<Chatconfig[]> {
await storage.init({
stringify: JSON.stringify,
parse: JSON.parse,
Expand All @@ -169,13 +162,13 @@ async function loadData() {
await storage.setItem("subscribed", []);
}

return Object.values(await storage.getItem("subscribed"));
return Object.values(await storage.getItem("subscribed")) as Chatconfig[];
}

async function saveData() {
async function saveData(): Promise<void> {
await storage.updateItem("subscribed", subscribed);
}

function isSubscribed(chatId) {
return subscribed.filter((chat) => chat.id == chatId).length;
function isSubscribed(chatId: number): boolean {
return (subscribed.filter((chat) => chat.id == chatId).length) ? true : false;
}
Loading

0 comments on commit 5d4a2d7

Please sign in to comment.