Skip to content

Commit

Permalink
API Added
Browse files Browse the repository at this point in the history
  • Loading branch information
keyurboss committed Apr 9, 2024
1 parent 0f1a4fc commit 279da25
Show file tree
Hide file tree
Showing 8 changed files with 477 additions and 24 deletions.
88 changes: 81 additions & 7 deletions apis/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strconv"

"github.com/gofiber/fiber/v2"
"github.com/rpsoftech/whatsapp-http-api/interfaces"
Expand All @@ -16,23 +17,25 @@ type (
To []string `json:"to" validate:"required,dive,min=1"`
Msg string `json:"msg"`
}
apiSendMediaMsg struct {
To []string `json:"to" validate:"required,dive,min=1"`
Msg string `json:"msg" validate:"required,min=3"`
apiSendMediaMsgWithBase64 struct {
apiSendMessage
FileName string `json:"fileName" validate:"required,min=3"`
Base64 string `json:"base64" validate:"required,min=3"`
}
)

func AddApis(app fiber.Router) {
app.Get("/qr_code", GetQrCode)
app.Post("/send_message", SendMessage)
app.Post("/send_media", SendMediaFile)
app.Post("/send_media_64", SendMediaFileWithBase64)
// auth.AddAuthPackages(app.Group("/auth"))
// data.AddDataPackage(app.Group("/data"))
// order.AddOrderPackage(app.Group("/order"))
}

func SendMediaFile(c *fiber.Ctx) error {
body := new(apiSendMediaMsg)
body := new(apiSendMessage)
c.BodyParser(body)
number, err := interfaces.ExtractNumberFromCtx(c)
if err != nil {
Expand All @@ -48,7 +51,6 @@ func SendMediaFile(c *fiber.Ctx) error {
Extra: err,
}
}
extensionName := file.Header.Get("Content-Type")
json.Unmarshal([]byte(c.FormValue("to", "[]")), &body.To)
json.Unmarshal([]byte(c.FormValue("msg", "")), &body.Msg)
if err := utility.ValidateReqInput(body); err != nil {
Expand Down Expand Up @@ -79,15 +81,63 @@ func SendMediaFile(c *fiber.Ctx) error {
Extra: err,
}
}
runHeadLess, err := strconv.ParseBool(interfaces.ExtractKeyFromHeader(c, "Headless"))
if err != nil {
runHeadLess = false
}
if runHeadLess {
go connection.SendMediaFile(body.To, destination, file.Filename, body.Msg)
return c.JSON(fiber.Map{
"success": true,
})
} else {
return c.JSON(connection.SendMediaFile(body.To, destination, file.Filename, body.Msg))
}
}
func SendMediaFileWithBase64(c *fiber.Ctx) error {
body := new(apiSendMediaMsgWithBase64)
c.BodyParser(body)
number, err := interfaces.ExtractNumberFromCtx(c)
if err != nil {
return err
}
if err := utility.ValidateReqInput(body); err != nil {
return err
}

return c.JSON(connection.SendMediaFile(body.To, destination, file.Filename, extensionName, body.Msg))
connection, ok := whatsapp.ConnectionMap[number]
if !ok || connection == nil {
return &interfaces.RequestError{
StatusCode: http.StatusNotFound,
Code: interfaces.ERROR_CONNECTION_NOT_FOUND,
Message: fmt.Sprintf("Number %s Not Found", number),
Name: "ERROR_CONNECTION_NOT_FOUND",
}
}
err = connection.ReturnStatusError()
if err != nil {
return err
}
runHeadLess, err := strconv.ParseBool(interfaces.ExtractKeyFromHeader(c, "Headless"))
if err != nil {
runHeadLess = false
}
if runHeadLess {
go connection.SendMediaFileBase64(body.To, body.Base64, body.FileName, body.Msg)
return c.JSON(fiber.Map{
"success": true,
})
} else {
return c.JSON(connection.SendMediaFileBase64(body.To, body.Base64, body.FileName, body.Msg))
}
}
func SendMessage(c *fiber.Ctx) error {
body := new(apiSendMessage)
c.BodyParser(body)
if err := utility.ValidateReqInput(body); err != nil {
return err
}

number, err := interfaces.ExtractNumberFromCtx(c)
if err != nil {
return err
Expand All @@ -105,9 +155,33 @@ func SendMessage(c *fiber.Ctx) error {
if err != nil {
return err
}
return c.JSON(connection.SendTextMessage(body.To, body.Msg))

runHeadLess, err := strconv.ParseBool(interfaces.ExtractKeyFromHeader(c, "Headless"))
if err != nil {
runHeadLess = false
}
if runHeadLess {
go connection.SendTextMessage(body.To, body.Msg)
return c.JSON(fiber.Map{
"success": true,
})
} else {
return c.JSON(connection.SendTextMessage(body.To, body.Msg))
}
}

// GetQrCode returns the QR Code of the connection based on the number in the request context
// @Summary Returns the QR Code of the connection
// @Description Returns the QR Code of the connection based on the number in the request context
// @Tags Connection
// @Accept json
// @Produce json
// @Param number path string true "Number"
// @Success 200 {object} fiber.Map{qrCode string}
// @Success 200 {object} fiber.Map{success bool}
// @Failure 404 {object} interfaces.RequestError
// @Failure 500 {object} interfaces.RequestError
// @Router /connections/{number}/qrcode [get]
func GetQrCode(c *fiber.Ctx) error {
number, err := interfaces.ExtractNumberFromCtx(c)
if err != nil {
Expand Down
30 changes: 30 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,57 @@ require (

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.2.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-openapi/analysis v0.21.4 // indirect
github.com/go-openapi/errors v0.20.3 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/loads v0.21.2 // indirect
github.com/go-openapi/runtime v0.26.0 // indirect
github.com/go-openapi/spec v0.21.0 // indirect
github.com/go-openapi/strfmt v0.21.7 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-openapi/validate v0.22.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/gofiber/contrib/swagger v1.1.1 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.17.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rs/zerolog v1.32.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/swaggo/swag v1.16.3 // indirect
github.com/urfave/cli/v2 v2.27.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.52.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
go.mau.fi/libsignal v0.1.0 // indirect
go.mau.fi/util v0.4.1 // indirect
go.mongodb.org/mongo-driver v1.11.3 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/term v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.20.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/qr v0.2.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading

0 comments on commit 279da25

Please sign in to comment.