Skip to content

Commit f143bd4

Browse files
authored
Merge pull request #32 from kha7iq/add-mastodon-support
feat(service): support for mastodon
2 parents 3856679 + 918b61b commit f143bd4

File tree

4 files changed

+180
-8
lines changed

4 files changed

+180
-8
lines changed

README.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ platforms.
5454

5555
- *Discord*
5656
- *Email*
57-
- *Microsoft Teams*
57+
- *Mastodon*
5858
- *Mattermost*
59-
- *Pushover*
59+
- *Microsoft Teams*
6060
- *Pushbullet*
61+
- *Pushover*
6162
- *RocketChat*
6263
- *Slack*
6364
- *Telegram*
@@ -146,11 +147,10 @@ USAGE:
146147
main [global options] command [command options] [arguments...]
147148

148149
DESCRIPTION:
149-
PingMe is a CLI tool which provides the ability to send messages or alerts
150-
to multiple messaging platforms and also email, everything is configurable
151-
via environment variables and command line switches.Currently supported
152-
platforms include Slack, Telegram, RocketChat, Discord, Pushover, Mattermost,
153-
Microsoft Teams and email address.
150+
PingMe is a CLI tool which provides the ability to send messages or alerts to multiple
151+
messaging platforms and also email, everything is configurable via environment
152+
variables and command line switches.Currently supported platforms include Slack, Telegram,
153+
RocketChat, Discord, Pushover, Mattermost, Pushbullet, Microsoft Teams, Twillio, Mastodon and email address.
154154

155155
COMMANDS:
156156
telegram Send message to telegram
@@ -162,6 +162,8 @@ COMMANDS:
162162
email Send an email
163163
mattermost Send message to mattermost
164164
pushbullet Send message to pushbullet
165+
twillio Send sms via twillio
166+
mastodon Set status message for mastodon
165167
help, h Shows a list of commands or help for one command
166168

167169
GLOBAL OPTIONS:

docs/services.md

+43
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,49 @@ jobs:
468468
| TWILLIO_TITLE | "" |
469469
| TWILLIO_MESSAGE | "" |
470470
471+
## Mastodon
472+
473+
Mastodon uses application token to authorize and set status.
474+
475+
```bash
476+
mastodon --url "mastodon.social" --msg "some message" --title "PingMe CLI" --token "123"
477+
```
478+
479+
- GitHub Action
480+
481+
```yaml
482+
on: [push]
483+
484+
jobs:
485+
pingme-job:
486+
runs-on: ubuntu-latest
487+
name: PingMe
488+
steps:
489+
- name: Checkout
490+
uses: actions/checkout@v2
491+
492+
- name: Ping me On
493+
uses: kha7iq/pingme-action@v1
494+
env:
495+
MASTODON_TOKEN: ${{ secrets.MASTODON_TOKEN }}
496+
MASTODON_SERVER: 'mastodon.social'
497+
MASTODON_TITLE: 'Reference: ${{ github.ref }}'
498+
MASTODON_MESSAGE: 'Event is triggered by ${{ github.event_name }}'
499+
500+
with:
501+
service: mastodon
502+
```
503+
504+
- **Variables**
505+
506+
| Variables | Default Value |
507+
| -------------------------- | :----------------: |
508+
| MASTODON_TOKEN | "" |
509+
| MASTODON_SERVER | "" |
510+
| MASTODON_TITLE | "" |
511+
| MASTODON_MESSAGE | "" |
512+
513+
471514
## Email
472515
473516
Email uses username & password to authenticate for sending emails. SMTP

main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"log"
55
"os"
66

7+
"github.com/kha7iq/pingme/service/mastodon"
8+
79
"github.com/kha7iq/pingme/service/twillio"
810

