48 lines
871 B
Go
48 lines
871 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
opentracing "github.com/opentracing/opentracing-go"
|
|
)
|
|
|
|
func printerFact(ctx context.Context, s *discordgo.Session, m *discordgo.Message, parv []string) error {
|
|
sp, ctx := opentracing.StartSpanFromContext(ctx, "printer.fact")
|
|
defer sp.Finish()
|
|
|
|
fact, err := getPrinterFact()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
s.ChannelMessageSend(m.ChannelID, fact)
|
|
return nil
|
|
}
|
|
|
|
func getPrinterFact() (string, error) {
|
|
resp, err := http.Get("https://xena.stdlib.com/printerfacts")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
factStruct := &struct {
|
|
Facts []string `json:"facts"`
|
|
}{}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
json.Unmarshal(body, factStruct)
|
|
|
|
text := fmt.Sprintf("%s", factStruct.Facts[0])
|
|
|
|
return text, nil
|
|
}
|