Skip to content

Commit

Permalink
Added: Pushover-notification (#671)
Browse files Browse the repository at this point in the history
* pushover init

* pushover lib

* remove debug

* pushover documentation

* update twitter message

* update docs
  • Loading branch information
lukman7788 authored May 25, 2022
1 parent 03f2ee1 commit d3eb10c
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 2 deletions.
18 changes: 18 additions & 0 deletions docs/src/pages/guides/notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ At this moment, Monika support these channel of notifications (You can use just
13. [Telegram](https://hyperjumptech.github.io/monika/guides/notifications#telegram)
14. [Webhook](https://hyperjumptech.github.io/monika/guides/notifications#webhook)
15. [WhatsApp Business](https://hyperjumptech.github.io/monika/guides/notifications#whatsapp)
16. [Pushover](https://hyperjumptech.github.io/monika/guides/notifications#pushover)

## Configurations

Expand Down Expand Up @@ -392,3 +393,20 @@ Monika supports Whatsapp notification. To enable notification via whatsapp, you
| Url | The URL of your whatsapp api server | `https://yourwhatsappapiserver.com` |
| Username | Your whatsapp api user name | `username` |
| Userpassword | Your whatsapp api user password | `userpassword` |

## Pushover

Monika supports Pushover. To enable notification via Pushover, you must create a pushover application first. More info at [Pushover documentation](https://pushover.net/api).

```yml
-id: unique-id-webhook,
type: pushover,
data:
token: "pushover-token"
user: "pushover-user"
```

| Key | Description | Example |
| ----- | -------------------------- | -------------------------- |
| Token | Pushover application token | `pushoverApplicationToken` |
| User | Pushover user key | `pushoverUserKey` |
6 changes: 5 additions & 1 deletion monika.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,11 @@ probes:
# data:
# thread_id: "1234567890"
# access_token: YOUR_CUSTOM_INTEGRATION_ACCESS_TOKEN

# - id: random-string-pushover
# type: pushover
# data:
# token: "app-token => please register app on pushover first"
# user: "user-token"
# limit log database size in bytes
db_limit:
max_db_size: 1000000000
Expand Down
1 change: 1 addition & 0 deletions src/components/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const updateConfig = (config: Config, validate = true): void => {
log.info('Updating config')
if (validate) {
const validated = validateConfig(config)

if (!validated.valid) {
throw new Error(validated.message)
}
Expand Down
10 changes: 10 additions & 0 deletions src/components/config/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ const MONIKA_NOTIF_NO_URL = setInvalidResponse(
// Webhook
const WEBHOOK_NO_URL = setInvalidResponse('URL not found')

// Pushover
const PUSHOVER_NO_TOKEN = setInvalidResponse('TOKEN not found')
const PUSHOVER_NO_USER = setInvalidResponse('USER not found')

// Discord
const DISCORD_NO_URL = setInvalidResponse('Discord URL not found')

Expand Down Expand Up @@ -236,6 +240,12 @@ function validateNotification(notifications: Notification[]): Validation {
break
}

case 'pushover': {
if (!notification.data.token) return PUSHOVER_NO_TOKEN
if (!notification.data.user) return PUSHOVER_NO_USER
break
}

default:
return setInvalidResponse(
`Notifications type is not allowed (${(notification as any)?.type})`
Expand Down
68 changes: 68 additions & 0 deletions src/components/notification/channel/pushover.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**********************************************************************************
* MIT License *
* *
* Copyright (c) 2021 Hyperjump Technology *
* *
* 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. *
**********************************************************************************/

import axios from 'axios'

import { PushoverData } from '../../../interfaces/data'
import { NotificationMessage } from '../../../interfaces/notification'

export const sendPushover = async (
data: PushoverData,
message: NotificationMessage
) => {
try {
const notificationType =
message.meta.type[0].toUpperCase() + message.meta.type.substring(1)

let content
switch (message.meta.type) {
case 'incident':
case 'recovery': {
content = `New ${notificationType} event from Monika\n\n${message.body}`
break
}
default:
content = message.body
break
}

const res = await axios.request({
method: 'POST',
url: `https://api.pushover.net/1/messages.json`,
headers: {
'Content-Type': 'application/json',
},
data: {
user: data.user,
token: data.token,
message: content,
html: 1,
},
})

return res
} catch (error) {
throw error
}
}
2 changes: 2 additions & 0 deletions src/components/notification/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
dataWorkplaceSchemaValidator,
dataLarkSchemaValidator,
dataGoogleChatSchemaValidator,
dataPushoverSchemaValidator,
} from './validator'

// reexported with alias because this `errorMessage` function is used in test file
Expand All @@ -66,6 +67,7 @@ export const notificationChecker = async (
lark: dataLarkSchemaValidator,
'google-chat': dataGoogleChatSchemaValidator,
pagerduty: pagerduty.validator,
pushover: dataPushoverSchemaValidator,
}

await Promise.all(
Expand Down
6 changes: 6 additions & 0 deletions src/components/notification/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { sendWorkplace } from './channel/workplace'
import { sendLark } from './channel/lark'
import { sendGoogleChat } from './channel/googlechat'
import { newPagerDuty } from './channel/pagerduty'
import { sendPushover } from './channel/pushover'

export class NotificationSendingError extends Error {
notificationType: string
Expand Down Expand Up @@ -142,6 +143,11 @@ export async function sendNotifications(
break
}

case 'pushover': {
await sendPushover(notification.data, message)
break
}

case 'smtp': {
const transporter = createSmtpTransport(notification.data)
await sendSmtpMail(transporter, {
Expand Down
7 changes: 7 additions & 0 deletions src/components/notification/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,10 @@ export const dataGoogleChatSchemaValidator = dataBaseEmailSchemaValidator(
).keys({
url: Joi.string().uri().required().label('Google URL'),
})

export const dataPushoverSchemaValidator = dataBaseEmailSchemaValidator(
'pushover'
).keys({
token: Joi.string().required().label('Pushover token'),
user: Joi.string().required().label('Pushover user'),
})
6 changes: 6 additions & 0 deletions src/interfaces/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ export interface TelegramData {
body: string
}

export interface PushoverData {
token: string
user: string
message: string
}

export interface WebhookDataBody {
url: string
time: string
Expand Down
8 changes: 7 additions & 1 deletion src/interfaces/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
LarkData,
SlackData,
GoogleChatData,
PushoverData,
} from './data'

export type Notification =
Expand All @@ -54,7 +55,7 @@ export type Notification =
| LarkNotification
| GoogleChatNotification
| PagerDutyNotification

| PushoverNotification
interface BaseNotification {
id: string
}
Expand Down Expand Up @@ -131,6 +132,11 @@ interface GoogleChatNotification extends BaseNotification {
data: GoogleChatData
}

interface PushoverNotification extends BaseNotification {
type: 'pushover'
data: PushoverData
}

export interface NotificationMessage {
subject: string
body: string
Expand Down

0 comments on commit d3eb10c

Please sign in to comment.