-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
171 lines (139 loc) · 4.36 KB
/
main.go
File metadata and controls
171 lines (139 loc) · 4.36 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"encoding/json"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"github.com/bwmarrin/discordgo"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/helixspiral/apod"
)
func init() {
logLevel := &slog.LevelVar{}
if logLevelEnv := os.Getenv("LOG_LEVEL"); logLevelEnv != "" {
switch logLevelEnv {
case "ERROR":
logLevel.Set(slog.LevelError)
case "WARN":
logLevel.Set(slog.LevelWarn)
case "INFO":
logLevel.Set(slog.LevelInfo)
case "DEBUG":
logLevel.Set(slog.LevelDebug)
default:
logLevel.Set(slog.LevelInfo)
}
}
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: logLevel,
}))
// Replace the default logger
slog.SetDefault(logger)
}
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")
slog.Info("Starting the MQTT to Discord service", "discord_channel_id", discordChannelId, "mqtt_broker", mqttBroker, "mqtt_client_id", mqttClientId, "mqtt_topic", mqttTopic)
// Setup the discord bot
dg, err := discordgo.New("Bot " + discordBotToken)
if err != nil {
slog.Error("Error creating Discord session", "error", err)
os.Exit(1)
}
dg.Identify.Intents = discordgo.IntentsNone
// Connect to Discord
err = dg.Open()
if err != nil {
slog.Error("Error opening connection to Discord", "error", err)
os.Exit(1)
}
// 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) {
slog.Warn("Connection lost", "error", e)
}
options.OnConnect = func(c mqtt.Client) {
slog.Info("Connected to MQTT broker")
t := c.Subscribe(mqttTopic, 2, nil)
go func() {
_ = t.Wait()
if t.Error() != nil {
slog.Error("Error subscribing", "error", t.Error())
} else {
slog.Info("Subscribed to MQTT topic", "mqtt_topic", mqttTopic)
}
}()
}
options.OnReconnecting = func(_ mqtt.Client, co *mqtt.ClientOptions) {
slog.Info("Reconnecting to MQTT broker", "mqtt_broker", co.Servers)
}
options.DefaultPublishHandler = func(_ mqtt.Client, m mqtt.Message) {
slog.Debug("Received message", "topic", m.Topic(), "payload", string(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 {
slog.Error("Error sending message to Discord", "error", err)
return
}
// "Crosspost" the message so it goes to all the followers.
_, err = dg.ChannelMessageCrosspost(msg.ChannelID, msg.ID)
if err != nil {
slog.Error("Error crossposting message to Discord", "error", err)
return
}
}
// 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 {
slog.Error("Error connecting to MQTT broker", "error", token.Error())
os.Exit(1)
}
slog.Info("Connected to MQTT broker", "mqtt_broker", mqttBroker)
// 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
slog.Info("Shutting down")
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
}