Skip to content

Commit eb3d348

Browse files
committed
Developing SMS functionality
1 parent 71079a6 commit eb3d348

File tree

5 files changed

+98
-4
lines changed

5 files changed

+98
-4
lines changed

drivers/8810ft.go

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"net/url"
99
"strconv"
10+
"strings"
1011
"time"
1112
)
1213

@@ -29,7 +30,7 @@ type (
2930
var httpClient *http.Client
3031

3132
func init() {
32-
httpClient = &http.Client{Timeout: time.Second * 10}
33+
httpClient = &http.Client{Timeout: time.Second * 30}
3334
registerDriver("ZTE 8810FT", newZTE8810FT)
3435
}
3536

@@ -48,6 +49,7 @@ func (m *zte8810ft) getNewRequest(method string, url *url.URL) *http.Request {
4849
URL: url,
4950
Header: http.Header{
5051
"Referer": {fmt.Sprintf("http://%s/index.html", m.ip)},
52+
"Content"
5153
},
5254
}
5355
}
@@ -194,3 +196,80 @@ func (m *zte8810ft) GetCellConnStatus() (*LinkStatus, error) {
194196
return nil, ErrUnknown
195197
}
196198
}
199+
200+
func (m *zte8810ft) SendSMS(phone string, message string) error {
201+
// GET /goform/goform_set_cmd_process?goformId=SEND_SMS
202+
// Prepare everything to make a request
203+
204+
// Encode message into GSM-7
205+
// encodedMsg, err := gsm7.Encode([]byte(message))
206+
// if err != nil {
207+
// return ActionError{"sms send", err}
208+
// }
209+
210+
u := m.getBaseURL("/goform/goform_set_cmd_process")
211+
212+
// Build body
213+
query := u.Query()
214+
query.Add("goformId", "SEND_SMS")
215+
query.Add("ID", "-1")
216+
query.Add("encode_type", "GSM7_default")
217+
query.Add("Number", phone)
218+
// query.Add("MessageBody", fmt.Sprintf("%X", encodedMsg))
219+
query.Add("MessageBody", "0074006500730074")
220+
221+
// Build send timestamp
222+
currTime := time.Now()
223+
if _, tz := currTime.Zone(); tz >= 0 {
224+
query.Add("sms_time", currTime.Format("06;01;02;15;04;05;+")+strconv.Itoa(tz/3600))
225+
} else {
226+
query.Add("sms_time", currTime.Format("06;01;02;15;04;05;")+strconv.Itoa(tz/3600))
227+
}
228+
229+
// data := map[string]string{
230+
// "goformId": "SEND_SMS",
231+
// "Number": phone,
232+
// "sms_time": time.Now().Format("02;01;06;15;04;05;-07"),
233+
// "MessageBody": string(encodedMsg),
234+
// "ID": "-1",
235+
// "encode_type": "GSM7_default",
236+
// }
237+
238+
request := m.getNewRequest("POST", u)
239+
240+
// Some Go-level string manipulation
241+
fmt.Println(query.Encode())
242+
// stringReader := strings.NewReader(query.Encode())
243+
stringReader := strings.NewReader("goformId=SEND_SMS&Number=%2B79124446729&sms_time=24%3B07%3B28%3B19%3B01%3B24%3B%2B4&MessageBody=0074006500730074&ID=-1&encode_type=GSM7_default")
244+
stringReadCloser := io.NopCloser(stringReader)
245+
request.Body = stringReadCloser
246+
247+
248+
resp, err := httpClient.Do(request)
249+
250+
// Process errors
251+
switch {
252+
case err != nil:
253+
return ActionError{Action: "sms send", Err: err}
254+
case resp.StatusCode != 200:
255+
return ActionError{Action: "sms send", Err: fmt.Errorf("response status %d", resp.StatusCode)}
256+
}
257+
258+
// Read the response
259+
defer resp.Body.Close()
260+
body, err := io.ReadAll(resp.Body)
261+
if err != nil {
262+
return ErrUnknown
263+
}
264+
265+
result := new(result)
266+
if err := json.Unmarshal(body, result); err != nil {
267+
return ActionError{Action: "sms send", Err: UnmarshalError{RawData: &body, Err: err}}
268+
}
269+
270+
if result.Result != "success" {
271+
return ActionError{Action: "sms send", Err: fmt.Errorf("result: %s", result.Result)}
272+
}
273+
274+
return nil
275+
}

