voipms needs a callout to another api to send responses.. wew

This commit is contained in:
Aaron Bieber 2024-07-18 20:53:18 -06:00
parent 2cee88ad7c
commit 06cbf905b6
No known key found for this signature in database

View File

@ -2,8 +2,10 @@ package chats
import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
"golang.org/x/crypto/bcrypt"
@ -19,12 +21,48 @@ func smsCanSend(number string, numbers []string) bool {
return false
}
type voipms struct {
did string
dst string
message string
method string
apiUser string
apiPassword string
}
func (v voipms) UrlStr() string {
base, _ := url.Parse("https://voip.ms/api/v1/rest.php")
params := url.Values{}
params.Add("did", v.did)
params.Add("dst", v.dst)
params.Add("method", v.method)
params.Add("api_username", v.apiUser)
params.Add("api_password", v.apiPassword)
params.Add("message", v.message)
base.RawQuery = params.Encode()
return base.String()
}
func sendVoipmsResp(v voipms) error {
resp, err := http.Get(v.UrlStr())
if err != nil {
return err
}
defer resp.Body.Close()
log.Println(io.ReadAll(resp.Body))
return nil
}
// SMSListen listens for our incoming sms
func SMSListen(store ChatStore, plugins *plugins.Plugins) {
var (
smsPort, _ = store.Get("sms_listen")
smsAllowed, _ = store.Get("sms_users")
smsUsers = strings.Split(smsAllowed, ",")
voipmsUser, _ = store.Get("voipms_user")
voipmsPass, _ = store.Get("voipms_api_pass")
)
if smsPort != "" {
@ -67,9 +105,32 @@ func SMSListen(store ChatStore, plugins *plugins.Plugins) {
msg = r.Form.Get("Body")
from = r.Form.Get("From")
case http.MethodGet:
// voip.ms
// to={TO}&from={FROM}&message={MESSAGE}&id={ID}&date={TIMESTAMP}
msg = r.URL.Query().Get("message")
from = r.URL.Query().Get("from")
to := r.URL.Query().Get("to")
for _, p := range *plugins {
if p.Match(from, msg) {
log.Printf("%s: responding to '%s'", p.Name(), from)
p.SetStore(store)
resp := p.Process(from, msg)
err := sendVoipmsResp(voipms{
did: from,
dst: to,
message: resp,
method: "sendSMS",
apiUser: voipmsUser,
apiPassword: voipmsPass,
})
if err != nil {
log.Println(err)
}
}
}
return
default:
http.Error(
w,