911
"github.com/kha7iq/pingme/service/discord"
@@ -31,7 +33,7 @@ func main() {
3133
app.Description = `PingMe is a CLI tool which provides the ability to send messages or alerts to multiple
3234
messaging platforms and also email, everything is configurable via environment
3335
variables and command line switches.Currently supported platforms include Slack, Telegram,
34-
RocketChat, Discord, Pushover, Mattermost, Pushbullet, Microsoft Teams and email address.`
36+
RocketChat, Discord, Pushover, Mattermost, Pushbullet, Microsoft Teams, Twillio, Mastodon and email address.`
3537
// app.Commands contains the subcommands as functions which return []*cli.Command.
3638
app.Commands = []*cli.Command{
3739
telegram.Send(),
@@ -44,6 +46,7 @@ RocketChat, Discord, Pushover, Mattermost, Pushbullet, Microsoft Teams and email
4446
mattermost.Send(),
4547
pushbullet.Send(),
4648
twillio.Send(),
49+
mastodon.Send(),
4750
}
4851

4952
err := app.Run(os.Args)

service/mastodon/mastodon.go

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package mastodon
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
"net/http"
9+
"time"
10+
11+
"github.com/urfave/cli/v2"
12+
)
13+
14+
// Mastodon struct holds data parsed via flags for the service
15+
type Mastodon struct {
16+
Title string
17+
Token string
18+
ServerURL string
19+
Message string
20+
}
21+
22+
// Send parse values from *cli.context and return *cli.Command
23+
// and sets a status message for mastodon.
24+
func Send() *cli.Command {
25+
var mastodonOpts Mastodon
26+
return &cli.Command{
27+
Name: "mastodon",
28+
Usage: "Set status message for mastodon",
29+
UsageText: "pingme mastodon --token '123' --url 'mastodon.social' --title 'PingMe' " +
30+
"--msg 'some message'",
31+
Description: `Mastodon uses application token to authorize and sets a status message`,
32+
Flags: []cli.Flag{
33+
&cli.StringFlag{
34+
Destination: &mastodonOpts.Token,
35+
Name: "token",
36+
Aliases: []string{"t"},
37+
Required: true,
38+
Usage: "Application token for authorization.",
39+
EnvVars: []string{"MASTODON_TOKEN"},
40+
},
41+
&cli.StringFlag{
42+
Destination: &mastodonOpts.Message,
43+
Name: "msg",
44+
Aliases: []string{"m"},
45+
Usage: "Message content.",
46+
EnvVars: []string{"MASTODON_MESSAGE"},
47+
},
48+
&cli.StringFlag{
49+
Destination: &mastodonOpts.Title,
50+
Name: "title",
51+
Usage: "Title of the message.",
52+
EnvVars: []string{"MASTODON_TITLE"},
53+
},
54+
&cli.StringFlag{
55+
Destination: &mastodonOpts.ServerURL,
56+
Name: "url",
57+
Aliases: []string{"u"},
58+
Value: "mastodon.social",
59+
Required: true,
60+
Usage: "URL of mastodon server i.e mastodon.social",
61+
EnvVars: []string{"MASTODON_SERVER"},
62+
},
63+
},
64+
Action: func(ctx *cli.Context) error {
65+
endPointURL := "https://" + mastodonOpts.ServerURL + "/api/v1/statuses/"
66+
67+
// Create a Bearer string by appending string access token
68+
bearer := "Bearer " + mastodonOpts.Token
69+
70+
fullMessage := mastodonOpts.Title + "\n" + mastodonOpts.Message
71+
72+
if err := sendMastodon(endPointURL, bearer, fullMessage); err != nil {
73+
return fmt.Errorf("failed to send message\n[ERROR] - %v", err)
74+
}
75+
76+
return nil
77+
},
78+
}
79+
}
80+
81+
// sendMastodon function take the server url , authorization token
82+
// and message string to set the status.
83+
func sendMastodon(url string, token string, msg string) error {
84+
reqBody, err := json.Marshal(map[string]string{
85+
"status": msg,
86+
})
87+
if err != nil {
88+
return err
89+
}
90+
91+
// Create a new request using http
92+
req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqBody))
93+
if err != nil {
94+
return err
95+
}
96+
97+
// add authorization header to the request
98+
req.Header.Set("Authorization", token)
99+
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
100+
101+
// create a new http client and send request to server
102+
c := &http.Client{Timeout: 10 * time.Second}
103+
resp, err := c.Do(req)
104+
if err != nil {
105+
return err
106+
}
107+
defer resp.Body.Close()
108+
109+
// decode response received from server
110+
var data map[string]interface{}
111+
err = json.NewDecoder(resp.Body).Decode(&data)
112+
if err != nil {
113+
return err
114+
}
115+
116+
// check if server returned an error
117+
checkErr, ok := data["error"]
118+
if ok {
119+
return fmt.Errorf("%v", checkErr)
120+
}
121+
122+
log.Printf("Success!!\nVisibility: %v\nURL: %v\n", data["visibility"], data["url"])
123+
return nil
124+
}

0 commit comments

Comments
 (0)