wire in basic verb lookup
This commit is contained in:
parent
798b217c14
commit
a5f0a4660d
|
@ -4,11 +4,9 @@ import (
|
|||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"git.netflux.io/rob/esbot/internal/matrix"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type handler struct {
|
||||
|
@ -37,9 +35,8 @@ func (h *handler) handleTransactions(w http.ResponseWriter, r *http.Request) {
|
|||
w.Write([]byte("{}"))
|
||||
}
|
||||
|
||||
func ListenAndServe(listenAddr, baseURL, accessToken string, insecureTLS bool) error {
|
||||
func ListenAndServe(listenAddr, baseURL, accessToken string, insecureTLS bool, logger zerolog.Logger) error {
|
||||
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: insecureTLS}
|
||||
logger := log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
|
||||
|
||||
h := handler{
|
||||
logger: logger,
|
||||
|
@ -49,5 +46,6 @@ func ListenAndServe(listenAddr, baseURL, accessToken string, insecureTLS bool) e
|
|||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/transactions/", h.handleTransactions)
|
||||
|
||||
logger.Info().Msgf("Listening on %s...", listenAddr)
|
||||
return http.ListenAndServe(listenAddr, loggerMiddleware(logger, mux))
|
||||
}
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
package lang
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type (
|
||||
Mood int
|
||||
Tense int
|
||||
)
|
||||
|
||||
const (
|
||||
Indicative Mood = iota
|
||||
)
|
||||
|
||||
const (
|
||||
Present Tense = iota
|
||||
Future
|
||||
Imperfect
|
||||
Preterite
|
||||
Conditional
|
||||
PresentPerfect
|
||||
FuturePerfect
|
||||
PastPerfect
|
||||
)
|
||||
|
||||
type Verb struct {
|
||||
Inf string
|
||||
InfEn string
|
||||
Forms []string
|
||||
}
|
||||
|
||||
func (v *Verb) String() string {
|
||||
return fmt.Sprintf("%s: %s\n%s", v.Inf, v.InfEn, strings.Join(v.Forms, ", "))
|
||||
}
|
||||
|
||||
var (
|
||||
verbs map[string]*Verb = make(map[string]*Verb)
|
||||
)
|
||||
|
||||
func GetVerb(q string) *Verb {
|
||||
return verbs[q]
|
||||
}
|
||||
|
||||
func Load() (int, error) {
|
||||
var err error
|
||||
f, err := os.Open("./internal/lang/resources/jehle_verb_database.csv")
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("could not open verb database: %v", err)
|
||||
}
|
||||
|
||||
csv := csv.NewReader(f)
|
||||
csv.FieldsPerRecord = 17
|
||||
csv.LazyQuotes = true
|
||||
var record []string
|
||||
var count int
|
||||
for {
|
||||
record, err = csv.Read()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
return 0, fmt.Errorf("could not read verb database: %v", err)
|
||||
}
|
||||
|
||||
// only indicative present tense for now
|
||||
if record[2] != "Indicativo" || record[4] != "Presente" {
|
||||
continue
|
||||
}
|
||||
|
||||
inf := record[0]
|
||||
verbs[inf] = &Verb{Inf: inf, InfEn: record[1], Forms: record[7:13]}
|
||||
count++
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
|
@ -7,7 +7,9 @@ import (
|
|||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"git.netflux.io/rob/esbot/internal/lang"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
|
@ -92,13 +94,22 @@ func (p *processor) acceptInvite(e *RawEvent) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *processor) handleVerb(e *RawEvent, cmd, arg string) error {
|
||||
replyBody := sendBody{
|
||||
Body: fmt.Sprintf("rcvd command %q, arg %q", cmd, arg),
|
||||
MsgType: "text",
|
||||
func (p *processor) handleVerb(e *RawEvent, _, arg string) error {
|
||||
verb := lang.GetVerb(strings.ToLower(arg))
|
||||
var sb sendBody
|
||||
if verb != nil {
|
||||
sb = sendBody{
|
||||
Body: verb.String(),
|
||||
MsgType: "m.notice",
|
||||
}
|
||||
} else {
|
||||
sb = sendBody{
|
||||
Body: "I don't know this verb. Try another.",
|
||||
MsgType: "m.notice",
|
||||
}
|
||||
}
|
||||
|
||||
encodedBody, err := json.Marshal(replyBody)
|
||||
encodedBody, err := json.Marshal(sb)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error encoding message: %v", err)
|
||||
}
|
||||
|
|
22
main.go
22
main.go
|
@ -1,29 +1,41 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"git.netflux.io/rob/esbot/internal/http"
|
||||
"git.netflux.io/rob/esbot/internal/lang"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
logger := log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
|
||||
|
||||
accessToken := os.Getenv("ACCESS_TOKEN")
|
||||
if accessToken == "" {
|
||||
log.Fatal("missing ACCESS_TOKEN")
|
||||
logger.Fatal().Msg("missing ACCESS_TOKEN")
|
||||
}
|
||||
|
||||
listenAddr := os.Getenv("LISTEN_ADDR")
|
||||
if listenAddr == "" {
|
||||
log.Fatal("missing LISTEN_ADDR")
|
||||
logger.Fatal().Msg("missing LISTEN_ADDR")
|
||||
}
|
||||
|
||||
baseURL := os.Getenv("BASE_URL")
|
||||
if baseURL == "" {
|
||||
log.Fatal("missing BASE_URL")
|
||||
logger.Fatal().Msg("missing BASE_URL")
|
||||
}
|
||||
|
||||
insecureTLS := os.Getenv("INSECURE_TLS") == "yes"
|
||||
|
||||
log.Fatal(http.ListenAndServe(listenAddr, baseURL, accessToken, insecureTLS))
|
||||
if n, err := lang.Load(); err == nil {
|
||||
logger.Info().Msgf("Loaded %d verbs", n)
|
||||
} else {
|
||||
logger.Fatal().Msgf("could not initialize languages: %v", err)
|
||||
}
|
||||
|
||||
if err := http.ListenAndServe(listenAddr, baseURL, accessToken, insecureTLS, logger); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue