consider linked number in AUTO_RECEIVE_SCHEDULE

* up to now, linked numbers weren't considered in the
  AUTO_RECEIVE_SCHEDULE.

see #363
This commit is contained in:
Bernhard B
2023-05-11 20:49:23 +02:00
parent b98f4cf3f9
commit 99da94d048

View File

@@ -3,13 +3,6 @@ package main
import ( import (
"encoding/json" "encoding/json"
"flag" "flag"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/bbernhard/signal-cli-rest-api/api" "github.com/bbernhard/signal-cli-rest-api/api"
"github.com/bbernhard/signal-cli-rest-api/client" "github.com/bbernhard/signal-cli-rest-api/client"
docs "github.com/bbernhard/signal-cli-rest-api/docs" docs "github.com/bbernhard/signal-cli-rest-api/docs"
@@ -19,6 +12,10 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
swaggerFiles "github.com/swaggo/files" swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger" ginSwagger "github.com/swaggo/gin-swagger"
"io/ioutil"
"net/http"
"os"
"strconv"
) )
// @title Signal Cli REST API // @title Signal Cli REST API
@@ -265,22 +262,38 @@ func main() {
log.Fatal("AUTO_RECEIVE_SCHEDULE: Invalid schedule: ", err.Error()) log.Fatal("AUTO_RECEIVE_SCHEDULE: Invalid schedule: ", err.Error())
} }
type SignalCliAccountConfig struct {
Number string `json:"number"`
}
type SignalCliAccountConfigs struct {
Accounts []SignalCliAccountConfig `json:"accounts"`
}
signalCliConfigJsonData, err := ioutil.ReadFile(*signalCliConfig + "/data/accounts.json")
if err != nil {
log.Fatal("AUTO_RECEIVE_SCHEDULE: Couldn't read accounts.json: ", err.Error())
}
var signalCliAccountConfigs SignalCliAccountConfigs
err = json.Unmarshal(signalCliConfigJsonData, &signalCliAccountConfigs)
if err != nil {
log.Fatal("AUTO_RECEIVE_SCHEDULE: Couldn't parse accounts.json: ", err.Error())
}
c := cron.New() c := cron.New()
c.Schedule(schedule, cron.FuncJob(func() { c.Schedule(schedule, cron.FuncJob(func() {
err := filepath.Walk(*signalCliConfig, func(path string, info os.FileInfo, err error) error { for _, account := range signalCliAccountConfigs.Accounts {
filename := filepath.Base(path) log.Debug("AUTO_RECEIVE_SCHEDULE: Calling receive for number ", account.Number)
if strings.HasPrefix(filename, "+") && info.Mode().IsRegular() { resp, err := http.Get("http://127.0.0.1:" + port + "/v1/receive/" + account.Number)
log.Debug("AUTO_RECEIVE_SCHEDULE: Calling receive for number ", filename)
resp, err := http.Get("http://127.0.0.1:" + port + "/v1/receive/" + filename)
if err != nil { if err != nil {
log.Error("AUTO_RECEIVE_SCHEDULE: Couldn't call receive for number ", filename, ": ", err.Error()) log.Error("AUTO_RECEIVE_SCHEDULE: Couldn't call receive for number ", account.Number, ": ", err.Error())
} }
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
jsonResp, err := ioutil.ReadAll(resp.Body) jsonResp, err := ioutil.ReadAll(resp.Body)
resp.Body.Close() resp.Body.Close()
if err != nil { if err != nil {
log.Error("AUTO_RECEIVE_SCHEDULE: Couldn't read json response: ", err.Error()) log.Error("AUTO_RECEIVE_SCHEDULE: Couldn't read json response: ", err.Error())
return nil continue
} }
type ReceiveResponse struct { type ReceiveResponse struct {
@@ -290,19 +303,12 @@ func main() {
err = json.Unmarshal(jsonResp, &receiveResponse) err = json.Unmarshal(jsonResp, &receiveResponse)
if err != nil { if err != nil {
log.Error("AUTO_RECEIVE_SCHEDULE: Couldn't parse json response: ", err.Error()) log.Error("AUTO_RECEIVE_SCHEDULE: Couldn't parse json response: ", err.Error())
return nil continue
} }
log.Error("AUTO_RECEIVE_SCHEDULE: Couldn't call receive for number ", filename, ": ", receiveResponse) log.Error("AUTO_RECEIVE_SCHEDULE: Couldn't call receive for number ", account.Number, ": ", receiveResponse)
} }
} }
return nil
})
if err != nil {
log.Fatal("AUTO_RECEIVE_SCHEDULE: Couldn't get registered numbers")
}
})) }))
c.Start() c.Start()
} }