flesh out some struct action
This commit is contained in:
parent
608806becb
commit
ca8a9d0b19
@ -1,6 +1,7 @@
|
|||||||
package plugins
|
package plugins
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
@ -15,28 +16,58 @@ import (
|
|||||||
type Beer struct {
|
type Beer struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Beer) match(msg string) string {
|
// BeerRecord is the parent beer type
|
||||||
|
type BeerRecord struct {
|
||||||
|
DatasetID string `json:"datasetid"`
|
||||||
|
RecordID string `json:"recordid"`
|
||||||
|
Fields BeerFields `json:"fields"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// BeerFields ar the fields we care about for any given beer
|
||||||
|
type BeerFields struct {
|
||||||
|
Website string `json:"website"`
|
||||||
|
City string `json:"city"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Country string `json:"country"`
|
||||||
|
Descr string `json:"descript"`
|
||||||
|
Style string `json:"style_name"`
|
||||||
|
IBU int `json:"ibu"`
|
||||||
|
BreweryName string `json:"name_breweries"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// BeerRecords are a collection of responses
|
||||||
|
type BeerRecords []BeerRecord
|
||||||
|
|
||||||
|
func (h *Beer) fix(msg string) string {
|
||||||
re := regexp.MustCompile(`(?i)^beer: `)
|
re := regexp.MustCompile(`(?i)^beer: `)
|
||||||
return re.ReplaceAllString(msg, "$1")
|
return re.ReplaceAllString(msg, "$1")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Beer) get(beer string) (string, error) {
|
func (h *Beer) match(msg string) bool {
|
||||||
|
re := regexp.MustCompile(`(?i)^beer: `)
|
||||||
|
return re.MatchString(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Beer) get(beer string) (*BeerRecords, error) {
|
||||||
u := "https://data.opendatasoft.com/api/records/1.0/search?dataset=open-beer-database%40public-us&q="
|
u := "https://data.opendatasoft.com/api/records/1.0/search?dataset=open-beer-database%40public-us&q="
|
||||||
u = fmt.Sprintf("%s%s", u, url.PathEscape(beer))
|
u = fmt.Sprintf("%s%s", u, url.PathEscape(beer))
|
||||||
log.Println(u)
|
log.Println(u)
|
||||||
resp, err := http.Get(u)
|
resp, err := http.Get(u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(body), nil
|
beers := &BeerRecords{}
|
||||||
|
_ = json.Unmarshal([]byte(body), beers)
|
||||||
|
|
||||||
|
return beers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Respond to hi events
|
// Respond to hi events
|
||||||
@ -45,14 +76,16 @@ func (h *Beer) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) {
|
|||||||
switch mtype {
|
switch mtype {
|
||||||
case "m.text":
|
case "m.text":
|
||||||
if post, ok := ev.Body(); ok {
|
if post, ok := ev.Body(); ok {
|
||||||
beer := h.match(post)
|
if h.match(post) {
|
||||||
if beer != "" {
|
beer := h.fix(post)
|
||||||
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
|
if beer != "" {
|
||||||
json, err := h.get(beer)
|
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
|
||||||
if err != nil {
|
j, err := h.get(beer)
|
||||||
SendMessage(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look for beer. (%s)", ev.Sender, err))
|
if err != nil {
|
||||||
|
SendMessage(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look for beer. (%s)", ev.Sender, err))
|
||||||
|
}
|
||||||
|
SendMessage(c, ev.RoomID, fmt.Sprintf("%v", j))
|
||||||
}
|
}
|
||||||
SendMessage(c, ev.RoomID, fmt.Sprintf("%s!", json))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user