|
| 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