drivers/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type (
2323
BaseModem
2424

2525
SendSMS(phone string, message string) error
26-
GetAllSMS() ([]SMS, error)
26+
// GetAllSMS() ([]SMS, error)
2727
}
2828
)
2929

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.22
55
require (
66
github.com/alexflint/go-arg v1.5.1
77
github.com/go-playground/validator/v10 v10.22.0
8+
github.com/i582/cfmt v1.4.0
89
github.com/spf13/viper v1.19.0
910
)
1011

@@ -16,7 +17,6 @@ require (
1617
github.com/go-playground/universal-translator v0.18.1 // indirect
1718
github.com/gookit/color v1.3.2 // indirect
1819
github.com/hashicorp/hcl v1.0.0 // indirect
19-
github.com/i582/cfmt v1.4.0 // indirect
2020
github.com/leodido/go-urn v1.4.0 // indirect
2121
github.com/magiconair/properties v1.8.7 // indirect
2222
github.com/mitchellh/mapstructure v1.5.0 // indirect
@@ -28,6 +28,7 @@ require (
2828
github.com/spf13/cast v1.6.0 // indirect
2929
github.com/spf13/pflag v1.0.5 // indirect
3030
github.com/subosito/gotenv v1.6.0 // indirect
31+
github.com/warthog618/sms v0.3.0 // indirect
3132
go.uber.org/atomic v1.9.0 // indirect
3233
go.uber.org/multierr v1.9.0 // indirect
3334
golang.org/x/crypto v0.21.0 // indirect

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ github.com/i582/cfmt v1.4.0 h1:DNugs+dvy3xjJSUk9Oita0udy1YVQh2vDP6cWYhDCIQ=
3030
github.com/i582/cfmt v1.4.0/go.mod h1:tpHWAxhE4Y7yy7sliaNe0pnnEs1SZe67KLljyOlEYI8=
3131
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
3232
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
33+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
34+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
3335
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
3436
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
3537
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
@@ -38,6 +40,7 @@ github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0V
3840
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
3941
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
4042
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
43+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
4144
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
4245
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
4346
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -65,13 +68,16 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE
6568
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
6669
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
6770
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
71+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
6872
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
6973
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
7074
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
7175
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
7276
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7377
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
7478
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
79+
github.com/warthog618/sms v0.3.0 h1:LYAb5ngmu2qjNExgji3B7xi2tIZ9+DsuE9pC5xs4wwc=
80+
github.com/warthog618/sms v0.3.0/go.mod h1:+bYZGeBxu003sxD5xhzsrIPBAjPBzTABsRTwSpd7ld4=
7581
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
7682
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
7783
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
@@ -89,8 +95,12 @@ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
8995
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9096
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
9197
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
98+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
99+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
92100
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
93101
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
102+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
103+
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
94104
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
95105
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
96106
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

mcli.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ func run() error {
135135
if err != nil {
136136
parser.FailSubcommand("Unknown action", "sms")
137137
}
138-
sms.SendSMS(args.SMS.Send.PhoneNumber, args.SMS.Send.PhoneNumber)
138+
139+
err = sms.SendSMS(args.SMS.Send.PhoneNumber, args.SMS.Send.PhoneNumber)
140+
if err != nil {
141+
return err
142+
}
139143
}
140144
case parser.Subcommand() == nil:
141145
parser.Fail("Missing or unknown command")

0 commit comments

Comments
 (0)