Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6e7b2cdee8 | ||
|
|
2a519e2883 | ||
|
|
2041e3e20b | ||
|
|
83fb0aa49e | ||
|
|
a1c197cb06 | ||
|
|
59362806e5 |
+23
-19
@@ -14,28 +14,32 @@ jobs:
|
||||
- uses: actions/checkout@v5
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v4
|
||||
uses: actions/setup-go@v6
|
||||
with:
|
||||
go-version: '1.24.4' # You can specify a specific version or 'stable'
|
||||
go-version: '1.26.1' # You can specify a specific version or 'stable'
|
||||
|
||||
- name: Build
|
||||
run: make build
|
||||
run: |
|
||||
echo "Initializing config"
|
||||
mkdir -p ~/.ssh
|
||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/textsender_models_deploy_key
|
||||
chmod 600 ~/.ssh/textsender_models_deploy_key
|
||||
ssh-keyscan ${{ secrets.MY_HOST }} >> ~/.ssh/known_hosts
|
||||
|
||||
eval $(ssh-agent -s)
|
||||
ssh-add -v ~/.ssh/textsender_models_deploy_key
|
||||
|
||||
go env -w GOPRIVATE='${{ secrets.GIT_HOST_ROOT }}'
|
||||
|
||||
echo "Creating local .gitconfig"
|
||||
touch ~/.gitconfig
|
||||
cat > ~/.gitconfig << "EOF"
|
||||
[url "ssh://git@${{ secrets.GIT_HOST_ROOT }}"]
|
||||
insteadOf = https://${{ secrets.GIT_HOST_ROOT }}
|
||||
EOF
|
||||
|
||||
echo "Building binary"
|
||||
make build
|
||||
|
||||
- name: Test
|
||||
run: go test -v ./...
|
||||
|
||||
- name: Run gofmt (optional)
|
||||
# Uncomment to check code formatting with gofmt
|
||||
run: |
|
||||
if [ -n "$(gofmt -l.)" ]; then
|
||||
echo "Go code is not formatted. Please run 'gofmt -w.' to fix it."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: Run golint (optional)
|
||||
# Uncomment to check code style with golint
|
||||
run: |
|
||||
if [ -n "$(golint./...)" ]; then
|
||||
echo "Go code has style issues. Please run 'golint./...' to see them."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -2,3 +2,4 @@
|
||||
/messages
|
||||
/numbers
|
||||
/config.json
|
||||
/vendor
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
# This file is a template, and might need editing before it works on your project.
|
||||
# You can copy and paste this template into a new `.gitlab-ci.yml` file.
|
||||
# You should not add this template to an existing `.gitlab-ci.yml` file by using the `include:` keyword.
|
||||
#
|
||||
# To contribute improvements to CI/CD templates, please follow the Development guide at:
|
||||
# https://docs.gitlab.com/ee/development/cicd/templates.html
|
||||
# This specific template is located at:
|
||||
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Go.gitlab-ci.yml
|
||||
|
||||
image: golang:1.24
|
||||
|
||||
stages:
|
||||
- test
|
||||
- build
|
||||
# - deploy
|
||||
|
||||
format:
|
||||
stage: test
|
||||
script:
|
||||
- go fmt $(go list ./... | grep -v /vendor/)
|
||||
- go vet $(go list ./... | grep -v /vendor/)
|
||||
- go test -race $(go list ./... | grep -v /vendor/)
|
||||
|
||||
compile:
|
||||
stage: build
|
||||
script:
|
||||
- make build
|
||||
|
||||
artifacts:
|
||||
paths:
|
||||
- mybinaries
|
||||
|
||||
# deploy:
|
||||
# stage: deploy
|
||||
# script: echo "Define your deployment script!"
|
||||
# environment: production
|
||||
@@ -6,16 +6,16 @@ GO_VERSION ?= $(shell go version | awk '{print $$3}')
|
||||
.PHONY: build
|
||||
build:
|
||||
go build -ldflags="\
|
||||
-X 'git.kundeng.us/phoenix/sender/version.Version=$(VERSION)' \
|
||||
-X 'git.kundeng.us/phoenix/sender/version.BuildTime=$(BUILD_TIME)' \
|
||||
-X 'git.kundeng.us/phoenix/sender/version.Commit=$(COMMIT)' \
|
||||
-X 'git.kundeng.us/phoenix/sender/version.GoVersion=$(GO_VERSION)'" \
|
||||
-o sender main.go
|
||||
-X 'git.kundeng.us/phoenix/sender/internal/version.Version=$(VERSION)' \
|
||||
-X 'git.kundeng.us/phoenix/sender/internal/version.BuildTime=$(BUILD_TIME)' \
|
||||
-X 'git.kundeng.us/phoenix/sender/internal/version.Commit=$(COMMIT)' \
|
||||
-X 'git.kundeng.us/phoenix/sender/internal/version.GoVersion=$(GO_VERSION)'" \
|
||||
-o sender cmd/sender/main.go
|
||||
|
||||
.PHONY: install
|
||||
install:
|
||||
go install -ldflags="\
|
||||
-X 'git.kundeng.us/phoenix/sender/version.Version=$(VERSION)' \
|
||||
-X 'git.kundeng.us/phoenix/sender/version.BuildTime=$(BUILD_TIME)' \
|
||||
-X 'git.kundeng.us/phoenix/sender/version.Commit=$(COMMIT)' \
|
||||
-X 'git.kundeng.us/phoenix/sender/version.GoVersion=$(GO_VERSION)'"
|
||||
-X 'git.kundeng.us/phoenix/sender/internal/version.Version=$(VERSION)' \
|
||||
-X 'git.kundeng.us/phoenix/sender/internal/version.BuildTime=$(BUILD_TIME)' \
|
||||
-X 'git.kundeng.us/phoenix/sender/internal/version.Commit=$(COMMIT)' \
|
||||
-X 'git.kundeng.us/phoenix/sender/internal/version.GoVersion=$(GO_VERSION)'"
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"git.kundeng.us/phoenix/sender/internal/config"
|
||||
"git.kundeng.us/phoenix/sender/internal/messaging"
|
||||
"git.kundeng.us/phoenix/sender/internal/util"
|
||||
"git.kundeng.us/phoenix/sender/internal/version"
|
||||
)
|
||||
|
||||
func main() {
|
||||
myArgs := os.Args
|
||||
argCount := len(myArgs)
|
||||
|
||||
if config.IsVersionFlagPresent() {
|
||||
fmt.Println(config.App_Name)
|
||||
fmt.Println(version.String())
|
||||
return
|
||||
} else if argCount < 4 {
|
||||
log.Printf("Argument count: %v\n", argCount)
|
||||
log.Fatal("Provide argument to config file and number file")
|
||||
}
|
||||
|
||||
configPath, numberPath, messagePath := util.GetPaths(myArgs)
|
||||
|
||||
log.Printf("Config file path: %s\n", configPath)
|
||||
log.Println("Numbers file path:", numberPath)
|
||||
log.Println("Message path:", messagePath)
|
||||
|
||||
cfg, _ := config.ParseConfig(configPath)
|
||||
cfg.PrintConfig()
|
||||
|
||||
numbers, err := util.ParseNumbers(numberPath)
|
||||
if err != nil {
|
||||
log.Println("Error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
prepMessage, err := util.ParseMessage(messagePath)
|
||||
if err != nil {
|
||||
log.Println("Error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(numbers) == 0 || prepMessage.Content == "" {
|
||||
if len(numbers) == 0 {
|
||||
log.Println("No numbers to send")
|
||||
os.Exit(-1)
|
||||
} else {
|
||||
log.Println("No message to send")
|
||||
os.Exit(-1)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("")
|
||||
log.Println("Message:", prepMessage.Content)
|
||||
|
||||
messaging.SendMessages(cfg, numbers, *prepMessage, 5)
|
||||
}
|
||||
@@ -1,11 +1,16 @@
|
||||
module git.kundeng.us/phoenix/sender
|
||||
|
||||
go 1.24.4
|
||||
|
||||
require github.com/twilio/twilio-go v1.26.3
|
||||
go 1.26.1
|
||||
|
||||
require (
|
||||
github.com/golang-jwt/jwt/v5 v5.2.2 // indirect
|
||||
github.com/golang/mock v1.6.0 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
git.kundeng.us/phoenix/swoosh v0.2.0
|
||||
git.kundeng.us/phoenix/textsender-models v0.2.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1 // indirect
|
||||
github.com/golang/mock v1.6.0 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/twilio/twilio-go v1.30.4 // indirect
|
||||
)
|
||||
|
||||
@@ -1,28 +1,42 @@
|
||||
git.kundeng.us/phoenix/swoosh v0.0.12 h1:OtGUYJzakpzJ0fF2BP/z+maCtEfG+avQUJiCY9LMix8=
|
||||
git.kundeng.us/phoenix/swoosh v0.0.12/go.mod h1:5J/URwiWcjksaPmczelvBEeLdwB1mACFuhUdtWTheDg=
|
||||
git.kundeng.us/phoenix/swoosh v0.2.0 h1:aecJdtRhgQofaOfki0fm+YszUuwKyfKqF5YVb20yxHI=
|
||||
git.kundeng.us/phoenix/swoosh v0.2.0/go.mod h1:NxWD9iUunLIJRgTvlZbZa6jyDHc0JPPRuTmsrxV/hMs=
|
||||
git.kundeng.us/phoenix/textsender-models v0.0.12 h1:ps9H3FS5LyCwQhAiIvg4vYyfLZZ64dex1y9ytb0o9C4=
|
||||
git.kundeng.us/phoenix/textsender-models v0.0.12/go.mod h1:9iPDQJg1Tc6WMNoW5+f8YKmnosMwlWHJ++hmxNLDEe0=
|
||||
git.kundeng.us/phoenix/textsender-models v0.2.0 h1:smz8Fs8VOs1Ya23txbOM0YPRidZIsM0yE9unHF0D/nQ=
|
||||
git.kundeng.us/phoenix/textsender-models v0.2.0/go.mod h1:3CkqA/HFKPhpMYxkKn5uVbZEzEbG3sofLZE8pZ1BHO4=
|
||||
github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
|
||||
github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeDy8=
|
||||
github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
|
||||
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
|
||||
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/localtunnel/go-localtunnel v0.0.0-20170326223115-8a804488f275 h1:IZycmTpoUtQK3PD60UYBwjaCUHUP7cML494ao9/O8+Q=
|
||||
github.com/localtunnel/go-localtunnel v0.0.0-20170326223115-8a804488f275/go.mod h1:zt6UU74K6Z6oMOYJbJzYpYucqdcQwSMPBEdSvGiaUMw=
|
||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/twilio/twilio-go v1.16.1 h1:tzoAOtQiWWnKeR5EZpSlCyElRLtvrMjLeKfohtnraik=
|
||||
github.com/twilio/twilio-go v1.16.1/go.mod h1:tdnfQ5TjbewoAu4lf9bMsGvfuJ/QU9gYuv9yx3TSIXU=
|
||||
github.com/twilio/twilio-go v1.23.13 h1:R5RM5rtIR2egUjQMYxN9jNwKoYVDD7MMBGpBcv/kauw=
|
||||
github.com/twilio/twilio-go v1.23.13/go.mod h1:zRkMjudW7v7MqQ3cWNZmSoZJ7EBjPZ4OpNh2zm7Q6ko=
|
||||
github.com/twilio/twilio-go v1.26.3 h1:K2mYBzbhPVyWF+Jq5Sw53edBFvkgWo4sKTvgaO7461I=
|
||||
github.com/twilio/twilio-go v1.26.3/go.mod h1:FpgNWMoD8CFnmukpKq9RNpUSGXC0BwnbeKZj2YHlIkw=
|
||||
github.com/twilio/twilio-go v1.28.8 h1:wbFz7Wt4S5mCEaes6FcM/ddcJGIhdjwp/9CHb9e+4fk=
|
||||
github.com/twilio/twilio-go v1.28.8/go.mod h1:FpgNWMoD8CFnmukpKq9RNpUSGXC0BwnbeKZj2YHlIkw=
|
||||
github.com/twilio/twilio-go v1.30.4 h1:Whrz37IykDD9KJI2YX4LWaGxYWZoYB7va8fASGDrLng=
|
||||
github.com/twilio/twilio-go v1.30.4/go.mod h1:QbitvbvtkV77Jn4BABAKVmxabYSjMyQG4tHey9gfPqg=
|
||||
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
@@ -48,4 +62,6 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
||||
@@ -3,10 +3,10 @@ package config
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"git.kundeng.us/phoenix/sender/models"
|
||||
axlry "git.kundeng.us/phoenix/textsender-models/tx0/config/auxiliary"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -20,26 +20,25 @@ func IsVersionFlagPresent() bool {
|
||||
if *versionFlag {
|
||||
return true
|
||||
} else {
|
||||
return true
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func ParseConfig(filepath string) (config models.TwiloDetails, success bool) {
|
||||
func ParseConfig(filepath string) (config axlry.TwilioConfig, success bool) {
|
||||
content, err := os.ReadFile(filepath)
|
||||
|
||||
fmt.Println("Reading config file...")
|
||||
fmt.Printf("%s\n", filepath)
|
||||
log.Println("Reading config file...")
|
||||
log.Println(filepath)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("An error reading the file")
|
||||
|
||||
log.Println("An error reading the file")
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(content, &config)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("An error occurred")
|
||||
log.Println("An error occurred")
|
||||
return config, false
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package messaging
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.kundeng.us/phoenix/swoosh/swoop/send"
|
||||
cnfg "git.kundeng.us/phoenix/textsender-models/tx0/config/auxiliary"
|
||||
"git.kundeng.us/phoenix/textsender-models/tx0/contact"
|
||||
msg "git.kundeng.us/phoenix/textsender-models/tx0/message"
|
||||
)
|
||||
|
||||
func SendMessages(config cnfg.TwilioConfig, numbers [](contact.Contact), message msg.Message, secondInterval int) {
|
||||
log.Println("Sending message")
|
||||
|
||||
longSleepLimit := 5
|
||||
count := 0
|
||||
|
||||
var snr send.MessageSender
|
||||
snr.Config = &config
|
||||
|
||||
for _, number := range numbers {
|
||||
numberToSend := number
|
||||
log.Println("Sending to: " + numberToSend.PhoneNumber)
|
||||
|
||||
if resp, _, err := snr.Send(message, number, nil); err != nil {
|
||||
log.Println("Error sending:", err)
|
||||
} else {
|
||||
if response, err := json.Marshal(*resp); err != nil {
|
||||
log.Println("Error parsing:", err)
|
||||
} else {
|
||||
log.Println("Response:", string(response))
|
||||
}
|
||||
}
|
||||
|
||||
log.Println("Sleeping")
|
||||
if count%longSleepLimit == 0 {
|
||||
// sleep for X seconds
|
||||
tme := 1 * time.Second
|
||||
time.Sleep(tme)
|
||||
} else {
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-models/tx0/contact"
|
||||
"git.kundeng.us/phoenix/textsender-models/tx0/message"
|
||||
)
|
||||
|
||||
func GetPaths(args []string) (configPath string, numberPath string, messagePath string) {
|
||||
configPath = args[1]
|
||||
numberPath = args[2]
|
||||
messagePath = args[3]
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func ParseNumbers(filepath string) (numbers [](contact.Contact), err error) {
|
||||
var nmbrs []contact.Contact
|
||||
|
||||
content, err := os.ReadFile(filepath)
|
||||
|
||||
if err != nil {
|
||||
log.Println("An error occurred")
|
||||
return numbers, err
|
||||
} else {
|
||||
if err = json.Unmarshal(content, &nmbrs); err != nil {
|
||||
log.Println("An error occurred:", err)
|
||||
return numbers, err
|
||||
} else {
|
||||
return nmbrs, nil
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func ParseMessage(filepath string) (*message.Message, error) {
|
||||
var msg message.Message
|
||||
|
||||
content, err := os.ReadFile(filepath)
|
||||
|
||||
if err != nil {
|
||||
log.Println("An error occurred")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(content, &msg)
|
||||
|
||||
if err != nil {
|
||||
log.Println("An error occurred")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &msg, nil
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.kundeng.us/phoenix/sender/config"
|
||||
"git.kundeng.us/phoenix/sender/messaging"
|
||||
"git.kundeng.us/phoenix/sender/models"
|
||||
"git.kundeng.us/phoenix/sender/util"
|
||||
"git.kundeng.us/phoenix/sender/version"
|
||||
)
|
||||
|
||||
func main() {
|
||||
myArgs := os.Args
|
||||
argCount := len(myArgs)
|
||||
|
||||
if config.IsVersionFlagPresent() {
|
||||
fmt.Println(config.App_Name)
|
||||
fmt.Println(version.String())
|
||||
return
|
||||
} else if argCount < 4 {
|
||||
fmt.Printf("Argument count: %v\n", argCount)
|
||||
fmt.Println("Provide argument to config file and number file")
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
configPath, numberPath, messagePath := util.GetPaths(myArgs)
|
||||
|
||||
fmt.Printf("Config file path: %s\n", configPath)
|
||||
|
||||
cfg, _ := config.ParseConfig(configPath)
|
||||
|
||||
cfg.PrintConfig()
|
||||
|
||||
fmt.Println("Numbers file path:", numberPath)
|
||||
|
||||
numbers := util.ParseNumbers(numberPath)
|
||||
|
||||
if len(numbers) == 0 {
|
||||
fmt.Println("No numbers to send")
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
models.PrintNumbers(numbers)
|
||||
fmt.Println("")
|
||||
|
||||
fmt.Println("Message path:", messagePath)
|
||||
|
||||
message := util.ParseMessage(messagePath)
|
||||
|
||||
if message.IsEmpty() {
|
||||
fmt.Println("No message to send")
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
message.Print()
|
||||
|
||||
messaging.SendMessages(cfg, numbers, message, 5)
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package messaging
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/twilio/twilio-go"
|
||||
twilioApi "github.com/twilio/twilio-go/rest/api/v2010"
|
||||
|
||||
"git.kundeng.us/phoenix/sender/models"
|
||||
)
|
||||
|
||||
func SendMessages(config models.TwiloDetails, numbers [](models.Number), message models.Message, secondInterval int) {
|
||||
fmt.Println("Sending message")
|
||||
|
||||
longSleepLimit := 5
|
||||
count := 0
|
||||
|
||||
client := initClient(config)
|
||||
|
||||
for _, number := range numbers {
|
||||
numberToSend := number
|
||||
|
||||
if numberToSend.IsEmpty() {
|
||||
fmt.Println("Empty number")
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Println(numberToSend)
|
||||
|
||||
params := &twilioApi.CreateMessageParams{}
|
||||
params.SetTo(numberToSend.PhoneNumber)
|
||||
params.SetFrom(config.Number)
|
||||
params.SetBody(message.Text)
|
||||
|
||||
fmt.Println("Sending to: " + numberToSend.PhoneNumber)
|
||||
|
||||
resp, err := client.Api.CreateMessage(params)
|
||||
if err != nil {
|
||||
fmt.Println("Error sending SMS message: " + err.Error())
|
||||
} else {
|
||||
response, _ := json.Marshal(*resp)
|
||||
fmt.Println("Response: " + string(response))
|
||||
}
|
||||
|
||||
if count%longSleepLimit == 0 {
|
||||
// sleep for X seconds
|
||||
tme := 5 * time.Second
|
||||
time.Sleep(tme)
|
||||
} else {
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func initClient(config models.TwiloDetails) *twilio.RestClient {
|
||||
return twilio.NewRestClientWithParams(twilio.ClientParams{
|
||||
Username: config.AccountSID,
|
||||
Password: config.AuthToken,
|
||||
})
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Number struct {
|
||||
PhoneNumber string `json:"number"`
|
||||
}
|
||||
|
||||
func (nbr Number) IsEmpty() bool {
|
||||
if nbr.PhoneNumber == "" {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func PrintNumbers(numbers [](Number)) {
|
||||
for _, element := range numbers {
|
||||
fmt.Println("Phone number:", element.PhoneNumber)
|
||||
}
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
func (msg Message) IsEmpty() bool {
|
||||
if msg.Text == "" {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (msg Message) Print() {
|
||||
fmt.Println("Message:", msg.Text)
|
||||
}
|
||||
|
||||
type TwiloDetails struct {
|
||||
AccountSID string `json:"auth_sid"`
|
||||
ServiceSID string `json:"service_sid"`
|
||||
URI string `json:"uri"`
|
||||
AuthToken string `json:"auth_token"`
|
||||
Number string `json:"phone_number"`
|
||||
}
|
||||
|
||||
func (config TwiloDetails) PrintConfig() {
|
||||
fmt.Printf("Account SID: %s\n", config.AccountSID)
|
||||
fmt.Printf("Service SID: %s\n", config.ServiceSID)
|
||||
fmt.Println("URI:", config.URI)
|
||||
fmt.Printf("Auth Token: %s\n", config.AuthToken)
|
||||
fmt.Printf("Number: %s\n", config.Number)
|
||||
}
|
||||
|
||||
func Init() TwiloDetails {
|
||||
return TwiloDetails{
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -19,6 +19,6 @@ send_message="True"
|
||||
echo "Changing directory to $root_dir"
|
||||
cd $root_dir
|
||||
|
||||
./twilio_sender "$config_path" "$numbers_path" "$messages_path"
|
||||
./sender "$config_path" "$numbers_path" "$messages_path"
|
||||
|
||||
echo "Done"
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
package util
|
||||
|
||||
import "encoding/json"
|
||||
import "fmt"
|
||||
import "os"
|
||||
import "strings"
|
||||
|
||||
import (
|
||||
"git.kundeng.us/phoenix/sender/models"
|
||||
)
|
||||
|
||||
func GetPaths(args []string) (configPath string, numberPath string, messagePath string) {
|
||||
configPath = args[1]
|
||||
numberPath = args[2]
|
||||
messagePath = args[3]
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func ParseNumbers(filepath string) (numbers [](models.Number)) {
|
||||
nmbrs := [](string){}
|
||||
|
||||
content, err := os.ReadFile(filepath)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("An error occurred")
|
||||
return numbers
|
||||
}
|
||||
|
||||
err = json.Unmarshal(content, &nmbrs)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("An error occurred")
|
||||
return numbers
|
||||
}
|
||||
|
||||
for _, nbr := range nmbrs {
|
||||
var number models.Number
|
||||
number.PhoneNumber = nbr
|
||||
numbers = append(numbers, number)
|
||||
}
|
||||
|
||||
return numbers
|
||||
}
|
||||
|
||||
func ParseMessage(filepath string) (message models.Message) {
|
||||
msg := [](string){""}
|
||||
|
||||
content, err := os.ReadFile(filepath)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("An error occurred")
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(content, &msg)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("An error occurred")
|
||||
return
|
||||
}
|
||||
|
||||
message.Text = strings.Join(msg, "")
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user