forked from dolfly/termux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sms.go
99 lines (86 loc) · 2.34 KB
/
sms.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package termux
import (
"bytes"
"encoding/json"
"fmt"
"log"
)
// SMSBoxType enumerates available sms box types
type SMSBoxType int
const (
// All sms box type
All SMSBoxType = 0
// Inbox sms box type
Inbox SMSBoxType = 1
// Sent sms box type
Sent SMSBoxType = 2
// Draft sms box type
Draft SMSBoxType = 3
// Outbox sms box type
Outbox SMSBoxType = 4
)
// SMS represents a piece of SMS
// Example:
/*
{
"threadid": 4,
"type": "inbox",
"read": true,
"number": "+1000001",
"received": "2023-08-15 10:01:40",
"body": "Hello World!",
"_id": 3
}
*/
type SMS struct {
ThreadID int `json:"threadid"` // ThreadID is the unique identifier of the SMS thread
Type string `json:"type"` // Type is the type of the SMS box
Read bool `json:"read"` // Read is the flag indicating whether the SMS has been read
Number string `json:"number"` // Number is the phone number of the sender
Received string `json:"received"` // Received is the time when the SMS was received
Body string `json:"body"` // Body is the content of the SMS
ID int `json:"_id"` // ID is the unique identifier of the SMS
}
func (s SMS) String() string {
return fmt.Sprintf("ThreadID: %v\n"+
"Type: %v\n"+
"Read: %v\n"+
"Number: %v\n"+
"Received: %v\n"+
"Body: %v\n"+
"ID: %v",
s.ThreadID, s.Type, s.Read, s.Number, s.Received, s.Body, s.ID)
}
// SMSList acquires a list of the received SMS in the given SMS box with a given limit and offset
func SMSList(limit int, offset int, box SMSBoxType) ([]SMS, error) {
buf := bytes.NewBuffer([]byte{})
if err := exec(nil, buf, "SmsInbox", map[string]interface{}{
"type": int(box),
"limit": limit,
"offset": offset,
}, ""); err != nil {
return nil, err
}
res := buf.Bytes()
if err := checkErr(res); err != nil {
return nil, err
}
l := make([]SMS, 0)
if err := json.Unmarshal(res, &l); err != nil {
log.Printf("Failed to unmarshal: %v\n", err)
log.Printf("Response: %v\n", res)
return nil, err
}
return l, nil
}
// SMSSend sends a text message to the given recipient numbers
func SMSSend(numbers []string, text string) error {
in := bytes.NewBufferString(text)
buf := bytes.NewBuffer([]byte{})
if err := exec(in, buf, "SmsSend", map[string]interface{}{
"recipients": numbers,
}, ""); err != nil {
return err
}
return checkErr(buf.Bytes())
}