From 2c3f3a1c3d214cf7288fe0d4b9ef388db624795f Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Sun, 19 Jan 2020 18:20:33 +0100 Subject: [PATCH] added possibility to register number with voice see #7 --- README.md | 10 +++++++++- src/main.go | 29 +++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 17ad0a8..63e5ae5 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,15 @@ Sample REST API calls: ```curl -X POST -H "Content-Type: application/json" 'http://127.0.0.1:8080/v1/register/+431212131491291'``` -* Verify the number using the code received via SMS +* Register a number (with voice verification) + +```curl -X POST -H "Content-Type: application/json" --data '{"use_voice": true}' 'http://127.0.0.1:8080/v1/register/'``` + + e.g: + + ```curl -X POST -H "Content-Type: application/json" --data '{"use_voice": true}' 'http://127.0.0.1:8080/v1/register/+431212131491291'``` + +* Verify the number using the code received via SMS/voice ```curl -X POST -H "Content-Type: application/json" 'http://127.0.0.1:8080/v1/register//verify/'``` diff --git a/src/main.go b/src/main.go index df63801..e5ef100 100644 --- a/src/main.go +++ b/src/main.go @@ -12,7 +12,7 @@ import ( "bytes" "os" "encoding/base64" - //"strings" + "encoding/json" ) func runSignalCli(args []string) error { @@ -55,12 +55,37 @@ func main() { router.POST("/v1/register/:number", func(c *gin.Context) { number := c.Param("number") + type Request struct{ + UseVoice bool `json:"use_voice"` + } + + var req Request + + buf := new(bytes.Buffer) + buf.ReadFrom(c.Request.Body) + if buf.String() != "" { + err := json.Unmarshal(buf.Bytes(), &req) + if err != nil { + log.Error("Couldn't register number: ", err.Error()) + c.JSON(400, "Couldn't process request - invalid request.") + return + } + } else { + req.UseVoice = false + } + if number == "" { c.JSON(400, "Please provide a number") return } - err := runSignalCli([]string{"--config", *signalCliConfig, "-u", number, "register"}) + command := []string{"--config", *signalCliConfig, "-u", number, "register"} + + if req.UseVoice == true { + command = append(command, "--voice") + } + + err := runSignalCli(command) if err != nil { c.JSON(400, err.Error()) return