whatsapp-multi-devicewhatsapp-apiwhatsapprestgolanggobotwhatsapp-web-multi-devicewhatsapp-api-gorest-apigolang-whatsapp-apigolang-whatsapp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.1 KiB
89 lines
2.1 KiB
package rest
|
|
|
|
import (
|
|
"fmt"
|
|
domainApp "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/app"
|
|
"github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/utils"
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
type App struct {
|
|
Service domainApp.IAppService
|
|
}
|
|
|
|
func InitRestApp(app *fiber.App, service domainApp.IAppService) App {
|
|
rest := App{Service: service}
|
|
app.Get("/app/login", rest.Login)
|
|
app.Get("/app/login-with-code", rest.LoginWithCode)
|
|
app.Get("/app/logout", rest.Logout)
|
|
app.Get("/app/reconnect", rest.Reconnect)
|
|
app.Get("/app/devices", rest.Devices)
|
|
|
|
return App{Service: service}
|
|
}
|
|
|
|
func (handler *App) Login(c *fiber.Ctx) error {
|
|
response, err := handler.Service.Login(c.UserContext())
|
|
utils.PanicIfNeeded(err)
|
|
|
|
return c.JSON(utils.ResponseData{
|
|
Status: 200,
|
|
Code: "SUCCESS",
|
|
Message: "Login success",
|
|
Results: map[string]any{
|
|
"qr_link": fmt.Sprintf("%s://%s/%s", c.Protocol(), c.Hostname(), response.ImagePath),
|
|
"qr_duration": response.Duration,
|
|
},
|
|
})
|
|
}
|
|
|
|
func (handler *App) LoginWithCode(c *fiber.Ctx) error {
|
|
loginCode, err := handler.Service.LoginWithCode(c.UserContext(), c.Query("phone"))
|
|
utils.PanicIfNeeded(err)
|
|
|
|
return c.JSON(utils.ResponseData{
|
|
Status: 200,
|
|
Code: "SUCCESS",
|
|
Message: "Login with code success",
|
|
Results: map[string]any{
|
|
"login_code": loginCode,
|
|
},
|
|
})
|
|
|
|
}
|
|
|
|
func (handler *App) Logout(c *fiber.Ctx) error {
|
|
err := handler.Service.Logout(c.UserContext())
|
|
utils.PanicIfNeeded(err)
|
|
|
|
return c.JSON(utils.ResponseData{
|
|
Status: 200,
|
|
Code: "SUCCESS",
|
|
Message: "Success logout",
|
|
Results: nil,
|
|
})
|
|
}
|
|
|
|
func (handler *App) Reconnect(c *fiber.Ctx) error {
|
|
err := handler.Service.Reconnect(c.UserContext())
|
|
utils.PanicIfNeeded(err)
|
|
|
|
return c.JSON(utils.ResponseData{
|
|
Status: 200,
|
|
Code: "SUCCESS",
|
|
Message: "Reconnect success",
|
|
Results: nil,
|
|
})
|
|
}
|
|
|
|
func (handler *App) Devices(c *fiber.Ctx) error {
|
|
devices, err := handler.Service.FetchDevices(c.UserContext())
|
|
utils.PanicIfNeeded(err)
|
|
|
|
return c.JSON(utils.ResponseData{
|
|
Status: 200,
|
|
Code: "SUCCESS",
|
|
Message: "Fetch device success",
|
|
Results: devices,
|
|
})
|
|
}
|