mirror-nourybot/pkg/api/ivr/firstline.go

50 lines
1.1 KiB
Go
Raw Normal View History

2021-10-17 18:43:38 +02:00
package ivr
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
log "github.com/sirupsen/logrus"
)
type firstLineApiResponse struct {
User string `json:"user"`
Message string `json:"message"`
Time string `json:"time"`
Error string `json:"error"`
}
var (
firstLineBaseUrl = "https://api.ivr.fi/logs/firstmessage"
)
2021-10-20 23:21:13 +02:00
// FirstLine returns the first line a given user has sent in a
// given channel.
func FirstLine(channel string, username string) (string, error) {
resp, err := http.Get(fmt.Sprintf("%s/%s/%s", firstLineBaseUrl, channel, username))
2021-10-17 18:43:38 +02:00
if err != nil {
log.Error(err)
return "Something went wrong FeelsBadMan", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
2021-10-17 23:02:55 +02:00
log.Error(err)
2021-10-17 18:43:38 +02:00
}
var responseObject firstLineApiResponse
json.Unmarshal(body, &responseObject)
// User or channel was not found
if responseObject.Error != "" {
return fmt.Sprintf(responseObject.Error + " FeelsBadMan"), nil
} else {
return fmt.Sprintf(username + ": " + responseObject.Message + " (" + responseObject.Time + " ago)."), nil
}
}