From 9731b07e33718a7fb00f2f7b4a174e831addcab7 Mon Sep 17 00:00:00 2001 From: Matan Mashraki Date: Mon, 19 Oct 2020 20:46:17 +0300 Subject: [PATCH] Added license --- LICENSE | 21 ++++++++++++ README.md | 29 +++++++++-------- src/pages/api/send.ts | 1 - src/pages/index.tsx | 37 ++++++++++++++------- src/scheduleJobs.js | 76 +++++++++++++++++++++---------------------- 5 files changed, 100 insertions(+), 64 deletions(-) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..eacce8e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Matan Mashraki + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index 0cdf8b6..b181f7d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# NotifyBot +# NotifyBot Send yourself push notifications with Telegram. ## Usage @@ -11,24 +11,28 @@ Use the website or the API: - **schedule (optional):** Provide an ISO date string to send the message at ## Installation +[Docker Hub](https://hub.docker.com/r/planecore/notifybot) + Create a new Telegram Bot with [@BotFather](https://t.me/botfather) and create a token. Fill the BOT_TOKEN, leave CHAT_ID empty and start the container. ``` -notifybot: - image: planecore/notifybot - ports: - - "3000:3000" - environment: - - BOT_TOKEN= - - CHAT_ID= - volumes: - - ./db:/usr/src/app/db - restart: unless-stopped +version: "3" +services: + notifybot: + image: planecore/notifybot + ports: + - "3000:3000" + environment: + - BOT_TOKEN= + - CHAT_ID= + volumes: + - ./db:/usr/src/app/db + restart: unless-stopped ``` -After your container loaded, send the bot this command: `/get_chat_id` +After your container loaded, send this command to the bot: /get_chat_id Copy the reply the bot sent and place it in CHAT_ID. @@ -40,4 +44,3 @@ Restart the container the NotifyBot is ready to go! - Database: [LokiJS](https://github.com/techfort/LokiJS) - Telegram Bot: [node-telegram-bot-api](https://github.com/yagop/node-telegram-bot-api) - Logo: [Geist Icons](https://github.com/geist-org/react-icons) - diff --git a/src/pages/api/send.ts b/src/pages/api/send.ts index 73a5aca..956cd69 100644 --- a/src/pages/api/send.ts +++ b/src/pages/api/send.ts @@ -1,5 +1,4 @@ import { NextApiRequest, NextApiResponse } from "next" -import TelegramBot from "node-telegram-bot-api" import { sendMessage } from "../../data/bot" import { getDbInstance, save } from "../../data/db" diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 7b61361..4e37d94 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -13,7 +13,6 @@ import { import Message from "../models/Message" import { FormEvent, useState } from "react" import { useRouter } from "next/router" -import TelegramBot from "node-telegram-bot-api" import { MuiPickersUtilsProvider, DateTimePicker } from "@material-ui/pickers" import DayJSUtils from "@date-io/dayjs" import { ParsedUrlQuery } from "querystring" @@ -73,17 +72,31 @@ const HomePage: NextPage = ({ messages }) => { > - - setScheduleDate(date.toDate())} - ampm={false} - autoOk - disablePast - /> - +
+
+ +
+
+ + setScheduleDate(date.toDate())} + ampm={false} + autoOk + disablePast + /> + +
+
) diff --git a/src/scheduleJobs.js b/src/scheduleJobs.js index c383d3a..8ff7e7c 100644 --- a/src/scheduleJobs.js +++ b/src/scheduleJobs.js @@ -5,51 +5,51 @@ const TelegramBot = require("node-telegram-bot-api") const chatId = process.env.CHAT_ID const bot = new TelegramBot(process.env.BOT_TOKEN, { - polling: chatId === undefined, + polling: chatId === undefined || chatId === "", }) -if (chatId === undefined) { +if (chatId === undefined || chatId === "") { bot.onText(/\/get_chat_id/, (msg, _match) => { bot.sendMessage(msg.chat.id, msg.chat.id) }) -} - -// Send messages in queue every minute -sendMessagesInQueue() -scheduleJob() +} else { + // Send messages in queue every minute + sendMessagesInQueue() + scheduleJob() -function sendMessagesInQueue() { - const db = new loki("./db/notifybot.db") - db.loadDatabase({}, () => { - const messages = - db.getCollection("messages") || db.addCollection("messages") - messages.findAndUpdate( - { - sent: false, - schedule: { - $lte: new Date(Date.now() + 58 * 1000).toISOString(), + function sendMessagesInQueue() { + const db = new loki("./db/notifybot.db") + db.loadDatabase({}, () => { + const messages = + db.getCollection("messages") || db.addCollection("messages") + messages.findAndUpdate( + { + sent: false, + schedule: { + $lte: new Date(Date.now() + 58 * 1000).toISOString(), + }, }, - }, - (obj) => { - try { - bot.sendMessage(chatId, obj.message) - obj.sent = true - return obj - } catch (e) { - console.log("Couldn't send message") - console.log(e) - obj.sent = false - return obj + (obj) => { + try { + bot.sendMessage(chatId, obj.message) + obj.sent = true + return obj + } catch (e) { + console.log("Couldn't send message") + console.log(e) + obj.sent = false + return obj + } } - } - ) - db.save() - }) -} + ) + db.save() + }) + } -function scheduleJob() { - schedule.scheduleJob("* * * * *", async () => { - console.log("HELLO AT ", new Date()) - sendMessagesInQueue() - }) + function scheduleJob() { + schedule.scheduleJob("* * * * *", async () => { + console.log("HELLO AT ", new Date()) + sendMessagesInQueue() + }) + } }