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 (
"encoding/json"
"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/client"
docs "github.com/bbernhard/signal-cli-rest-api/docs"
@@ -19,6 +12,10 @@ import (
log "github.com/sirupsen/logrus"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"io/ioutil"
"net/http"
"os"
"strconv"
)
// @title Signal Cli REST API
@@ -265,22 +262,38 @@ func main() {
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.Schedule(schedule, cron.FuncJob(func() {
err := filepath.Walk(*signalCliConfig, func(path string, info os.FileInfo, err error) error {
filename := filepath.Base(path)
if strings.HasPrefix(filename, "+") && info.Mode().IsRegular() {
log.Debug("AUTO_RECEIVE_SCHEDULE: Calling receive for number ", filename)
resp, err := http.Get("http://127.0.0.1:" + port + "/v1/receive/" + filename)
for _, account := range signalCliAccountConfigs.Accounts {
log.Debug("AUTO_RECEIVE_SCHEDULE: Calling receive for number ", account.Number)
resp, err := http.Get("http://127.0.0.1:" + port + "/v1/receive/" + account.Number)
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 {
jsonResp, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Error("AUTO_RECEIVE_SCHEDULE: Couldn't read json response: ", err.Error())
return nil
continue
}
type ReceiveResponse struct {
@@ -290,19 +303,12 @@ func main() {
err = json.Unmarshal(jsonResp, &receiveResponse)
if err != nil {
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()
}