From 88d051558e4ea1e7deedcfdeae4168c6305ec98d Mon Sep 17 00:00:00 2001 From: Igor Bronovskyi Date: Tue, 27 Apr 2021 14:28:23 +0300 Subject: [PATCH 01/23] fix: syntax errors in the help commands --- cmd/discord.go | 2 +- cmd/mattermost.go | 2 +- cmd/msteams.go | 2 +- cmd/pushover.go | 2 +- cmd/slack.go | 2 +- docs/services.md | 10 +++++----- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cmd/discord.go b/cmd/discord.go index 11de496..d83f11a 100644 --- a/cmd/discord.go +++ b/cmd/discord.go @@ -31,7 +31,7 @@ func SendToDiscord() *cli.Command { Description: `Discord uses bot token to authenticate & send messages to defined channels. Multiple channel ids can be used separated by comma ','. All configuration options are also available via environment variables.`, - UsageText: "pingme discord --token '123' --channel '12345,67890' --message 'some message'", + UsageText: "pingme discord --token '123' --channel '12345,67890' --msg 'some message'", Flags: []cli.Flag{ &cli.StringFlag{ Destination: &discordOpts.Token, diff --git a/cmd/mattermost.go b/cmd/mattermost.go index 3862831..9f374ce 100644 --- a/cmd/mattermost.go +++ b/cmd/mattermost.go @@ -57,7 +57,7 @@ func SendToMattermost() *cli.Command { Name: "mattermost", Usage: "Send message to mattermost", UsageText: "pingme mattermost --token '123' --channel '12345567' --url 'localhost' --scheme http " + - "--message 'some message'", + "--msg 'some message'", Description: `Mattermost uses token to authenticate and channel ids for targets. You can specify multiple channels by separating the value with ','.`, Flags: []cli.Flag{ diff --git a/cmd/msteams.go b/cmd/msteams.go index d8e20a4..78cead2 100644 --- a/cmd/msteams.go +++ b/cmd/msteams.go @@ -29,7 +29,7 @@ func SendToTeams() *cli.Command { Usage: "Send message to microsoft teams", Description: `Teams uses webhooks to send messages, you can add multiple webhooks separated by comma ',' or you can add permissions for multiple channels to single webhook.`, - UsageText: "pingme teams --webhook 'https://example.webhook.office.com/xx' --message 'some message'", + UsageText: "pingme teams --webhook 'https://example.webhook.office.com/xx' --msg 'some message'", Flags: []cli.Flag{ &cli.StringFlag{ Destination: &msTeamOpt.Webhook, diff --git a/cmd/pushover.go b/cmd/pushover.go index cee8707..dfed0e7 100644 --- a/cmd/pushover.go +++ b/cmd/pushover.go @@ -26,7 +26,7 @@ func SendToPushOver() *cli.Command { return &cli.Command{ Name: "pushover", Usage: "Send message to pushover", - UsageText: "pingme pushover --token '123' --user '12345,567' --message 'some message'", + UsageText: "pingme pushover --token '123' --user '12345,567' --msg 'some message'", Description: `Pushover uses token to authenticate application and user token to send messages to the user. All configuration options are also available via environment variables.`, Flags: []cli.Flag{ diff --git a/cmd/slack.go b/cmd/slack.go index 42a2c53..d47d77e 100644 --- a/cmd/slack.go +++ b/cmd/slack.go @@ -28,7 +28,7 @@ func SendToSlack() *cli.Command { return &cli.Command{ Name: "slack", Usage: "Send message to slack", - UsageText: "pingme slack --token '123' --channel '12345,67890' --message 'some message'", + UsageText: "pingme slack --token '123' --channel '12345,67890' --msg 'some message'", Description: `Slack uses token to authenticate and send messages to defined channels. Multiple channel ids can be used separated by comma ','. All configuration options are also available via environment variables.`, diff --git a/docs/services.md b/docs/services.md index 4bf5be8..1d893e4 100644 --- a/docs/services.md +++ b/docs/services.md @@ -111,7 +111,7 @@ jobs: ## Pushover ```bash -pingme pushover --token '123' --user '12345567' --title 'some title' --message 'some message' +pingme pushover --token '123' --user '12345567' --title 'some title' --msg 'some message' ``` - Github Action @@ -158,7 +158,7 @@ Latest api version 4 is used for interacting with server, this can also be chan You can specify multiple channels by separating the value with ','. ```bash -pingme mattermost --token '123' --channel '12345,567' --url 'localhost' --scheme 'http' --message 'some message' +pingme mattermost --token '123' --channel '12345,567' --url 'localhost' --scheme 'http' --msg 'some message' ``` - Github Action @@ -207,7 +207,7 @@ Slack uses token to authenticate and send messages to defined channels. Multiple channel ids can be used separated by comma ','. ```bash -pingme slack --token '123' --channel '1234567890' --message 'some message' +pingme slack --token '123' --channel '1234567890' --msg 'some message' ``` - Github Action @@ -251,7 +251,7 @@ Discord uses bot token to authenticate & send messages to defined channels. Multiple channel ids can be used separated by comma ','. ```bash - pingme discord --token '123' --channel '1234567890' --message 'some message' + pingme discord --token '123' --channel '1234567890' --msg 'some message' ``` - Github Action @@ -295,7 +295,7 @@ Teams uses webhooks to send messages, you can add multiple webhooks separated by you can add permissions for multiple channels to single webhook. ```bash -pingme teams --webhook 'https://example.webhook.office.com/xx' --message 'some message' +pingme teams --webhook 'https://example.webhook.office.com/xx' --msg 'some message' ``` - Github Action From 7e1593e8a2c9b552f41d7bf06b820f52aa8c9212 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Wed, 28 Apr 2021 00:58:13 +0800 Subject: [PATCH 02/23] docs: add pushbullet service docs --- README.md | 24 ++++++++----- docs/home.md | 9 ++--- docs/install.md | 14 ++++++-- docs/services.md | 93 ++++++++++++++++++++++++++++++++++++------------ 4 files changed, 103 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 8884e8e..72621a6 100644 --- a/README.md +++ b/README.md @@ -24,14 +24,13 @@

- AboutDocumentationSupported ServicesInstallGithub ActionConfigurationContributing • - Show Your Support • + Show Your Support

--- @@ -45,7 +44,7 @@ And i can ship it everywhere with ease. Hence, the birth of PingMe. Everything is configurable via environment variables, and you can simply export the logs or messages to a variable which will be sent -as message. And most of all this serves as a swiss army knife sort of tool which supports multiple platforms. +as message, and most of all this serves as a swiss army knife sort of tool which supports multiple platforms. @@ -53,20 +52,28 @@ as message. And most of all this serves as a swiss army knife sort of tool which - *Discord* - *Email* - *Microsoft Teams* +- *Mattermost* +- *Pushover* +- *Pushbullet* - *RocketChat* - *Slack* - *Telegram* -- *Pushover* -- *Mattermost* - ## Install -### Linux & MacOs +### MacOS & Linux Homebrew ```bash brew install kha7iq/tap/pingme ``` +## Linux Binary +```bash +wget -q https://github.com/kha7iq/pingme/releases/download/v0.1.6/pingme_Linux_x86_64.tar.gz +tar -xf pingme_Linux_x86_64.tar.gz +chmod +x pingme +sudo mv pingme /usr/local/bin/pingme +``` + ### Go Get ```bash go get -u github.com/kha7iq/pingme @@ -91,7 +98,7 @@ Docker Registry ```bash docker pull khaliq/pingme:latest ``` -Gighub Registry +Github Registry ```bash docker pull ghcr.io/kha7iq/pingme:latest ``` @@ -132,6 +139,7 @@ COMMANDS: pushover Send message to pushover email Send an email mattermost Send message to mattermost + pushbullet Send message to pushbullet help, h Shows a list of commands or help for one command GLOBAL OPTIONS: diff --git a/docs/home.md b/docs/home.md index cd24cdc..d0cb0e4 100644 --- a/docs/home.md +++ b/docs/home.md @@ -33,24 +33,25 @@ ## About **PingMe** is a personal project to satisfy my needs of having alerts, most major platforms have integration to send alerts -but its not always useful, either you are stuck with one particular platform, or you have to do alot of integrations. I needed a small app +but it's not always useful, either you are stuck with one particular platform, or you have to do alot of integrations. I needed a small app which i can just call from my backup scripts, cron jobs, CI/CD pipelines or from anywhere to send a message with particular information. And i can ship it everywhere with ease. Hence, the birth of PingMe. Everything is configurable via environment variables, and you can simply export the logs or messages to a variable which will be sent -as message. And most of all this serves as a swiss army knife sort of tool which supports multiple platforms. +as message, and most of all this serves as a swiss army knife sort of tool which supports multiple platforms. ## Supported services - *Discord* - *Email* - *Microsoft Teams* +- *Mattermost* +- *Pushover* +- *Pushbullet* - *RocketChat* - *Slack* - *Telegram* -- *Pushover* -- *Mattermost* diff --git a/docs/install.md b/docs/install.md index 63a2870..61dc2b4 100644 --- a/docs/install.md +++ b/docs/install.md @@ -1,9 +1,17 @@ -## Linux & MacOs +## MacOS & Linux Homebrew ```bash brew install kha7iq/tap/pingme ``` +## Linux Binary +```bash +wget -q https://github.com/kha7iq/pingme/releases/download/v0.1.6/pingme_Linux_x86_64.tar.gz +tar -xf pingme_Linux_x86_64.tar.gz +chmod +x pingme +sudo mv +``` + ## Go Get ```bash go get -u github.com/kha7iq/pingme @@ -29,7 +37,7 @@ Checkout [release](https://github.com/kha7iq/pingme/releases) page for available ```bash docker pull khaliq/pingme:latest ``` -- Github Registry +- GitHub Registry ```bash docker pull ghcr.io/kha7iq/pingme:latest ``` @@ -39,7 +47,7 @@ docker run ghcr.io/kha7iq/pingme:latest ``` -## Github Action +## GitHub Action A github action is also available now for this app, you can find it on [Github Market Place](https://github.com/marketplace/actions/pingme-action) or from this [repository](https://github.com/kha7iq/pingme-action) on github. Usage examples for workflow are available in the repo. diff --git a/docs/services.md b/docs/services.md index 1d893e4..50eb2f3 100644 --- a/docs/services.md +++ b/docs/services.md @@ -19,7 +19,7 @@ Multiple channel ids can be used separated by comma ','. pingme telegram --token "0125:AAFHvnYf_ABC" --msg "This is a new message ✈" --channel="-1001001001,-1002002001" ``` -- Github Action +- GitHub Action ```yaml on: [push] @@ -37,8 +37,8 @@ jobs: env: TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_TOKEN }} TELEGRAM_CHANNELS: ${{ secrets.TELEGRAM_CHANNELS }} - TELEGRAM_TITLE: 'Refrence: ${{ github.ref }}' - TELEGRAM_MESSAGE: 'Event is triggerd by ${{ github.event_name }}' + TELEGRAM_TITLE: 'Reference: ${{ github.ref }}' + TELEGRAM_MESSAGE: 'Event is triggered by ${{ github.event_name }}' with: # Chose the messaging platform. @@ -88,8 +88,8 @@ jobs: ROCKETCHAT_SERVER_URL: ${{ secrets.ROCKETCHAT_SERVER_URL }} ROCKETCHAT_CHANNELS: ${{ secrets.ROCKETCHAT_CHANNELS }} ROCKETCHAT_URL_SCHEME: "https" - ROCKETCHAT_TITLE: 'Refrence: ${{ github.ref }}' - ROCKETCHAT_MESSAGE: 'Event is triggerd by ${{ github.event_name }}' + ROCKETCHAT_TITLE: 'Reference: ${{ github.ref }}' + ROCKETCHAT_MESSAGE: 'Event is triggered by ${{ github.event_name }}' with: # Chose the messaging platform. # slack / telegram / rocketchat / teams / pushover / discord / email / mattermost @@ -114,7 +114,7 @@ jobs: pingme pushover --token '123' --user '12345567' --title 'some title' --msg 'some message' ``` -- Github Action +- GitHub Action ```yaml on: [push] @@ -132,8 +132,8 @@ jobs: env: PUSHOVER_TOKEN: ${{ secrets.PUSHOVER_TOKEN }} PUSHOVER_USER: ${{ secrets.PUSHOVER_USER }} - PUSHOVER_TITLE: 'Refrence: ${{ github.ref }}' - PUSHOVER_MESSAGE: 'Event is triggerd by ${{ github.event_name }}' + PUSHOVER_TITLE: 'Reference: ${{ github.ref }}' + PUSHOVER_MESSAGE: 'Event is triggered by ${{ github.event_name }}' with: # Chose the messaging platform. @@ -161,7 +161,7 @@ You can specify multiple channels by separating the value with ','. pingme mattermost --token '123' --channel '12345,567' --url 'localhost' --scheme 'http' --msg 'some message' ``` -- Github Action +- GitHub Action ```yaml on: @@ -179,11 +179,11 @@ jobs: uses: kha7iq/pingme-action@v1 env: MATTERMOST_TOKEN: ${{ secrets.MATTERMOST_TOKEN }} - ROCKETCHAT_SERVER_URL: ${{ secrets.ROCKETCHAT_SERVER_URL }} + MATTERMOST_SERVER_URL: ${{ secrets.MATTERMOST_SERVER_URL }} MATTERMOST_CHANNELS: ${{ secrets.MATTERMOST_CHANNELS }} MATTERMOST_CHANNELS: ${{ secrets.MATTERMOST_CHANNELS }} - MATTERMOST_TITLE: 'Refrence: ${{ github.ref }}' - MATTERMOST_MESSAGE: 'Event is triggerd by ${{ github.event_name }}' + MATTERMOST_TITLE: 'Reference: ${{ github.ref }}' + MATTERMOST_MESSAGE: 'Event is triggered by ${{ github.event_name }}' with: # Chose the messaging platform. # slack / telegram / rocketchat / teams / pushover / discord / email / mattermost @@ -227,10 +227,10 @@ jobs: - name: Ping me On uses: kha7iq/pingme-action@v1 env: - PUSHOVER_TOKEN: ${{ secrets.SLACK_TOKEN }} + SLACK_TOKEN: ${{ secrets.SLACK_TOKEN }} SLACK_CHANNELS: ${{ secrets.SLACK_CHANNELS }} - SLACK_MSG_TITLE: 'Refrence: ${{ github.ref }}' - SLACK_MESSAGE: 'Event is triggerd by ${{ github.event_name }}' + SLACK_MSG_TITLE: 'Reference: ${{ github.ref }}' + SLACK_MESSAGE: 'Event is triggered by ${{ github.event_name }}' with: # Chose the messaging platform. # slack / telegram / rocketchat / teams / pushover / discord / email @@ -254,7 +254,7 @@ Multiple channel ids can be used separated by comma ','. pingme discord --token '123' --channel '1234567890' --msg 'some message' ``` -- Github Action +- GitHub Action ```yaml on: @@ -273,8 +273,8 @@ jobs: env: DISCORD_CHANNELS: ${{ secrets.DISCORD_CHANNELS }} DISCORD_TOKEN: ${{ secrets.DISCORD_TOKEN }} - DISCORD_TITLE: 'Refrence: ${{ github.ref }}' - DISCORD_MESSAGE: 'Event is triggerd by ${{ github.event_name }}' + DISCORD_TITLE: 'Reference: ${{ github.ref }}' + DISCORD_MESSAGE: 'Event is triggered by ${{ github.event_name }}' with: # Chose the messaging platform. # slack / telegram / rocketchat / teams / pushover / discord / email / mattermost @@ -298,7 +298,7 @@ you can add permissions for multiple channels to single webhook. pingme teams --webhook 'https://example.webhook.office.com/xx' --msg 'some message' ``` -- Github Action +- GitHub Action ```yaml on: [push] @@ -315,9 +315,8 @@ jobs: uses: kha7iq/pingme-action@v1 env: TEAMS_WEBHOOK: ${{ secrets.TEAMS_WEBHOOK }} - TELEGRAM_CHANNELS: ${{ secrets.TELEGRAM_CHANNELS }} - TEAMS_MSG_TITLE: 'Refrence: ${{ github.ref }}' - TEAMS_MESSAGE: 'Event is triggerd by ${{ github.event_name }}' + TEAMS_MSG_TITLE: 'Reference: ${{ github.ref }}' + TEAMS_MESSAGE: 'Event is triggered by ${{ github.event_name }}' with: # Chose the messaging platform. @@ -332,6 +331,56 @@ jobs: | TEAMS_MESSAGE | "" | | TEAMS_MSG_TITLE | "" | +## Pushbullet + +- SMS +```bash +pingme pushbullet --sms true --token "abcdefg" -d "adnroid" --msg "some message" --number "00123456789" +``` + +- Push notification +```bash +pingme pushbullet --token "abcdefg" -d "adnroid" --msg "some message" +``` + +- GitHub Action + +```yaml +on: [push] + +jobs: + pingme-job: + runs-on: ubuntu-latest + name: PingMe + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Ping me On + uses: kha7iq/pingme-action@v1 + env: + PUSHBULLET_TOKEN: ${{ secrets.PUSHBULLET_TOKEN }} + PUSHBULLET_DEVICE: ${{ secrets.PUSHBULLET_DEVICE }} + PUSHBULLET_TITLE: 'Reference: ${{ github.ref }}' + PUSHBULLET_MESSAGE: 'Event is triggered by ${{ github.event_name }}' + + with: + # Chose the messaging platform. + # slack / telegram / rocketchat / teams / pushover / discord / email + service: pushbullet +``` + +- **Variables** + +| Variables | Default Value | +| -------------------------- | :----------------: | +| PUSHBULLET_TOKEN | "" | +| PUSHBULLET_DEVICE | "" | +| PUSHBULLET_NUMBER | "" | +| PUSHBULLET_MESSAGE | "" | +| PUSHBULLET_SMS | "false" | +| PUSHBULLET_TITLE | "" | + ## Email Email uses username & password to authenticate for sending emails. From a7ff2e461ef0685e2a5b00bafe298b4526513f69 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Wed, 28 Apr 2021 00:58:50 +0800 Subject: [PATCH 03/23] fix: lint code --- cmd/mattermost.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/mattermost.go b/cmd/mattermost.go index 9f374ce..e858567 100644 --- a/cmd/mattermost.go +++ b/cmd/mattermost.go @@ -146,7 +146,6 @@ You can specify multiple channels by separating the value with ','.`, // toJson takes strings and convert them to json byte array func toJson(channel string, msg string) ([]byte, error) { - m := make(map[string]string, 2) m["channel_id"] = channel m["message"] = msg From 386c183c86b2ef088c5ee43ddec15eda441d3b00 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Wed, 28 Apr 2021 01:00:37 +0800 Subject: [PATCH 04/23] fix: exclude readme and license file from release --- .goreleaser.yaml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index fa6ba45..086b712 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -40,9 +40,6 @@ archives: format_overrides: - goos: windows format: zip - files: - - README.md - - LICENSE.md brews: - tap: From 575cd2e6b97863ea46f5f3d50323b09ca255ba29 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Wed, 28 Apr 2021 01:01:19 +0800 Subject: [PATCH 05/23] feat: add new service pushbullet --- cmd/pushbullet.go | 137 ++++++++++++++++++++++++++++++++++++++++++++++ go.sum | 1 + main.go | 3 +- 3 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 cmd/pushbullet.go diff --git a/cmd/pushbullet.go b/cmd/pushbullet.go new file mode 100644 index 0000000..c19b9ef --- /dev/null +++ b/cmd/pushbullet.go @@ -0,0 +1,137 @@ +package cmd + +import ( + "context" + "fmt" + "log" + "strings" + + "github.com/nikoksr/notify" + "github.com/nikoksr/notify/service/pushbullet" + "github.com/urfave/cli/v2" +) + +// pushBullet struct holds data parsed via flags for pushbullet service. +type pushBullet struct { + Token string + Message string + Title string + Device string + PhoneNumber string + SMS bool +} + +// SendToPushBullet parse values from *cli.context and return *cli.Command. +// Values include pushbullet token, Device, phone number, Message and Title. +// If multiple devices are provided they the string is split with "," separator and +// each device is added to receiver. +func SendToPushBullet() *cli.Command { + var pushBulletOpts pushBullet + return &cli.Command{ + Name: "pushbullet", + Usage: "Send message to pushbullet", + Description: `Pushbullet uses API token to authenticate & send messages to defined devices. +Multiple device nicknames or numbers can be used separated by comma.`, + UsageText: "pingme pushbullet --token '123' --device 'Web123, myAndroid' --msg 'some message'\n" + + "pingme pushbullet --token '123' --sms true --device 'Web123' --msg 'some message' --number '00123456789'", + Flags: []cli.Flag{ + &cli.StringFlag{ + Destination: &pushBulletOpts.Token, + Name: "token", + Aliases: []string{"t"}, + Required: true, + Usage: "Token of pushbullet api used for sending message.", + EnvVars: []string{"PUSHBULLET_TOKEN"}, + }, + &cli.StringFlag{ + Destination: &pushBulletOpts.Device, + Name: "device", + Aliases: []string{"d"}, + Required: true, + Usage: "Device's nickname of pushbullet.", + EnvVars: []string{"PUSHBULLET_DEVICE"}, + }, + &cli.StringFlag{ + Destination: &pushBulletOpts.PhoneNumber, + Name: "number", + Aliases: []string{"n"}, + Usage: "Target phone number", + EnvVars: []string{"PUSHBULLET_NUMBER"}, + }, + &cli.StringFlag{ + Destination: &pushBulletOpts.Message, + Name: "msg", + Aliases: []string{"m"}, + Usage: "Message content.", + EnvVars: []string{"PUSHBULLET_MESSAGE"}, + }, + &cli.StringFlag{ + Destination: &pushBulletOpts.Title, + Name: "title", + Value: TimeValue, + Usage: "Title of the message.", + EnvVars: []string{"PUSHBULLET_TITLE"}, + }, + &cli.BoolFlag{ + Destination: &pushBulletOpts.SMS, + Name: "sms", + Value: false, + Usage: "To send sms message set the value to 'true'", + EnvVars: []string{"PUSHBULLET_SMS"}, + }, + }, + Action: func(ctx *cli.Context) error { + notifier := notify.New() + + switch pushBulletOpts.SMS { + case true: + pushBulletSmsSvc, err := pushbullet.NewSMS(pushBulletOpts.Token, pushBulletOpts.Device) + if err != nil { + return err + } + devices := strings.Split(pushBulletOpts.PhoneNumber, ",") + for _, v := range devices { + if len(v) <= 0 { + return fmt.Errorf(EmptyChannel) + } + pushBulletSmsSvc.AddReceivers(v) + + notifier.UseServices(pushBulletSmsSvc) + + if err := notifier.Send( + context.Background(), + pushBulletOpts.Title, + pushBulletOpts.Message, + ); err != nil { + return err + } + + } + default: + pushBulletSvc := pushbullet.New(pushBulletOpts.Token) + + devices := strings.Split(pushBulletOpts.Device, ",") + for _, v := range devices { + if len(v) <= 0 { + return fmt.Errorf(EmptyChannel) + } + pushBulletSvc.AddReceivers(v) + } + + notifier.UseServices(pushBulletSvc) + + if err := notifier.Send( + context.Background(), + pushBulletOpts.Title, + pushBulletOpts.Message, + ); err != nil { + return err + } + + } + + log.Println("Successfully sent!") + return nil + }, + } +} diff --git a/go.sum b/go.sum index 5df3046..42a79a1 100644 --- a/go.sum +++ b/go.sum @@ -32,6 +32,7 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM= github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/cschomburg/go-pushbullet v0.0.0-20171206132031-67759df45fbb h1:7X9nrm+LNWdxzQOiCjy0G51rNUxbH35IDHCjAMvogyM= github.com/cschomburg/go-pushbullet v0.0.0-20171206132031-67759df45fbb/go.mod h1:RfQ9wji3fjcSEsQ+uFCtIh3+BXgcZum8Kt3JxvzYzlk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/main.go b/main.go index 36064b5..e789cde 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,7 @@ func main() { app.Description = `PingMe is a CLI tool which provides the ability to send messages or alerts to multiple messaging platforms and also email, everything is configurable via environment variables and command line switches.Currently supported platforms include Slack, Telegram, -RocketChat, Discord, Pushover, Mattermost, Microsoft Teams and email address.` +RocketChat, Discord, Pushover, Mattermost, Pushbullet, Microsoft Teams and email address.` // app.Commands contains the subcommands as functions which return []*cli.Command. app.Commands = []*cli.Command{ cmd.SendToTelegram(), @@ -32,6 +32,7 @@ RocketChat, Discord, Pushover, Mattermost, Microsoft Teams and email address.` cmd.SendToPushOver(), cmd.SendToEmail(), cmd.SendToMattermost(), + cmd.SendToPushBullet(), } err := app.Run(os.Args) From ce5cfe4375cbc6db122e3a2ef6fec0fe45466467 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Wed, 28 Apr 2021 22:10:28 +0800 Subject: [PATCH 06/23] chor: add linters config --- .golangci.yml | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..36077e2 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,96 @@ +linters-settings: + depguard: + list-type: blacklist + funlen: + lines: 120 + statements: 50 + gci: + local-prefixes: github.com/golangci/golangci-lint + goconst: + min-len: 2 + min-occurrences: 2 + gocritic: + enabled-tags: + - diagnostic + - experimental + - opinionated + - performance + - style + disabled-checks: + - ifElseChain + - octalLiteral + - whyNoLint + - wrapperFunc + gocyclo: + min-complexity: 15 + goimports: + local-prefixes: github.com/golangci/golangci-lint + golint: + min-confidence: 0 + gomnd: + settings: + mnd: + # don't include the "operation" and "assign" + checks: argument,case,condition,return + govet: + check-shadowing: true + settings: + printf: + funcs: + - (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof + - (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf + - (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf + - (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf + lll: + line-length: 140 + maligned: + suggest-new: true + misspell: + locale: US + nolintlint: + allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space) + allow-unused: false # report any unused nolint directives + require-explanation: false # don't require an explanation for nolint directives + require-specific: false # don't require nolint directives to be specific about which linter is being skipped + +linters: + disable-all: true + enable: + - bodyclose + - deadcode + - depguard + - dogsled + - errcheck + - funlen + - gochecknoinits + - goconst + - gocyclo + - gofmt + - goimports + - golint + - goprintffuncname + - gosec + - gosimple + - govet + - ineffassign + - lll + - misspell + - nakedret + - rowserrcheck + - staticcheck + - structcheck + - stylecheck + - typecheck + - unconvert + - unparam + - unused + - varcheck + - whitespace + + +# issues: +# # Excluding configuration per-path, per-linter, per-text and per-source +# exclude-rules: +# - path: _test\.go +# linters: +# - gomnd From 603aa5ee9e2fc0bb2b7a5e1ba5ccd225c98e8893 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Wed, 28 Apr 2021 22:11:15 +0800 Subject: [PATCH 07/23] chor: add workflow for linting checks on pr --- .github/workflows/pull_request.yml | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 .github/workflows/pull_request.yml diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml new file mode 100644 index 0000000..4f51f0d --- /dev/null +++ b/.github/workflows/pull_request.yml @@ -0,0 +1,36 @@ +on: pull_request + +env: + PUSHOVER_TOKEN: ${{ secrets.PUSHOVER_TOKEN }} + PUSHOVER_USER: ${{ secrets.PUSHOVER_USER }} + +jobs: + lint-code: + runs-on: self-hosted + name: PingMe + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: golangci-lint + uses: golangci/golangci-lint-action@v2 + with: + version: v1.29 + + - name: Notification for checks failure + uses: kha7iq/pingme-action@v1 + if: failure() + env: + PUSHOVER_TITLE: '🟢 New Request: ${{ github.ref }}' + PUSHOVER_MESSAGE: 'Event is triggerd by ${{ github.event_name }} Checks ❌ ${{ job.status }}' + with: + service: pushover + + - name: Notification for checks passing + uses: kha7iq/pingme-action@v1 + if: success() + env: + PUSHOVER_TITLE: '🟢 New Request: ${{ github.ref }}' + PUSHOVER_MESSAGE: 'Event is triggerd by ${{ github.event_name }} Checks ✅ ${{ job.status }}' + with: + service: pushover From edd49a3f167d6c86d0ad41d74b676083f8333630 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Wed, 28 Apr 2021 22:12:47 +0800 Subject: [PATCH 08/23] style: resovle linting issues --- cmd/discord.go | 2 +- cmd/mattermost.go | 11 +++++------ cmd/msteams.go | 1 - cmd/pushbullet.go | 2 -- cmd/rocketchat.go | 6 +++--- cmd/telegram.go | 6 +++--- 6 files changed, 12 insertions(+), 16 deletions(-) diff --git a/cmd/discord.go b/cmd/discord.go index d83f11a..964fb31 100644 --- a/cmd/discord.go +++ b/cmd/discord.go @@ -66,7 +66,7 @@ All configuration options are also available via environment variables.`, discordSvc := discord.New() if err := discordSvc.AuthenticateWithBotToken(discordOpts.Token); err != nil { - return fmt.Errorf("unable to authenticate %v\n", err) + return fmt.Errorf("unable to authenticate %v", err) } chn := strings.Split(discordOpts.Channel, ",") diff --git a/cmd/mattermost.go b/cmd/mattermost.go index e858567..449775a 100644 --- a/cmd/mattermost.go +++ b/cmd/mattermost.go @@ -18,7 +18,7 @@ type matterMost struct { Token string ServerURL string Scheme string - ApiURL string + APIURL string Message string ChanIDs string } @@ -108,7 +108,7 @@ You can specify multiple channels by separating the value with ','.`, EnvVars: []string{"MATTERMOST_SCHEME"}, }, &cli.StringFlag{ - Destination: &mattermostOpts.ApiURL, + Destination: &mattermostOpts.APIURL, Name: "api", Value: "/api/v4/posts", Usage: "Unless using older version of api default is fine.", @@ -116,7 +116,7 @@ You can specify multiple channels by separating the value with ','.`, }, }, Action: func(ctx *cli.Context) error { - endPointURL := mattermostOpts.Scheme + "://" + mattermostOpts.ServerURL + mattermostOpts.ApiURL + endPointURL := mattermostOpts.Scheme + "://" + mattermostOpts.ServerURL + mattermostOpts.APIURL // Create a Bearer string by appending string access token bearer := "Bearer " + mattermostOpts.Token @@ -129,7 +129,7 @@ You can specify multiple channels by separating the value with ','.`, return fmt.Errorf(EmptyChannel) } - jsonData, err := toJson(v, fullMessage) + jsonData, err := toJSON(v, fullMessage) if err != nil { return fmt.Errorf("error parsing json\n[ERROR] - %v", err) } @@ -137,7 +137,6 @@ You can specify multiple channels by separating the value with ','.`, if err := sendMattermost(endPointURL, bearer, jsonData); err != nil { return fmt.Errorf("failed to send message\n[ERROR] - %v", err) } - } return nil }, @@ -145,7 +144,7 @@ You can specify multiple channels by separating the value with ','.`, } // toJson takes strings and convert them to json byte array -func toJson(channel string, msg string) ([]byte, error) { +func toJSON(channel string, msg string) ([]byte, error) { m := make(map[string]string, 2) m["channel_id"] = channel m["message"] = msg diff --git a/cmd/msteams.go b/cmd/msteams.go index 78cead2..d0fac89 100644 --- a/cmd/msteams.go +++ b/cmd/msteams.go @@ -63,7 +63,6 @@ you can add permissions for multiple channels to single webhook.`, return fmt.Errorf(EmptyChannel) } teamsSvc.AddReceivers(v) - } notifier.UseServices(teamsSvc) diff --git a/cmd/pushbullet.go b/cmd/pushbullet.go index c19b9ef..6477ee4 100644 --- a/cmd/pushbullet.go +++ b/cmd/pushbullet.go @@ -105,7 +105,6 @@ Multiple device nicknames or numbers can be used separated by comma.`, ); err != nil { return err } - } default: pushBulletSvc := pushbullet.New(pushBulletOpts.Token) @@ -127,7 +126,6 @@ Multiple device nicknames or numbers can be used separated by comma.`, ); err != nil { return err } - } log.Println("Successfully sent!") diff --git a/cmd/rocketchat.go b/cmd/rocketchat.go index e024208..5ed4318 100644 --- a/cmd/rocketchat.go +++ b/cmd/rocketchat.go @@ -13,7 +13,7 @@ import ( type rocketChat struct { Token string - UserId string + UserID string Message string Channel string Title string @@ -43,7 +43,7 @@ All configuration options are also available via environment variables.`, " --channel 'alert' --msg 'some message'", Flags: []cli.Flag{ &cli.StringFlag{ - Destination: &rocketChatOpts.UserId, + Destination: &rocketChatOpts.UserID, Name: "userid", Aliases: []string{"id"}, Required: true, @@ -99,7 +99,7 @@ All configuration options are also available via environment variables.`, notifier := notify.New() rocketChatSvc, err := rocketchat.New(rocketChatOpts.ServerURL, rocketChatOpts.Scheme, - rocketChatOpts.UserId, rocketChatOpts.Token) + rocketChatOpts.UserID, rocketChatOpts.Token) if err != nil { return err } diff --git a/cmd/telegram.go b/cmd/telegram.go index c843452..8e1475f 100644 --- a/cmd/telegram.go +++ b/cmd/telegram.go @@ -77,9 +77,9 @@ All configuration options are also available via environment variables.`, if len(v) <= 0 { return fmt.Errorf(EmptyChannel) } - k, err := strconv.Atoi(v) - if err != nil { - log.Println(err) + k, errStr := strconv.Atoi(v) + if errStr != nil { + return errStr } telegramSvc.AddReceivers(int64(k)) } From cd4b86ef95b9ff3033633aae93167a280de0d4c5 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Wed, 28 Apr 2021 22:14:55 +0800 Subject: [PATCH 09/23] fix: update runner --- .github/workflows/pull_request.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 4f51f0d..95e201c 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -6,7 +6,7 @@ env: jobs: lint-code: - runs-on: self-hosted + runs-on: ubuntu-latest name: PingMe steps: - name: Checkout From d49c824d32fac7f0ee28ae717b9e2427c24a88c0 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Wed, 28 Apr 2021 22:19:53 +0800 Subject: [PATCH 10/23] fix: remove unused linters --- .golangci.yml | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 36077e2..8a7a0f9 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -9,29 +9,12 @@ linters-settings: goconst: min-len: 2 min-occurrences: 2 - gocritic: - enabled-tags: - - diagnostic - - experimental - - opinionated - - performance - - style - disabled-checks: - - ifElseChain - - octalLiteral - - whyNoLint - - wrapperFunc gocyclo: min-complexity: 15 goimports: local-prefixes: github.com/golangci/golangci-lint golint: min-confidence: 0 - gomnd: - settings: - mnd: - # don't include the "operation" and "assign" - checks: argument,case,condition,return govet: check-shadowing: true settings: @@ -86,11 +69,3 @@ linters: - unused - varcheck - whitespace - - -# issues: -# # Excluding configuration per-path, per-linter, per-text and per-source -# exclude-rules: -# - path: _test\.go -# linters: -# - gomnd From d9f690f2ebef90e84911888a173c720708547310 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Wed, 28 Apr 2021 22:35:28 +0800 Subject: [PATCH 11/23] chor: remove unneeded step from fmt --- Makefile | 1 - 1 file changed, 1 deletion(-) diff --git a/Makefile b/Makefile index 6028a13..f4d0a6e 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,6 @@ test: # gofumports and gci all go files fmt: find . -name '*.go' -not -wholename './vendor/*' | while read -r file; do gofumports -w "$$file"; done - gci -w -local github.com/kha7iq/pingme . .PHONY: fmt # Run all the linters From 9b5027207d0311b7e8a8623af23b5aab49ddc09a Mon Sep 17 00:00:00 2001 From: kha7iq Date: Thu, 29 Apr 2021 23:27:01 +0800 Subject: [PATCH 12/23] docs: add minor fixes --- CONTRIBUTING.md | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fa10bfc..2ee5658 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -4,19 +4,20 @@ possible. ## Project structure -- `main` - Contains definitions for the underlying notification services. - - `cmd/discord` - Discord notification service. - - `cmd/email` - Email notification service. - - `cmd/msteams` - Microsoft Teams notification service. - - `cmd/rocketchat` - RocketChat notification service. - - `cmd/slack` - Slack notification service. - - `cmd/telegram` - Telegram notification service +- `service` - Contains definitions for the underlying notification services. + - `service/discord` - Discord notification service. + - `service/email` - Email notification service. + - `service/msteams` - Microsoft Teams notification service. + - `service/rocketchat` - RocketChat notification service. + - `service/slack` - Slack notification service. + - `service/telegram` - Telegram notification service. + - `service/pushover` - Pushover Notification service. - Documentation - `docs` - Contains the documentation in markdown format. - - `services.md` If you are adding a new service please add documentaiton to `services.md`. + `docs` - Contains the documentation in Markdown format. + - `services.md` If you are adding a new service please add documentation to `services.md`. - `home.md` Is the main page rendered when docs website is loaded. - - `install.md` Contains the install instructions for different packages. + - `install.md` Contains the installation instructions for different packages. - Checking Locally - Docsify is used for documentation rendering from markdown, you can download From 9ebc8405d097cb119aa57b71b89aa21f3b9e2bd7 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Thu, 29 Apr 2021 23:28:10 +0800 Subject: [PATCH 13/23] docs: add minor fixes --- CODE_OF_CONDUCT.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index 5c07c54..b4df6c3 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -3,7 +3,7 @@ ## Our Pledge In the interest of fostering an open and welcoming environment, we as -contributors and maintainers pledge to making participation in our project and +contributors and maintainers pledge to making participation in our project, and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal From b59578d9900755a75ae17edcc0ab70a20e93f91c Mon Sep 17 00:00:00 2001 From: kha7iq Date: Thu, 29 Apr 2021 23:29:04 +0800 Subject: [PATCH 14/23] docs: add contributation links --- docs/_sidebar.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/_sidebar.md b/docs/_sidebar.md index aee2dc3..a7e297e 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -3,4 +3,5 @@ * [Home](/) * [Installation](install.md) * [Services & Usage](services.md) +* [Contribution](contribution.md) \ No newline at end of file From 5d9ba57a77e40fac14dc72fde5790633c2578b24 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Thu, 29 Apr 2021 23:30:06 +0800 Subject: [PATCH 15/23] docs: add contributation guide --- docs/contribution.md | 73 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 docs/contribution.md diff --git a/docs/contribution.md b/docs/contribution.md new file mode 100644 index 0000000..2ee5658 --- /dev/null +++ b/docs/contribution.md @@ -0,0 +1,73 @@ +## Contributing to PingMe +We want to make contributing to this project as easy and transparent as +possible. + +## Project structure + +- `service` - Contains definitions for the underlying notification services. + - `service/discord` - Discord notification service. + - `service/email` - Email notification service. + - `service/msteams` - Microsoft Teams notification service. + - `service/rocketchat` - RocketChat notification service. + - `service/slack` - Slack notification service. + - `service/telegram` - Telegram notification service. + - `service/pushover` - Pushover Notification service. + +- Documentation + `docs` - Contains the documentation in Markdown format. + - `services.md` If you are adding a new service please add documentation to `services.md`. + - `home.md` Is the main page rendered when docs website is loaded. + - `install.md` Contains the installation instructions for different packages. + + - Checking Locally + - Docsify is used for documentation rendering from markdown, you can download + the cli and test locally before opening a pull request. + + Install + ```bash + npm i docsify-cli -g + # yarn global add docsify-cli + ``` + Serve locally + ```bash + docsify serve docs + ``` + + +## Commits + +Commit messages should be well formatted, and to make that "standardized", we +are using Conventional Commits. + +```shell + + []: + │ │ │ + │ │ └─> Summary in present tense. Not capitalized. No period at the end. + │ │ + │ └─> Scope (optional): eg. common, compiler, authentication, core + │ + └─> Type: chore, docs, feat, fix, refactor, style, or test. + +``` + +You can follow the documentation on +[their website](https://www.conventionalcommits.org). + +## Pull Requests +We actively welcome your pull requests. + +1. Fork the repo and create your branch from `master`. +2. If you've added code that should be tested, add tests. +3. If you've changed APIs, update the documentation. +4. Ensure the test suite passes (`make test`). +5. Make sure your code lints (`make lint`). +6. Make sure your code is well formatted (`make fmt`). + +## Issues +We use GitHub issues to track public bugs. Please ensure your description is +clear and has sufficient instructions to be able to reproduce the issue. + +## License +By contributing to PingMe, you agree that your contributions will be licensed +under the LICENSE file in the root directory of this source tree. From 158452591ebc6c9a5a95d5ba758bc5c7771b9acc Mon Sep 17 00:00:00 2001 From: kha7iq Date: Thu, 29 Apr 2021 23:30:32 +0800 Subject: [PATCH 16/23] docs: update links --- docs/home.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/home.md b/docs/home.md index d0cb0e4..adc5244 100644 --- a/docs/home.md +++ b/docs/home.md @@ -61,7 +61,7 @@ as message, and most of all this serves as a swiss army knife sort of tool which ## Contributing -Contributions, issues and feature requests are welcome!
Feel free to check [issues page](https://github.com/kha7iq/pingme/issues). You can also take a look at the [contributing guide](https://github.com/kha7iq/pingme/blob/master/CONTRIBUTING.md). +Contributions, issues and feature requests are welcome!
Feel free to check [issues page](https://github.com/kha7iq/pingme/issues). You can also take a look at the [contribution guide](contribution.md). @@ -73,6 +73,6 @@ Give a ⭐️ if you like this project! ## Disclaimer -Any misuse of this utilify is your own liability and responsibility and cannot be attributed to the authors of this library. See [license](LICENSE) for more. +Any misuse of this utility is your own liability and responsibility and cannot be attributed to the authors of this library. See [license](https://github.com/kha7iq/pingme/blob/master/LICENSE.md) for more. Spamming through the use of this library **may get you permanently banned** on most supported platforms. From f1bdc11693dc191f0c20af877c6ce1ab3208bb70 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Thu, 29 Apr 2021 23:33:17 +0800 Subject: [PATCH 17/23] refactor: restructure project for better integration of new services --- main.go | 30 ++++++++++++++--------- {cmd => service/discord}/discord.go | 12 +++++---- {cmd => service/email}/email.go | 13 +++++----- service/helpers/helper.go | 13 ++++++++++ {cmd => service/mattermost}/mattermost.go | 12 +++++---- {cmd => service/msteams}/msteams.go | 13 +++++----- {cmd => service/pushbullet}/pushbullet.go | 15 ++++++------ {cmd => service/pushover}/pushover.go | 13 +++++----- {cmd => service/rocketchat}/rocketchat.go | 19 +++++--------- {cmd => service/slack}/slack.go | 13 +++++----- {cmd => service/telegram}/telegram.go | 13 +++++----- 11 files changed, 95 insertions(+), 71 deletions(-) rename {cmd => service/discord}/discord.go (91%) rename {cmd => service/email}/email.go (94%) create mode 100644 service/helpers/helper.go rename {cmd => service/mattermost}/mattermost.go (96%) rename {cmd => service/msteams}/msteams.go (90%) rename {cmd => service/pushbullet}/pushbullet.go (93%) rename {cmd => service/pushover}/pushover.go (91%) rename {cmd => service/rocketchat}/rocketchat.go (88%) rename {cmd => service/slack}/slack.go (91%) rename {cmd => service/telegram}/telegram.go (91%) diff --git a/main.go b/main.go index e789cde..a64d89c 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,15 @@ import ( "log" "os" - "github.com/kha7iq/pingme/cmd" + "github.com/kha7iq/pingme/service/discord" + "github.com/kha7iq/pingme/service/email" + "github.com/kha7iq/pingme/service/mattermost" + "github.com/kha7iq/pingme/service/msteams" + "github.com/kha7iq/pingme/service/pushbullet" + "github.com/kha7iq/pingme/service/pushover" + "github.com/kha7iq/pingme/service/rocketchat" + "github.com/kha7iq/pingme/service/slack" + "github.com/kha7iq/pingme/service/telegram" "github.com/urfave/cli/v2" ) @@ -12,7 +20,7 @@ import ( // Version variable is used for semVer var Version string -// main with combile all the function into commands +// main with combine all the function into commands func main() { app := cli.NewApp() app.Name = "PingMe" @@ -24,15 +32,15 @@ variables and command line switches.Currently supported platforms include Slack, RocketChat, Discord, Pushover, Mattermost, Pushbullet, Microsoft Teams and email address.` // app.Commands contains the subcommands as functions which return []*cli.Command. app.Commands = []*cli.Command{ - cmd.SendToTelegram(), - cmd.SendToRocketChat(), - cmd.SendToSlack(), - cmd.SendToDiscord(), - cmd.SendToTeams(), - cmd.SendToPushOver(), - cmd.SendToEmail(), - cmd.SendToMattermost(), - cmd.SendToPushBullet(), + telegram.Send(), + rocketchat.Send(), + slack.Send(), + discord.Send(), + msteams.Send(), + pushover.Send(), + email.Send(), + mattermost.Send(), + pushbullet.Send(), } err := app.Run(os.Args) diff --git a/cmd/discord.go b/service/discord/discord.go similarity index 91% rename from cmd/discord.go rename to service/discord/discord.go index 964fb31..1b654c1 100644 --- a/cmd/discord.go +++ b/service/discord/discord.go @@ -1,4 +1,4 @@ -package cmd +package discord import ( "context" @@ -6,6 +6,8 @@ import ( "log" "strings" + "github.com/kha7iq/pingme/service/helpers" + "github.com/nikoksr/notify" "github.com/nikoksr/notify/service/discord" "github.com/urfave/cli/v2" @@ -19,11 +21,11 @@ type discordPingMe struct { Title string } -// SendToDiscord parse values from *cli.context and return *cli.Command. +// Send parse values from *cli.context and return *cli.Command. // Values include discord bot token, userID, channelIDs, Message and Title. // If multiple channels are provided then the string is split with "," separator and // each channelID is added to receiver. -func SendToDiscord() *cli.Command { +func Send() *cli.Command { var discordOpts discordPingMe return &cli.Command{ Name: "discord", @@ -56,7 +58,7 @@ All configuration options are also available via environment variables.`, &cli.StringFlag{ Destination: &discordOpts.Title, Name: "title", - Value: TimeValue, + Value: helpers.TimeValue, Usage: "Title of the message.", EnvVars: []string{"DISCORD_MSG_TITLE"}, }, @@ -72,7 +74,7 @@ All configuration options are also available via environment variables.`, chn := strings.Split(discordOpts.Channel, ",") for _, v := range chn { if len(v) <= 0 { - return fmt.Errorf(EmptyChannel) + return helpers.ErrChannel } discordSvc.AddReceivers(v) diff --git a/cmd/email.go b/service/email/email.go similarity index 94% rename from cmd/email.go rename to service/email/email.go index fda5267..aae64e2 100644 --- a/cmd/email.go +++ b/service/email/email.go @@ -1,11 +1,12 @@ -package cmd +package email import ( "context" - "fmt" "log" "strings" + "github.com/kha7iq/pingme/service/helpers" + "github.com/nikoksr/notify" "github.com/nikoksr/notify/service/mail" "github.com/urfave/cli/v2" @@ -23,12 +24,12 @@ type email struct { Identity string } -// SendToEmail parse values from *cli.context and return *cli.Command. +// Send parses values from *cli.context and return *cli.Command. // SendAddress is used for authentication with smtp server, host and port is required // the default value for port is set to "587" and host as "smtp.gmail.com" // If multiple ReceiverAddress are provided then the string value is split with "," separator and // each ReceiverAddress is added to receiver. -func SendToEmail() *cli.Command { +func Send() *cli.Command { var emailOpts email return &cli.Command{ Name: "email", @@ -98,7 +99,7 @@ All configuration options are also available via environment variables.`, &cli.StringFlag{ Destination: &emailOpts.Subject, Name: "sub", - Value: TimeValue, + Value: helpers.TimeValue, Usage: "Subject of the email", EnvVars: []string{"EMAIL_SUBJECT"}, }, @@ -111,7 +112,7 @@ All configuration options are also available via environment variables.`, chn := strings.Split(emailOpts.ReceiverAddress, ",") for _, v := range chn { if len(v) <= 0 { - return fmt.Errorf(EmptyChannel) + return helpers.ErrChannel } emailSvc.AddReceivers(v) diff --git a/service/helpers/helper.go b/service/helpers/helper.go new file mode 100644 index 0000000..2779475 --- /dev/null +++ b/service/helpers/helper.go @@ -0,0 +1,13 @@ +package helpers + +import ( + "errors" + "time" +) + +var ( + // ErrChannel variable holds default error message if no channel is provided. + ErrChannel = errors.New("target channel or id can not be empty") + // TimeValue holds current date and time in unix format. + TimeValue = "⏰ " + time.Now().Format(time.UnixDate) +) diff --git a/cmd/mattermost.go b/service/mattermost/mattermost.go similarity index 96% rename from cmd/mattermost.go rename to service/mattermost/mattermost.go index 449775a..bc8566d 100644 --- a/cmd/mattermost.go +++ b/service/mattermost/mattermost.go @@ -1,4 +1,4 @@ -package cmd +package mattermost import ( "bytes" @@ -9,6 +9,8 @@ import ( "strings" "time" + "github.com/kha7iq/pingme/service/helpers" + "github.com/urfave/cli/v2" ) @@ -47,11 +49,11 @@ type matterMostResponse struct { Metadata struct{} `json:"metadata"` } -// SendToMattermost parse values from *cli.context and return *cli.Command +// Send parse values from *cli.context and return *cli.Command // and send messages to target channels. // If multiple channel ids are provided then the string is split with "," separator and // message is sent to each channel. -func SendToMattermost() *cli.Command { +func Send() *cli.Command { var mattermostOpts matterMost return &cli.Command{ Name: "mattermost", @@ -87,7 +89,7 @@ You can specify multiple channels by separating the value with ','.`, &cli.StringFlag{ Destination: &mattermostOpts.Title, Name: "title", - Value: TimeValue, + Value: helpers.TimeValue, Usage: "Title of the message.", EnvVars: []string{"MATTERMOST_TITLE"}, }, @@ -126,7 +128,7 @@ You can specify multiple channels by separating the value with ','.`, ids := strings.Split(mattermostOpts.ChanIDs, ",") for _, v := range ids { if len(v) == 0 { - return fmt.Errorf(EmptyChannel) + return helpers.ErrChannel } jsonData, err := toJSON(v, fullMessage) diff --git a/cmd/msteams.go b/service/msteams/msteams.go similarity index 90% rename from cmd/msteams.go rename to service/msteams/msteams.go index d0fac89..e109441 100644 --- a/cmd/msteams.go +++ b/service/msteams/msteams.go @@ -1,11 +1,12 @@ -package cmd +package msteams import ( "context" - "fmt" "log" "strings" + "github.com/kha7iq/pingme/service/helpers" + "github.com/nikoksr/notify" msteams2 "github.com/nikoksr/notify/service/msteams" "github.com/urfave/cli/v2" @@ -18,11 +19,11 @@ type msTeams struct { Title string } -// SendToTeams parse values from *cli.context and return *cli.Command. +// Send parse values from *cli.context and return *cli.Command. // Values include Ms Teams Webhook, Message and Title. // If multiple webhooks are provided then the string is split with "," separator and // each webhook is added to receiver. -func SendToTeams() *cli.Command { +func Send() *cli.Command { var msTeamOpt msTeams return &cli.Command{ Name: "teams", @@ -48,7 +49,7 @@ you can add permissions for multiple channels to single webhook.`, &cli.StringFlag{ Destination: &msTeamOpt.Title, Name: "title", - Value: TimeValue, + Value: helpers.TimeValue, Usage: "Title of the message.", EnvVars: []string{"TEAMS_MSG_TITLE"}, }, @@ -60,7 +61,7 @@ you can add permissions for multiple channels to single webhook.`, chn := strings.Split(msTeamOpt.Webhook, ",") for _, v := range chn { if len(v) <= 0 { - return fmt.Errorf(EmptyChannel) + return helpers.ErrChannel } teamsSvc.AddReceivers(v) } diff --git a/cmd/pushbullet.go b/service/pushbullet/pushbullet.go similarity index 93% rename from cmd/pushbullet.go rename to service/pushbullet/pushbullet.go index 6477ee4..a358c0a 100644 --- a/cmd/pushbullet.go +++ b/service/pushbullet/pushbullet.go @@ -1,11 +1,12 @@ -package cmd +package pushbullet import ( "context" - "fmt" "log" "strings" + "github.com/kha7iq/pingme/service/helpers" + "github.com/nikoksr/notify" "github.com/nikoksr/notify/service/pushbullet" "github.com/urfave/cli/v2" @@ -21,11 +22,11 @@ type pushBullet struct { SMS bool } -// SendToPushBullet parse values from *cli.context and return *cli.Command. +// Send parse values from *cli.context and return *cli.Command. // Values include pushbullet token, Device, phone number, Message and Title. // If multiple devices are provided they the string is split with "," separator and // each device is added to receiver. -func SendToPushBullet() *cli.Command { +func Send() *cli.Command { var pushBulletOpts pushBullet return &cli.Command{ Name: "pushbullet", @@ -68,7 +69,7 @@ Multiple device nicknames or numbers can be used separated by comma.`, &cli.StringFlag{ Destination: &pushBulletOpts.Title, Name: "title", - Value: TimeValue, + Value: helpers.TimeValue, Usage: "Title of the message.", EnvVars: []string{"PUSHBULLET_TITLE"}, }, @@ -92,7 +93,7 @@ Multiple device nicknames or numbers can be used separated by comma.`, devices := strings.Split(pushBulletOpts.PhoneNumber, ",") for _, v := range devices { if len(v) <= 0 { - return fmt.Errorf(EmptyChannel) + return helpers.ErrChannel } pushBulletSmsSvc.AddReceivers(v) @@ -112,7 +113,7 @@ Multiple device nicknames or numbers can be used separated by comma.`, devices := strings.Split(pushBulletOpts.Device, ",") for _, v := range devices { if len(v) <= 0 { - return fmt.Errorf(EmptyChannel) + return helpers.ErrChannel } pushBulletSvc.AddReceivers(v) } diff --git a/cmd/pushover.go b/service/pushover/pushover.go similarity index 91% rename from cmd/pushover.go rename to service/pushover/pushover.go index dfed0e7..3d4adb1 100644 --- a/cmd/pushover.go +++ b/service/pushover/pushover.go @@ -1,10 +1,11 @@ -package cmd +package pushover import ( - "fmt" "log" "strings" + "github.com/kha7iq/pingme/service/helpers" + "github.com/gregdel/pushover" "github.com/urfave/cli/v2" ) @@ -17,11 +18,11 @@ type pushOver struct { Title string } -// SendToPushOver parse values from *cli.context and return *cli.Command. +// Send parse values from *cli.context and return *cli.Command. // Values include token, users, Message and Title. // If multiple users are provided then the string is split with "," separator and // each user is added to receiver. -func SendToPushOver() *cli.Command { +func Send() *cli.Command { var pushOverOpts pushOver return &cli.Command{ Name: "pushover", @@ -56,7 +57,7 @@ All configuration options are also available via environment variables.`, &cli.StringFlag{ Destination: &pushOverOpts.Title, Name: "title", - Value: TimeValue, + Value: helpers.TimeValue, Usage: "Title of the message.", EnvVars: []string{"PUSHOVER_TITLE"}, }, @@ -68,7 +69,7 @@ All configuration options are also available via environment variables.`, for _, v := range users { if len(v) == 0 { - return fmt.Errorf(EmptyChannel) + return helpers.ErrChannel } recipient := pushover.NewRecipient(v) responsePushOver, err := app.SendMessage(message, recipient) diff --git a/cmd/rocketchat.go b/service/rocketchat/rocketchat.go similarity index 88% rename from cmd/rocketchat.go rename to service/rocketchat/rocketchat.go index 5ed4318..7efcfba 100644 --- a/cmd/rocketchat.go +++ b/service/rocketchat/rocketchat.go @@ -1,11 +1,10 @@ -package cmd +package rocketchat import ( "context" - "fmt" "strings" - "time" + "github.com/kha7iq/pingme/service/helpers" "github.com/nikoksr/notify" "github.com/nikoksr/notify/service/rocketchat" "github.com/urfave/cli/v2" @@ -21,17 +20,11 @@ type rocketChat struct { Scheme string } -var ( - // EmptyChannel variable holds default error message if no channel is provided. - EmptyChannel = "channel name or id can not be empty" - TimeValue = "⏰ " + time.Now().Format(time.UnixDate) -) - -// SendToRocketChat parse values from *cli.context and return *cli.Command. +// Send parse values from *cli.context and return *cli.Command. // Values include rocketchat token, , UserId, channelIDs, ServerURL, Scheme, Message and Title. // If multiple channels are provided then the string is split with "," separator and // each channelID is added to receiver. -func SendToRocketChat() *cli.Command { +func Send() *cli.Command { var rocketChatOpts rocketChat return &cli.Command{ Name: "rocketchat", @@ -90,7 +83,7 @@ All configuration options are also available via environment variables.`, &cli.StringFlag{ Destination: &rocketChatOpts.Title, Name: "title", - Value: TimeValue, + Value: helpers.TimeValue, Usage: "Title of the message", EnvVars: []string{"ROCKETCHAT_TITLE"}, }, @@ -106,7 +99,7 @@ All configuration options are also available via environment variables.`, chn := strings.Split(rocketChatOpts.Channel, ",") for _, v := range chn { if len(v) <= 0 { - return fmt.Errorf(EmptyChannel) + return helpers.ErrChannel } rocketChatSvc.AddReceivers(v) diff --git a/cmd/slack.go b/service/slack/slack.go similarity index 91% rename from cmd/slack.go rename to service/slack/slack.go index d47d77e..4cc5709 100644 --- a/cmd/slack.go +++ b/service/slack/slack.go @@ -1,11 +1,12 @@ -package cmd +package slack import ( "context" - "fmt" "log" "strings" + "github.com/kha7iq/pingme/service/helpers" + "github.com/nikoksr/notify" "github.com/nikoksr/notify/service/slack" "github.com/urfave/cli/v2" @@ -19,11 +20,11 @@ type slackPingMe struct { Title string } -// SendToSlack parse values from *cli.context and return *cli.Command. +// Send parse values from *cli.context and return *cli.Command. // Values include slack token, channelIDs, Message and Title. // If multiple channels are provided then the string is split with "," separator and // each channelID is added to receiver. -func SendToSlack() *cli.Command { +func Send() *cli.Command { var slackOpts slackPingMe return &cli.Command{ Name: "slack", @@ -59,7 +60,7 @@ All configuration options are also available via environment variables.`, &cli.StringFlag{ Destination: &slackOpts.Title, Name: "title", - Value: TimeValue, + Value: helpers.TimeValue, Usage: "Title of the message.", EnvVars: []string{"SLACK_MSG_TITLE"}, }, @@ -70,7 +71,7 @@ All configuration options are also available via environment variables.`, chn := strings.Split(slackOpts.Channel, ",") for _, v := range chn { if len(v) <= 0 { - return fmt.Errorf(EmptyChannel) + return helpers.ErrChannel } slackSvc.AddReceivers(v) diff --git a/cmd/telegram.go b/service/telegram/telegram.go similarity index 91% rename from cmd/telegram.go rename to service/telegram/telegram.go index 8e1475f..ecd5c21 100644 --- a/cmd/telegram.go +++ b/service/telegram/telegram.go @@ -1,12 +1,13 @@ -package cmd +package telegram import ( "context" - "fmt" "log" "strconv" "strings" + "github.com/kha7iq/pingme/service/helpers" + "github.com/nikoksr/notify" "github.com/nikoksr/notify/service/telegram" "github.com/urfave/cli/v2" @@ -20,11 +21,11 @@ type teleGram struct { Title string } -// SendToTelegram parse values from *cli.context and return *cli.Command. +// Send parse values from *cli.context and return *cli.Command. // Values include telegram token, channelIDs, Message and Title. // If multiple channels are provided they the string is split with "," separator and // each channelID is added to receiver. -func SendToTelegram() *cli.Command { +func Send() *cli.Command { var telegramOpts teleGram return &cli.Command{ Name: "telegram", @@ -60,7 +61,7 @@ All configuration options are also available via environment variables.`, &cli.StringFlag{ Destination: &telegramOpts.Title, Name: "title", - Value: TimeValue, + Value: helpers.TimeValue, Usage: "Title of the message.", EnvVars: []string{"TELEGRAM_TITLE"}, }, @@ -75,7 +76,7 @@ All configuration options are also available via environment variables.`, chn := strings.Split(telegramOpts.Channel, ",") for _, v := range chn { if len(v) <= 0 { - return fmt.Errorf(EmptyChannel) + return helpers.ErrChannel } k, errStr := strconv.Atoi(v) if errStr != nil { From b78c708bc7f5eaaf8f422e6e7090e5c3cdfd403c Mon Sep 17 00:00:00 2001 From: kha7iq Date: Fri, 30 Apr 2021 03:38:24 +0800 Subject: [PATCH 18/23] chore: change job name --- .github/workflows/pull_request.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 95e201c..9cc4b59 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -7,7 +7,7 @@ env: jobs: lint-code: runs-on: ubuntu-latest - name: PingMe + name: Perform Checks steps: - name: Checkout uses: actions/checkout@v2 @@ -17,7 +17,7 @@ jobs: with: version: v1.29 - - name: Notification for checks failure + - name: Notify on failure uses: kha7iq/pingme-action@v1 if: failure() env: @@ -26,7 +26,7 @@ jobs: with: service: pushover - - name: Notification for checks passing + - name: Notify on success uses: kha7iq/pingme-action@v1 if: success() env: From bf9c0f12b05bc8342be6313fe5496a1b6325b34f Mon Sep 17 00:00:00 2001 From: kha7iq Date: Mon, 3 May 2021 02:06:23 +0800 Subject: [PATCH 19/23] docs: add twillio to supported services --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 72621a6..a39b09e 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ as message, and most of all this serves as a swiss army knife sort of tool which - *RocketChat* - *Slack* - *Telegram* +- *Twillio* ## Install From ce00fbb835dc72365d48c189a7eb02b5a8ba528a Mon Sep 17 00:00:00 2001 From: kha7iq Date: Mon, 3 May 2021 02:06:49 +0800 Subject: [PATCH 20/23] docs: fix install instructions --- docs/install.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/install.md b/docs/install.md index 61dc2b4..94818c5 100644 --- a/docs/install.md +++ b/docs/install.md @@ -9,7 +9,7 @@ brew install kha7iq/tap/pingme wget -q https://github.com/kha7iq/pingme/releases/download/v0.1.6/pingme_Linux_x86_64.tar.gz tar -xf pingme_Linux_x86_64.tar.gz chmod +x pingme -sudo mv +sudo mv pingme /usr/local/bin/pingme ``` ## Go Get From 42d64570576b7078ef91635a21c18a780be8f845 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Mon, 3 May 2021 02:07:13 +0800 Subject: [PATCH 21/23] docs: add twillio to supported services --- docs/home.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/home.md b/docs/home.md index adc5244..e77bdd9 100644 --- a/docs/home.md +++ b/docs/home.md @@ -52,6 +52,7 @@ as message, and most of all this serves as a swiss army knife sort of tool which - *RocketChat* - *Slack* - *Telegram* +- *Twillio* From f6e4829524c27b321fd279d9960f5a5b0e30dcc4 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Mon, 3 May 2021 02:07:41 +0800 Subject: [PATCH 22/23] docs: add documentation for twillio --- docs/services.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/docs/services.md b/docs/services.md index 50eb2f3..e63046a 100644 --- a/docs/services.md +++ b/docs/services.md @@ -382,6 +382,52 @@ jobs: | PUSHBULLET_TITLE | "" | + +## Twillio SMS +SMS can be sent via twillio to multiple numbers, you can add multiple receivers separated by a comma. + +```bash + pingme twillio --token 'tokenabc' --account 'sid123' --sender '+140001442' --receiver '+140001442'' --msg 'some message' +``` + +- GitHub Action + +```yaml +on: [push] + +jobs: + pingme-job: + runs-on: ubuntu-latest + name: PingMe + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Ping me On + uses: kha7iq/pingme-action@v1 + env: + TWILLIO_TOKEN: ${{ secrets.TWILLIO_TOKEN }} + TWILLIO_ACCOUNT_SID: ${{ secrets.TWILLIO_ACCOUNT_SID }} + TWILLIO_SENDER: ${{ secrets.TWILLIO_SENDER }} + TWILLIO_RECEIVER: ${{ secrets.TWILLIO_RECEIVER }} + TWILLIO_TITLE: 'Reference: ${{ github.ref }}' + TWILLIO_MESSAGE: 'Event is triggered by ${{ github.event_name }}' + with: + # Chose the messaging platform. + # slack / telegram / rocketchat / teams / pushover / discord / email / mattermost / twillio + service: twillio +``` +- **Variables** + +| Variables | Default Value | +| -------------------------- | :----------------: | +| TWILLIO_TOKEN | "" | +| TWILLIO_ACCOUNT_SID | "" | +| TWILLIO_SENDER | "" | +| TWILLIO_RECEIVER | "" | +| TWILLIO_TITLE | "" | +| TWILLIO_MESSAGE | "" | + ## Email Email uses username & password to authenticate for sending emails. SMTP hostname i.e smtp.gmail.com and port i.e (587) should be provided as well for the server. From 20e4b8ee3a13f76f747b3c865b3b0adb17391070 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Mon, 3 May 2021 02:08:24 +0800 Subject: [PATCH 23/23] feat: add new service twillio --- go.mod | 1 + go.sum | 13 +++++ main.go | 3 ++ service/twillio/twillio.go | 102 +++++++++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 service/twillio/twillio.go diff --git a/go.mod b/go.mod index 92ba9aa..5bb0762 100644 --- a/go.mod +++ b/go.mod @@ -7,5 +7,6 @@ require ( github.com/gregdel/pushover v0.0.0-20210216095829-2131362cb888 github.com/nikoksr/notify v0.15.0 github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/sfreiberg/gotwilio v0.0.0-20201211181435-c426a3710ab5 // indirect github.com/urfave/cli/v2 v2.3.0 ) diff --git a/go.sum b/go.sum index 42a79a1..728a3da 100644 --- a/go.sum +++ b/go.sum @@ -41,6 +41,8 @@ github.com/deckarep/golang-set v1.7.1/go.mod h1:93vsz/8Wt4joVM7c2AVqh+YRMiUSc14y github.com/dghubble/go-twitter v0.0.0-20201011215211-4b180d0cc78d/go.mod h1:xfg4uS5LEzOj8PgZV7SQYRHbG7jPUnelEiaAVJxmhJE= github.com/dghubble/oauth1 v0.7.0/go.mod h1:8pFdfPkv/jr8mkChVbNVuJ0suiHe278BtWI4Tk1ujxk= github.com/dghubble/sling v1.3.0/go.mod h1:XXShWaBWKzNLhu2OxikSNFrlsvowtz4kyRuXUG7oQKY= +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/facebookgo/ensure v0.0.0-20160127193407-b4ab57deab51/go.mod h1:Yg+htXGokKKdzcwhuNDwVvN+uBxDGXJ7G/VN1d8fa64= @@ -71,11 +73,14 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/gopackage/ddp v0.0.0-20170117053602-652027933df4 h1:4EZlYQIiyecYJlUbVkFXCXHz1QPhVXcHnQKAzBTPfQo= github.com/gopackage/ddp v0.0.0-20170117053602-652027933df4/go.mod h1:lEO7XoHJ/xNRBCxrn4h/CEB67h0kW1B0t4ooP2yrjUA= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY= +github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= @@ -88,6 +93,8 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfC github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA= github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= @@ -124,7 +131,10 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sendgrid/rest v2.6.3+incompatible/go.mod h1:kXX7q3jZtJXK5c5qK83bSGMdV6tsOE70KbHoqJls4lE= github.com/sendgrid/sendgrid-go v3.8.0+incompatible/go.mod h1:QRQt+LX/NmgVEvmdRw0VT/QgUn499+iza2FnDca9fg8= +github.com/sfreiberg/gotwilio v0.0.0-20201211181435-c426a3710ab5 h1:76NN4jha0iT2Qwfth8Xf8q2LlQEG7jiZ86dFDKHN9l8= +github.com/sfreiberg/gotwilio v0.0.0-20201211181435-c426a3710ab5/go.mod h1:dhtsjtHOWmTLjCOyNloce1diOIs9H1mvVmcOG7qmZUc= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/skip2/go-qrcode v0.0.0-20190110000554-dc11ecdae0a9/go.mod h1:PLPIyL7ikehBD1OAjmKKiOEhbvWyHGaNDjquXMcYABo= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= @@ -133,6 +143,7 @@ github.com/slack-go/slack v0.8.1/go.mod h1:FGqNzJBmxIsZURAxh2a8D21AnOVvvXZvGligs github.com/sony/sonyflake v1.0.0 h1:MpU6Ro7tfXwgn2l5eluf9xQvQJDROTBImNCfRXn/YeM= github.com/sony/sonyflake v1.0.0/go.mod h1:Jv3cfhf/UFtolOTTRd3q4Nl6ENqM+KfyZ5PseKfZGF4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= @@ -176,6 +187,8 @@ golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/main.go b/main.go index a64d89c..3a79a81 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,8 @@ import ( "log" "os" + "github.com/kha7iq/pingme/service/twillio" + "github.com/kha7iq/pingme/service/discord" "github.com/kha7iq/pingme/service/email" "github.com/kha7iq/pingme/service/mattermost" @@ -41,6 +43,7 @@ RocketChat, Discord, Pushover, Mattermost, Pushbullet, Microsoft Teams and email email.Send(), mattermost.Send(), pushbullet.Send(), + twillio.Send(), } err := app.Run(os.Args) diff --git a/service/twillio/twillio.go b/service/twillio/twillio.go new file mode 100644 index 0000000..066024c --- /dev/null +++ b/service/twillio/twillio.go @@ -0,0 +1,102 @@ +package twillio + +import ( + "log" + "strings" + + "github.com/kha7iq/pingme/service/helpers" + "github.com/sfreiberg/gotwilio" + "github.com/urfave/cli/v2" +) + +// Twillio struct holds data parsed via flags for the service +type Twillio struct { + Title string + Token string + AccountSid string + Sender string + Receiver string + Message string +} + +// Send parse values from *cli.context and return *cli.Command +// and send messages to target numbers. +// If multiple receivers are provided then the string is split with "," separator and +// message is sent to each number. +func Send() *cli.Command { + var twillioOpts Twillio + return &cli.Command{ + Name: "twillio", + Usage: "Send sms via twillio", + UsageText: "pingme twillio --token 'tokenabc' --account 'sid123' " + + "--sender '+140001442' --receiver '+140001442'' --msg 'some message'", + Description: `Twillio provides ability to send sms to multiple numbers. +You can specify multiple receivers by separating the value with a comma.`, + Flags: []cli.Flag{ + &cli.StringFlag{ + Destination: &twillioOpts.Token, + Name: "token", + Aliases: []string{"t"}, + Required: true, + Usage: "Auth token for twillio account.", + EnvVars: []string{"TWILLIO_TOKEN"}, + }, + &cli.StringFlag{ + Destination: &twillioOpts.AccountSid, + Name: "account", + Required: true, + Aliases: []string{"a"}, + Usage: "Twillio account sid", + EnvVars: []string{"TWILLIO_ACCOUNT_SID"}, + }, + &cli.StringFlag{ + Destination: &twillioOpts.Message, + Name: "msg", + Aliases: []string{"m"}, + Usage: "Message content.", + EnvVars: []string{"TWILLIO_MESSAGE"}, + }, + &cli.StringFlag{ + Destination: &twillioOpts.Title, + Name: "title", + Usage: "Title of the message.", + EnvVars: []string{"TWILLIO_TITLE"}, + }, + &cli.StringFlag{ + Destination: &twillioOpts.Sender, + Name: "sender", + Aliases: []string{"s"}, + Usage: "Sender's phone number", + EnvVars: []string{"TWILLIO_SENDER"}, + }, + &cli.StringFlag{ + Destination: &twillioOpts.Receiver, + Name: "receiver", + Aliases: []string{"r"}, + Usage: "Receiver's phone number", + EnvVars: []string{"TWILLIO_RECEIVER"}, + }, + }, + Action: func(ctx *cli.Context) error { + client := gotwilio.NewTwilioClient(twillioOpts.AccountSid, twillioOpts.Token) + fullMessage := twillioOpts.Title + "\n" + twillioOpts.Message + + numbers := strings.Split(twillioOpts.Receiver, ",") + for _, v := range numbers { + if len(v) == 0 { + return helpers.ErrChannel + } + + _, exception, err := client.SendSMS(twillioOpts.Sender, twillioOpts.Receiver, fullMessage, "", "") + if err != nil { + return err + } + if exception != nil { + return exception + } + } + log.Println("Successfully sent!") + return nil + }, + } +}