Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
373e9ff3c3 | ||
|
|
d3fff1519b | ||
|
|
2b6acc51fb | ||
|
|
fade4467d6 | ||
|
|
c8800c010e | ||
|
|
266ccb5f98 | ||
|
|
1cc082083c |
@@ -12,7 +12,7 @@
|
||||
"RefreshURI": "https://api.imgur.com/oauth2/token",
|
||||
"RefreshFormat": "refresh_token=%s\u0026client_id=%s\u0026client_secret=%s\u0026grant_type=refresh_token",
|
||||
"RefreshMethod": "POST",
|
||||
"UploadURI": "https://api.imgur.com/3/image",
|
||||
"UploadURI": "https://api.imgur.com/3/image?name=something.jpeg",
|
||||
"UploadMethod": "POST"
|
||||
},
|
||||
"Maps": {
|
||||
@@ -74,7 +74,7 @@
|
||||
"Room": "!WbawbZxHqnxxfhvcSj:synapse.home.blapointe.com"
|
||||
}
|
||||
},
|
||||
"Once": false,
|
||||
"Once": true,
|
||||
"Brokers": {
|
||||
"NTG": {
|
||||
"Mock": true,
|
||||
|
||||
39
main.go
39
main.go
@@ -9,6 +9,7 @@ import (
|
||||
"local/truckstop/config"
|
||||
"local/truckstop/message"
|
||||
"log"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -87,38 +88,52 @@ func matrixrecv() error {
|
||||
}()
|
||||
func() {
|
||||
log.Printf("looking for states")
|
||||
db := config.Get().DB()
|
||||
states := map[string]map[config.State]struct{}{}
|
||||
for _, msg := range messages {
|
||||
key := fmt.Sprintf("states_%d", msg.Timestamp.Unix())
|
||||
if !strings.HasPrefix(msg.Content, "!state") {
|
||||
continue
|
||||
}
|
||||
if _, ok := states[msg.Sender]; ok {
|
||||
continue
|
||||
}
|
||||
states[msg.Sender] = map[config.State]struct{}{}
|
||||
for _, state := range parseOutStates([]byte(msg.Content)) {
|
||||
states[msg.Sender][state] = struct{}{}
|
||||
if _, err := db.Get(key); err == storage.ErrNotFound {
|
||||
states[msg.Sender] = map[config.State]struct{}{}
|
||||
for _, state := range parseOutStates([]byte(msg.Content)) {
|
||||
states[msg.Sender][state] = struct{}{}
|
||||
}
|
||||
}
|
||||
if err := db.Set(key, []byte{'k'}); err != nil {
|
||||
log.Printf("failed to mark state gathered @%s: %v", key, err)
|
||||
}
|
||||
}
|
||||
setNewStates(states)
|
||||
}()
|
||||
func() {
|
||||
log.Printf("looking for pauses")
|
||||
db := config.Get().DB()
|
||||
pauses := map[string]time.Time{}
|
||||
for _, msg := range messages {
|
||||
key := fmt.Sprintf("pauses_%d", msg.Timestamp.Unix())
|
||||
if !strings.HasPrefix(msg.Content, "!available ") {
|
||||
continue
|
||||
}
|
||||
if _, ok := pauses[msg.Sender]; ok {
|
||||
continue
|
||||
}
|
||||
t, err := time.ParseInLocation(
|
||||
"2006-01-02",
|
||||
strings.TrimSpace(strings.TrimPrefix(msg.Content, "!available ")),
|
||||
time.Local,
|
||||
)
|
||||
if err == nil {
|
||||
pauses[msg.Sender] = t
|
||||
if _, err := db.Get(key); err == storage.ErrNotFound {
|
||||
t, err := time.ParseInLocation(
|
||||
"2006-01-02",
|
||||
strings.TrimSpace(strings.TrimPrefix(msg.Content, "!available ")),
|
||||
time.Local,
|
||||
)
|
||||
if err == nil {
|
||||
pauses[msg.Sender] = t
|
||||
}
|
||||
}
|
||||
if err := db.Set(key, []byte{'k'}); err != nil {
|
||||
log.Printf("failed to mark state gathered @%s: %v", key, err)
|
||||
}
|
||||
}
|
||||
setNewPauses(pauses)
|
||||
@@ -311,7 +326,7 @@ func sendJob(job broker.Job) error {
|
||||
}
|
||||
maps := config.Get().Maps
|
||||
if maps.Pickup {
|
||||
pickup := fmt.Sprintf("%s,%s", job.Pickup.City, job.Pickup.State)
|
||||
pickup := fmt.Sprintf("%s,%s", url.QueryEscape(job.Pickup.City), job.Pickup.State)
|
||||
uri := fmt.Sprintf(maps.URIFormat, pickup, pickup)
|
||||
log.Printf("sending pickup image: %s", uri)
|
||||
if err := sender.SendImage(uri); err != nil {
|
||||
@@ -319,7 +334,7 @@ func sendJob(job broker.Job) error {
|
||||
}
|
||||
}
|
||||
if maps.Dropoff {
|
||||
dropoff := fmt.Sprintf("%s,%s", job.Dropoff.City, job.Dropoff.State)
|
||||
dropoff := fmt.Sprintf("%s,%s", url.QueryEscape(job.Dropoff.City), job.Dropoff.State)
|
||||
uri := fmt.Sprintf(maps.URIFormat, dropoff, dropoff)
|
||||
log.Printf("sending dropoff image: %s", uri)
|
||||
if err := sender.SendImage(uri); err != nil {
|
||||
|
||||
@@ -7,8 +7,10 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"local/truckstop/config"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -30,9 +32,17 @@ func refreshToken() error {
|
||||
}
|
||||
|
||||
func uploadImage(b []byte) (string, error) {
|
||||
images := config.Get().Images
|
||||
buff := bytes.NewBuffer(nil)
|
||||
writer := multipart.NewWriter(buff)
|
||||
part, err := writer.CreateFormFile("image", "name")
|
||||
name := "name"
|
||||
if u, err := url.Parse(images.UploadURI); err != nil {
|
||||
} else if s, ok := u.Query()["name"]; !ok {
|
||||
} else {
|
||||
name = s[0]
|
||||
log.Printf("found name in upload uri: %s", name)
|
||||
}
|
||||
part, err := writer.CreateFormFile("image", name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -43,7 +53,6 @@ func uploadImage(b []byte) (string, error) {
|
||||
}
|
||||
writer.Close()
|
||||
|
||||
images := config.Get().Images
|
||||
request, err := http.NewRequest(
|
||||
images.UploadMethod,
|
||||
images.UploadURI,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"local/truckstop/config"
|
||||
@@ -119,19 +120,25 @@ func (m Matrix) SendImage(uri string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if response.StatusCode != http.StatusOK {
|
||||
b, _ := ioutil.ReadAll(response.Body)
|
||||
response.Body.Close()
|
||||
return fmt.Errorf("failed to get %s: (%d) %s", uri, response.StatusCode, b)
|
||||
}
|
||||
b, err := ioutil.ReadAll(response.Body)
|
||||
response.Body.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
publicURI, err := UploadImage(b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c, err := m.getclient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mediaUpload, err := c.UploadToContentRepo(bytes.NewReader(b), "image/jpeg", int64(len(b)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
publicURI := mediaUpload.ContentURI
|
||||
resp, err := c.SendImage(m.room, "img", publicURI)
|
||||
log.Printf("sent image %s => %s: %+v", uri, publicURI, resp)
|
||||
return err
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
todo:
|
||||
- no hard code jpeg or have it in multiple places
|
||||
- todo: switch house to selfhosted for rate limit
|
||||
tags: now
|
||||
- mark consumed;; save scroll id for matrix
|
||||
- TEST its falling apart
|
||||
- test each !command callbacks to matrix
|
||||
- change matrix so I test my custom logic even if I dont fetch remote
|
||||
- warn/err/etc. on clobbering ids.matrix since clients can mess with one another
|
||||
|
||||
Reference in New Issue
Block a user