-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (111 loc) · 3.22 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/bwmarrin/discordgo"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/helixspiral/apod"
)
func main() {
// Some initial setup with our environment
discordBotToken := os.Getenv("DISCORD_BOT_TOKEN")
discordChannelId := os.Getenv("DISCORD_CHANNEL_ID")
mqttBroker := os.Getenv("MQTT_BROKER")
mqttClientId := os.Getenv("MQTT_CLIENT_ID")
mqttTopic := os.Getenv("MQTT_TOPIC")
// Setup the discord bot
dg, err := discordgo.New("Bot " + discordBotToken)
if err != nil {
panic(err)
}
dg.Identify.Intents = discordgo.IntentsNone
// Connect to Discord
err = dg.Open()
if err != nil {
panic(err)
}
// Setup the MQTT client options
options := mqtt.NewClientOptions().AddBroker(mqttBroker).SetClientID(mqttClientId)
options.ConnectRetry = true
options.AutoReconnect = true
options.OnConnectionLost = func(c mqtt.Client, e error) {
log.Println("Connection lost")
}
options.OnConnect = func(c mqtt.Client) {
log.Println("Connected")
t := c.Subscribe(mqttTopic, 2, nil)
go func() {
_ = t.Wait()
if t.Error() != nil {
log.Printf("Error subscribing: %s\n", t.Error())
} else {
log.Println("Subscribed to:", mqttTopic)
}
}()
}
options.OnReconnecting = func(_ mqtt.Client, co *mqtt.ClientOptions) {
log.Println("Attempting to reconnect")
}
options.DefaultPublishHandler = func(_ mqtt.Client, m mqtt.Message) {
log.Printf("Received: %s->%s\n", m.Topic(), m.Payload())
// Unmarshal the received json into a struct
var apodMsg apod.ApodQueryOutput
_ = json.Unmarshal(m.Payload(), &apodMsg)
// Build the message we're going to send to Discord
messageToSend := buildApodMessage(&apodMsg)
// Send the message to the specified channel
msg, err := dg.ChannelMessageSend(discordChannelId, messageToSend)
if err != nil {
panic(err)
}
// "Crosspost" the message so it goes to all the followers.
_, err = dg.ChannelMessageCrosspost(msg.ChannelID, msg.ID)
if err != nil {
panic(err)
}
}
// Setup the MQTT client with the options we set
mqttClient := mqtt.NewClient(options)
// Connect to the MQTT server
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
log.Println("Connected")
// Block indefinitely until something above errors, or we close out.
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
signal.Notify(sig, syscall.SIGTERM)
<-sig
log.Println("Signal caught -> Exit")
mqttClient.Disconnect(1000)
}
// Build the message for Discord
func buildApodMessage(a *apod.ApodQueryOutput) string {
var messageToSend string
// Setup the message to send to Discord
messageToSend += "```"
messageToSend += fmt.Sprintf("Title: %s\n\n", a.Title)
messageToSend += fmt.Sprintf("Date: %s\n\n", a.Date)
messageToSend += fmt.Sprintf("Explanation: %s\n\n", a.Explanation)
if a.Copyright != "" {
messageToSend += fmt.Sprintf("Copyright: %s\n", a.Copyright)
}
messageToSend += "```\n"
switch a.MediaType {
case "image":
if a.HdUrl != "" {
messageToSend += a.HdUrl
} else {
messageToSend += a.Url
}
case "video":
messageToSend += a.ThumbnailUrl
messageToSend += "\n\n"
messageToSend += a.Url
}
return messageToSend
}