Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
266ccb5f98 | ||
|
|
1cc082083c | ||
|
|
5170e96f7e | ||
|
|
826278fa78 | ||
|
|
111fe0ad6d | ||
|
|
72a474b50a | ||
|
|
de80f11392 | ||
|
|
d26d043f2e | ||
|
|
b6dbe25a48 | ||
|
|
09e87c66d8 | ||
|
|
75409a11b9 | ||
|
|
3213c21c7e |
@@ -53,7 +53,7 @@ func (j Job) FormatMultilineText() string {
|
||||
)
|
||||
}
|
||||
out := ""
|
||||
clients := config.Clients()
|
||||
clients := config.Clients(j.Pickup.Date)
|
||||
for k := range clients {
|
||||
log.Printf("job multiline: %+v contains %s then use %v", clients[k].States, j.Pickup.State, k)
|
||||
if strings.Contains(fmt.Sprint(clients[k].States), j.Pickup.State) {
|
||||
|
||||
36
config.json
36
config.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"Interval": {
|
||||
"Input": "10s..30s",
|
||||
"Input": "15s..20s",
|
||||
"OK": "6h0m0s..6h0m0s",
|
||||
"Error": "6h0m0s..6h0m0s"
|
||||
},
|
||||
@@ -10,7 +10,7 @@
|
||||
"RefreshToken": "171417741bf762b99b0b9f9137491b7a69874a77",
|
||||
"AccessToken": "e63db98f92d2db7ac7f56914a2030c889b378e9b",
|
||||
"RefreshURI": "https://api.imgur.com/oauth2/token",
|
||||
"RefreshFormat": "refresh_token=%s&client_id=%s&client_secret=%s&grant_type=refresh_token",
|
||||
"RefreshFormat": "refresh_token=%s\u0026client_id=%s\u0026client_secret=%s\u0026grant_type=refresh_token",
|
||||
"RefreshMethod": "POST",
|
||||
"UploadURI": "https://api.imgur.com/3/image",
|
||||
"UploadMethod": "POST"
|
||||
@@ -18,9 +18,18 @@
|
||||
"Maps": {
|
||||
"URIFormat": "https://maps.googleapis.com/maps/api/staticmap?center=%s\u0026markers=label=A|%s\u0026zoom=5\u0026size=250x250\u0026scale=1\u0026format=jpeg\u0026maptype=roadmap\u0026key=AIzaSyBkACm-LQkoSfsTO5_XAzBVZE9-JQzcNkg",
|
||||
"Pickup": true,
|
||||
"Dropoff": true
|
||||
"Dropoff": false
|
||||
},
|
||||
"Clients": {
|
||||
"bel": {
|
||||
"States": [
|
||||
"NC"
|
||||
],
|
||||
"IDs": {
|
||||
"Matrix": "@bel:synapse.home.blapointe.com"
|
||||
},
|
||||
"Available": 1612328400
|
||||
},
|
||||
"broc": {
|
||||
"States": [
|
||||
"FL",
|
||||
@@ -29,7 +38,7 @@
|
||||
"IDs": {
|
||||
"Matrix": "@belandbroc:matrix.org"
|
||||
},
|
||||
"PauseUntil": 5642107200
|
||||
"Available": 5642452800
|
||||
},
|
||||
"caleb": {
|
||||
"States": [
|
||||
@@ -38,33 +47,34 @@
|
||||
"IDs": {
|
||||
"Matrix": "@belandbroc:matrix.org"
|
||||
},
|
||||
"PauseUntil": -62135596800
|
||||
"Available": -62135596800
|
||||
},
|
||||
"pa": {
|
||||
"States": [
|
||||
"OH"
|
||||
"NC"
|
||||
],
|
||||
"IDs": {
|
||||
"Matrix": "@belandbroc:matrix.org"
|
||||
},
|
||||
"PauseUntil": -62135596800
|
||||
"Available": -62135596800
|
||||
}
|
||||
},
|
||||
"Storage": [
|
||||
"map"
|
||||
"files",
|
||||
"/tmp/play"
|
||||
],
|
||||
"Message": {
|
||||
"Matrix": {
|
||||
"ReceiveEnabled": true,
|
||||
"Mock": false,
|
||||
"Homeserver": "https://matrix-client.matrix.org",
|
||||
"Username": "@breellocaldev:matrix.org",
|
||||
"Token": "syt_YnJlZWxsb2NhbGRldg_HTewKMMePdEcLvceAKEz_2fHsHa",
|
||||
"Homeserver": "https://synapse.home.blapointe.com",
|
||||
"Username": "@bel:synapse.home.blapointe.com",
|
||||
"Token": "MDAyOGxvY2F0aW9uIHN5bmFwc2UuaG9tZS5ibGFwb2ludGUuY29tCjAwMTNpZGVudGlmaWVyIGtleQowMDEwY2lkIGdlbiA9IDEKMDAzMmNpZCB1c2VyX2lkID0gQGJlbDpzeW5hcHNlLmhvbWUuYmxhcG9pbnRlLmNvbQowMDE2Y2lkIHR5cGUgPSBhY2Nlc3MKMDAyMWNpZCBub25jZSA9IFpXTTtCaTNBODdtSTpsZkAKMDAyZnNpZ25hdHVyZSBX37kStY2wt_ftMfEYPfk1ADMJ6ZVfZsro9ic3weZ25Ao",
|
||||
"Device": "TGNIOGKATZ",
|
||||
"Room": "!ySKxwGHQPzPfVAldfJ:matrix.org"
|
||||
"Room": "!WbawbZxHqnxxfhvcSj:synapse.home.blapointe.com"
|
||||
}
|
||||
},
|
||||
"Once": true,
|
||||
"Once": false,
|
||||
"Brokers": {
|
||||
"NTG": {
|
||||
"Mock": true,
|
||||
|
||||
@@ -63,7 +63,7 @@ type Client struct {
|
||||
IDs struct {
|
||||
Matrix string
|
||||
}
|
||||
PauseUntil Time
|
||||
Available Time
|
||||
}
|
||||
|
||||
var live Config
|
||||
@@ -76,11 +76,11 @@ func configPath() string {
|
||||
return p
|
||||
}
|
||||
|
||||
func Clients() map[string]Client {
|
||||
func Clients(t time.Time) map[string]Client {
|
||||
clients := Get().Clients
|
||||
result := map[string]Client{}
|
||||
for k := range clients {
|
||||
if clients[k].PauseUntil.Get().IsZero() || time.Now().After(clients[k].PauseUntil.Get()) {
|
||||
if clients[k].Available.Get().IsZero() || t.After(clients[k].Available.Get()) {
|
||||
result[k] = clients[k]
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,7 @@ func Clients() map[string]Client {
|
||||
|
||||
func AllStates() []State {
|
||||
statem := map[State]struct{}{}
|
||||
for _, v := range Clients() {
|
||||
for _, v := range Clients(time.Now().Add(time.Hour * 24 * 365)) {
|
||||
for _, state := range v.States {
|
||||
statem[state] = struct{}{}
|
||||
}
|
||||
|
||||
82
main.go
82
main.go
@@ -9,6 +9,7 @@ import (
|
||||
"local/truckstop/config"
|
||||
"local/truckstop/message"
|
||||
"log"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -56,37 +57,83 @@ func matrixrecv() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
func() {
|
||||
log.Printf("looking for help")
|
||||
printed := false
|
||||
for _, msg := range messages {
|
||||
if !strings.HasPrefix(msg.Content, "!help") {
|
||||
continue
|
||||
}
|
||||
key := fmt.Sprintf("help_%d", msg.Timestamp.Unix())
|
||||
db := config.Get().DB()
|
||||
if !printed {
|
||||
if _, err := db.Get(key); err == storage.ErrNotFound {
|
||||
log.Printf("sending help")
|
||||
help := fmt.Sprintf("commands:\n...`!help`...print this help\n...`!state nc NC nC Nc`...set states for self\n...`!available 2022-12-31`...set date self is available for work\n\nrun a command for someone else: `!state ga @caleb`")
|
||||
if err := sender.Send(help); err != nil {
|
||||
log.Printf("failed to send help: %v", err)
|
||||
} else {
|
||||
printed = true
|
||||
if err := db.Set(key, []byte{'k'}); err != nil {
|
||||
log.Printf("failed to mark help given @%s: %v", key, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if err := db.Set(key, []byte{'k'}); err != nil {
|
||||
log.Printf("failed to mark help given @%s: %v", key, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
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
|
||||
}
|
||||
if !strings.HasPrefix(msg.Content, "pause until") {
|
||||
continue
|
||||
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
|
||||
}
|
||||
}
|
||||
t, err := time.ParseInLocation(
|
||||
"2006-01-02",
|
||||
strings.TrimSpace(strings.TrimPrefix(msg.Content, "pause until")),
|
||||
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)
|
||||
@@ -95,18 +142,18 @@ func matrixrecv() error {
|
||||
}
|
||||
|
||||
func setNewPauses(pauses map[string]time.Time) {
|
||||
log.Printf("set new pauses: %+v", pauses)
|
||||
if len(pauses) == 0 {
|
||||
return
|
||||
}
|
||||
log.Printf("set new pauses: %+v", pauses)
|
||||
conf := *config.Get()
|
||||
changed := map[string]time.Time{}
|
||||
for client, pause := range pauses {
|
||||
clientconf := conf.Clients[client]
|
||||
if clientconf.PauseUntil.Get().Unix() == pause.Unix() {
|
||||
if clientconf.Available.Get().Unix() == pause.Unix() {
|
||||
continue
|
||||
}
|
||||
clientconf.PauseUntil = config.Time(pause)
|
||||
clientconf.Available = config.Time(pause)
|
||||
conf.Clients[client] = clientconf
|
||||
changed[client] = pause
|
||||
}
|
||||
@@ -138,6 +185,7 @@ func setNewStates(states map[string]map[config.State]struct{}) {
|
||||
})
|
||||
clientconf := conf.Clients[client]
|
||||
if fmt.Sprint(newstates) == fmt.Sprint(clientconf.States) {
|
||||
message.NewMatrix().Send(fmt.Sprintf("%s: still searching for %+v", client, newstates))
|
||||
continue
|
||||
}
|
||||
clientconf.States = newstates
|
||||
@@ -278,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 {
|
||||
@@ -286,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 {
|
||||
|
||||
@@ -41,9 +41,12 @@ func (m Matrix) Receive() ([]Message, error) {
|
||||
log.Printf("matrix.Receive()")
|
||||
messages := make([]Message, 0)
|
||||
for k := range config.Get().Clients {
|
||||
messages = append(messages, Message{Sender: k, Content: "OH"})
|
||||
messages = append(messages, Message{Timestamp: time.Now(), Sender: k, Content: "!state OH"})
|
||||
if k == "bel" {
|
||||
messages = append(messages, Message{Timestamp: time.Now(), Sender: k, Content: "!help"})
|
||||
}
|
||||
if k == "broc" {
|
||||
messages = append(messages, Message{Sender: k, Content: "pause until 2148-10-" + fmt.Sprint(time.Now().Unix()%28)})
|
||||
messages = append(messages, Message{Timestamp: time.Now(), Sender: k, Content: "!available 2148-10-" + fmt.Sprint(time.Now().Unix()%28)})
|
||||
}
|
||||
}
|
||||
return messages, nil
|
||||
@@ -62,6 +65,9 @@ func (m Matrix) Receive() ([]Message, error) {
|
||||
}
|
||||
messages := make([]Message, 0)
|
||||
result, err := c.Messages(m.room, "", "", 'b', 50)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, event := range result.Chunk {
|
||||
if _, ok := matrixIDs[event.Sender]; !ok {
|
||||
continue
|
||||
@@ -70,14 +76,11 @@ func (m Matrix) Receive() ([]Message, error) {
|
||||
case "m.room.message":
|
||||
b, ok := event.Body()
|
||||
if ok {
|
||||
messages = append(messages, Message{Sender: event.Sender, Content: strings.TrimSpace(b)})
|
||||
messages = append(messages, Message{Timestamp: time.Unix(0, event.Timestamp*int64(time.Millisecond)), Sender: event.Sender, Content: strings.TrimSpace(b)})
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
clientChange := regexp.MustCompile("^@[a-z]+")
|
||||
clientChange := regexp.MustCompile("@[a-z]+$")
|
||||
for i := range messages {
|
||||
if found := clientChange.FindString(messages[i].Content); found != "" {
|
||||
messages[i].Content = strings.TrimSpace(strings.ReplaceAll(messages[i].Content, found, ""))
|
||||
@@ -89,6 +92,7 @@ func (m Matrix) Receive() ([]Message, error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
messages[i].Content = strings.TrimSpace(messages[i].Content)
|
||||
}
|
||||
return messages, nil
|
||||
}
|
||||
@@ -115,6 +119,11 @@ 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 {
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package message
|
||||
|
||||
import "time"
|
||||
|
||||
type Sender interface {
|
||||
Send(string) error
|
||||
Receive() ([]Message, error)
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
Sender string
|
||||
Content string
|
||||
Sender string
|
||||
Content string
|
||||
Timestamp time.Time
|
||||
}
|
||||
|
||||
23
todo.yaml
23
todo.yaml
@@ -1,14 +1,5 @@
|
||||
todo:
|
||||
- convert pauseuntil to search results only on and after target date
|
||||
- todo: maps of to+from to get location within state via api
|
||||
details: |
|
||||
curl 'https://maps.googleapis.com/maps/api/staticmap?center=Advance,NC&markers=label=A|Advance,NC&zoom=5&size=250x250&scale=2&format=jpeg&maptype=roadmap&key=AIzaSyBkACm-LQkoSfsTO5_XAzBVZE9-JQzcNkg' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0' -H 'Accept: image/avif,image/webp,*/*' -H 'Accept-Language: en-US,en;q=0.5' -H 'Accept-Encoding: gzip, deflate, br' -H 'DNT: 1' -H 'Alt-Used: maps.googleapis.com' -H 'Connection: keep-alive' -H 'Sec-Fetch-Dest: image' -H 'Sec-Fetch-Mode: no-cors' -H 'Sec-Fetch-Site: cross-site' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' -H 'TE: trailers' > whatever.jpg; open whatever.jpg~/Go/src/local/truckstop
|
||||
subtasks:
|
||||
- DONE; yandex; key 9baa3e42-c6e5-4eb5-a891-05ffcede6a25 yandex maps
|
||||
- google; key AIzaSyBkACm-LQkoSfsTO5_XAzBVZE9-JQzcNkg
|
||||
- !help,
|
||||
- !states optional but explicit option
|
||||
- pause until => !busy until
|
||||
- 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
|
||||
- modify old items once no longer available; drop stale jobs good candidate but requires new matrix interaction
|
||||
@@ -16,9 +7,19 @@ todo:
|
||||
- todo: filter out jobs like CA
|
||||
subtasks:
|
||||
- banlist criteria like vendors, brokers, metadata
|
||||
- setup ma on element !!fluffychat
|
||||
- set up copy for caleb, broc
|
||||
done:
|
||||
- setup ma on element !!fluffychat
|
||||
- !help,
|
||||
- !states optional but explicit option
|
||||
- TEST; convert pauseuntil to search results only on and after target date
|
||||
- pause until => !busy until
|
||||
- todo: maps of to+from to get location within state via api
|
||||
details: |
|
||||
curl 'https://maps.googleapis.com/maps/api/staticmap?center=Advance,NC&markers=label=A|Advance,NC&zoom=5&size=250x250&scale=2&format=jpeg&maptype=roadmap&key=AIzaSyBkACm-LQkoSfsTO5_XAzBVZE9-JQzcNkg' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0' -H 'Accept: image/avif,image/webp,*/*' -H 'Accept-Language: en-US,en;q=0.5' -H 'Accept-Encoding: gzip, deflate, br' -H 'DNT: 1' -H 'Alt-Used: maps.googleapis.com' -H 'Connection: keep-alive' -H 'Sec-Fetch-Dest: image' -H 'Sec-Fetch-Mode: no-cors' -H 'Sec-Fetch-Site: cross-site' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' -H 'TE: trailers' > whatever.jpg; open whatever.jpg~/Go/src/local/truckstop
|
||||
subtasks:
|
||||
- DONE; yandex; key 9baa3e42-c6e5-4eb5-a891-05ffcede6a25 yandex maps
|
||||
- google; key AIzaSyBkACm-LQkoSfsTO5_XAzBVZE9-JQzcNkg
|
||||
- todo: upload g map to imgur
|
||||
details: |
|
||||
w oath d9ac7cabe813d10 9d0b3d82800b30ca88f595d3bcd6985f627d7d82
|
||||
|
||||
Reference in New Issue
Block a user