| 1 |
//go:build !nofirebase |
| 2 |
|
| 3 |
package main |
| 4 |
|
| 5 |
import ( |
| 6 |
"context" |
| 7 |
firebase "firebase.google.com/go/v4" |
| 8 |
"firebase.google.com/go/v4/messaging" |
| 9 |
"flag" |
| 10 |
"fmt" |
| 11 |
"google.golang.org/api/option" |
| 12 |
"os" |
| 13 |
"strings" |
| 14 |
) |
| 15 |
|
| 16 |
func main() { |
| 17 |
conffile := flag.String("config", "/etc/fbsend/fbsend.json", "config file") |
| 18 |
flag.Parse() |
| 19 |
if flag.NArg() < 2 { |
| 20 |
fail("Syntax: fbsend [-config FILE] topic key=value ...") |
| 21 |
} |
| 22 |
topic := flag.Arg(0) |
| 23 |
data := make(map[string]string) |
| 24 |
for i := 1; i < flag.NArg(); i++ { |
| 25 |
kv := strings.SplitN(flag.Arg(i), "=", 2) |
| 26 |
if len(kv) != 2 { |
| 27 |
fail(fmt.Sprintf("Invalid argument: %s (%v)", flag.Arg(i), kv)) |
| 28 |
} |
| 29 |
data[kv[0]] = kv[1] |
| 30 |
} |
| 31 |
fb, err := firebase.NewApp(context.Background(), nil, option.WithAuthCredentialsFile(option.ServiceAccount, *conffile)) |
| 32 |
if err != nil { |
| 33 |
fail(err.Error()) |
| 34 |
} |
| 35 |
msg, err := fb.Messaging(context.Background()) |
| 36 |
if err != nil { |
| 37 |
fail(err.Error()) |
| 38 |
} |
| 39 |
_, err = msg.Send(context.Background(), &messaging.Message{ |
| 40 |
Topic: topic, |
| 41 |
Data: data, |
| 42 |
}) |
| 43 |
if err != nil { |
| 44 |
fail(err.Error()) |
| 45 |
} |
| 46 |
fmt.Println("Sent successfully") |
| 47 |
} |
| 48 |
|
| 49 |
func fail(s string) { |
| 50 |
fmt.Println(s) |
| 51 |
os.Exit(1) |
| 52 |
} |