Repository navigation
Expand file tree
/
Copy pathsms.go
More file actions
134 lines (116 loc) · 3.42 KB
/
Copy pathsms.go
File metadata and controls
134 lines (116 loc) · 3.42 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/die-net/lrucache"
"github.com/docopt/docopt-go"
"github.com/fiatjaf/lntxbot/t"
"github.com/gregjones/httpcache"
)
// powered by sms4sats.com
var sms4satsHttpClient = &http.Client{
Transport: httpcache.NewTransport(
lrucache.New(5, 60*5),
),
}
func handleReceiveSMS(ctx context.Context, opts docopt.Opts) {
u := ctx.Value("initiator").(*User)
country := "usa"
if str, ok := opts["<country>"].(string); ok {
country = str
}
service := "other"
if str, ok := opts["<service>"].(string); ok {
service = str
}
params, _ := json.Marshal(struct {
Country string `json:"country"`
Service string `json:"service"`
Ref string `json:"ref"`
}{country, service, "fiatjaf@lntxbot.com"})
resp, err := sms4satsHttpClient.Post("https://api2.sms4sats.com/createorder", "application/json", bytes.NewBuffer(params))
if err != nil {
send(ctx, u, t.ERROR, t.T{
"Err": fmt.Sprintf("failed to call sms4sats.com API: %s", err.Error()),
})
return
}
if resp.StatusCode != 200 {
b, _ := ioutil.ReadAll(resp.Body)
text := string(b)
send(ctx, u, t.ERROR, t.T{
"Err": fmt.Sprintf("sms4sats.com API returned an error (%d): %s", resp.StatusCode, text),
})
return
}
var val struct {
Status string `json:"status"`
OrderId string `json:"orderId"`
Payreq string `json:"payreq"`
}
if err := json.NewDecoder(resp.Body).Decode(&val); err != nil {
send(ctx, u, t.ERROR, t.T{
"Err": fmt.Sprintf("sms4sats.com API returned a broken response while creating order"),
})
return
}
processingMessageId := send(ctx, u, val.Payreq+"\n\n"+translate(ctx, t.PROCESSING))
if _, err := u.payInvoice(ctx, val.Payreq, 0); err != nil {
send(ctx, u, t.ERROR, t.T{"Err": err.Error()}, processingMessageId)
} else {
go func() {
// sms4sats uses hold invoices
// <-waitPaymentSuccess(hash)
time.Sleep(20 * time.Second)
if resp, err := http.Get("https://api2.sms4sats.com/orderstatus?orderId=" + val.OrderId); err == nil {
var val2 struct {
Number int `json:"number"`
Status string `json:"status"`
Paid bool `json:"paid"`
// Error string `json:"error"`
}
if err := json.NewDecoder(resp.Body).Decode(&val2); err != nil {
send(ctx, u, t.ERROR, t.T{
"Err": fmt.Sprintf("sms4sats.com API returned a broken response from status check"),
})
return
}
if val2.Number == 0 {
send(ctx, u, t.ERROR, t.T{"Err": "Failed to get a number, try another country/service e.g. /sms russia twitter"}, processingMessageId)
return
}
numberMessageId := send(ctx, u, t.SMSRECEIVE, t.T{
"number": val2.Number,
"country": country,
"service": service,
"orderId": val.OrderId,
})
// continue checking orderstatus to see if code is received, then send to user
// check orderstatus every 5 seconds for 20 minutes and then give up
count := 0
for count < 1200 {
if resp, err := http.Get("https://api2.sms4sats.com/orderstatus?orderId=" + val.OrderId); err == nil {
var val3 struct {
Code string `json:"code"`
}
if err := json.NewDecoder(resp.Body).Decode(&val3); err != nil {
return
}
if val3.Code != "" {
count = 1200
send(ctx, u, numberMessageId, t.SMSSTATUS, t.T{"code": val3.Code})
break
}
}
time.Sleep(5 * time.Second)
count += 5
}
}
}()
}
}