Skip to content

Commit

Permalink
Added license
Browse files Browse the repository at this point in the history
  • Loading branch information
planecore committed Oct 19, 2020
1 parent 94fd2e7 commit 9731b07
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 64 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# <img src=public/logo.png width="32"> NotifyBot
# <img src="https://raw.githubusercontent.com/planecore/NotifyBot/main/public/logo.png" width="32"> NotifyBot
Send yourself push notifications with Telegram.

## Usage
Expand All @@ -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.

Expand All @@ -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)

1 change: 0 additions & 1 deletion src/pages/api/send.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
37 changes: 25 additions & 12 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -73,17 +72,31 @@ const HomePage: NextPage<HomePageProps> = ({ messages }) => {
>
<Tabs.Item label="Send Now" value="false" />
<Tabs.Item label="Send Later" value="true">
<MuiPickersUtilsProvider utils={DayJSUtils}>
<DateTimePicker
value={scheduleDate}
inputVariant="outlined"
style={{ height: 30 }}
onChange={(date) => setScheduleDate(date.toDate())}
ampm={false}
autoOk
disablePast
/>
</MuiPickersUtilsProvider>
<div style={{ height: 40 }}>
<div style={{ position: "absolute" }}>
<Button type="secondary" ghost>
{scheduleDate.toLocaleString("en-US", {
month: "short",
day: "numeric",
hour: "numeric",
minute: "2-digit",
})}
</Button>
</div>
<div style={{ position: "absolute" }}>
<MuiPickersUtilsProvider utils={DayJSUtils}>
<DateTimePicker
value={scheduleDate}
inputVariant="outlined"
style={{ height: 40, width: 200, opacity: 0, zIndex: 50 }}
onChange={(date) => setScheduleDate(date.toDate())}
ampm={false}
autoOk
disablePast
/>
</MuiPickersUtilsProvider>
</div>
</div>
</Tabs.Item>
</Tabs>
)
Expand Down
76 changes: 38 additions & 38 deletions src/scheduleJobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
}
}

0 comments on commit 9731b07

Please sign in to comment.