From 2e2fc6a77ba3429ca9d7c4dc93a082a8034c301b Mon Sep 17 00:00:00 2001 From: Keyur Shah Date: Sat, 20 Apr 2024 23:38:18 +0530 Subject: [PATCH] THIS IS DONE --- apis/append.token.go | 26 ++++++++++++++++++++++++++ apis/index.go | 7 ++++--- apis/start.number.go | 29 +++++++++++++++++++++++++++++ main.go | 4 ++-- whatsapp/index.go | 11 ++++++----- whatsapp/interfaces.go | 5 ++++- 6 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 apis/append.token.go create mode 100644 apis/start.number.go diff --git a/apis/append.token.go b/apis/append.token.go new file mode 100644 index 0000000..20be522 --- /dev/null +++ b/apis/append.token.go @@ -0,0 +1,26 @@ +package apis + +import ( + "github.com/gofiber/fiber/v2" + "github.com/rpsoftech/whatsapp-http-api/env" +) + +func AppendTokenInConfigJSON(c *fiber.Ctx) error { + token := c.Query("token") + if token == "" { + return c.JSON(fiber.Map{ + "success": false, + }) + } + // check token exist in config + if _, ok := env.ServerConfig.Tokens[token]; ok { + return c.JSON(fiber.Map{ + "success": false, + }) + } + env.ServerConfig.Tokens[token] = "" + env.ServerConfig.Save() + return c.JSON(fiber.Map{ + "success": true, + }) +} diff --git a/apis/index.go b/apis/index.go index 2159937..95902ce 100644 --- a/apis/index.go +++ b/apis/index.go @@ -29,6 +29,7 @@ type ( func AddApis(app fiber.Router) { app.Get("/qr_code", GetQrCode) + app.Post("/start", StartNumber) // app.Get("/qr_scan", QrScan) { authenticated := app.Group("", middleware.AllowOnlyValidLoggedInWhatsapp) @@ -167,7 +168,7 @@ func SendMessage(c *fiber.Ctx) error { Name: "ERROR_INVALID_INPUT", } } - number, err := interfaces.ExtractNumberFromCtx(c) + token, err := interfaces.ExtractNumberFromCtx(c) if err != nil { return err } @@ -179,12 +180,12 @@ func SendMessage(c *fiber.Ctx) error { Name: "ERROR_INVALID_INPUT", } } - connection, ok := whatsapp.ConnectionMap[number] + connection, ok := whatsapp.ConnectionMap[token] if !ok || connection == nil { return &interfaces.RequestError{ StatusCode: http.StatusNotFound, Code: interfaces.ERROR_CONNECTION_NOT_FOUND, - Message: fmt.Sprintf("Number %s Not Found", number), + Message: fmt.Sprintf("Number %s Not Found", token), Name: "ERROR_CONNECTION_NOT_FOUND", } } diff --git a/apis/start.number.go b/apis/start.number.go new file mode 100644 index 0000000..66d4add --- /dev/null +++ b/apis/start.number.go @@ -0,0 +1,29 @@ +package apis + +import ( + "fmt" + + "github.com/gofiber/fiber/v2" + "github.com/rpsoftech/whatsapp-http-api/env" + "github.com/rpsoftech/whatsapp-http-api/interfaces" + "github.com/rpsoftech/whatsapp-http-api/whatsapp" +) + +func StartNumber(c *fiber.Ctx) error { + token, err := interfaces.ExtractNumberFromCtx(c) + if err != nil { + return err + } + _, ok := whatsapp.ConnectionMap[token] + if ok { + return c.JSON(fiber.Map{ + "success": false, + "reason": fmt.Sprintf("Number %s is already connected", token), + }) + } + jidString := env.ServerConfig.JID[token] + whatsapp.ConnectToNumber(jidString, token) + return c.JSON(fiber.Map{ + "success": true, + }) +} diff --git a/main.go b/main.go index 361a697..3a19336 100644 --- a/main.go +++ b/main.go @@ -42,9 +42,9 @@ func main() { whatsapp.InitSqlContainer() if env.Env.AUTO_CONNECT_TO_WHATSAPP { go func() { - for k, v := range env.ServerConfig.Tokens { + for k := range env.ServerConfig.Tokens { jidString := env.ServerConfig.JID[k] - whatsapp.ConnectToNumber(v, jidString, k) + whatsapp.ConnectToNumber(jidString, k) } }() } diff --git a/whatsapp/index.go b/whatsapp/index.go index 47c6de8..f0048c9 100644 --- a/whatsapp/index.go +++ b/whatsapp/index.go @@ -23,7 +23,7 @@ func InitSqlContainer() { } } -func ConnectToNumber(number string, jidString string, token string) { +func ConnectToNumber(jidString string, token string) { // SqlContainer.PutDevice() if deviceStores, _ := SqlContainer.GetAllDevices(); true { for _, deviceStore := range deviceStores { @@ -33,13 +33,10 @@ func ConnectToNumber(number string, jidString string, token string) { var JID types.JID if jidString != "" { JID, _ = types.ParseJID(jidString) - } else if number != "" { - JID = types.NewJID(number, types.DefaultUserServer) } var deviceStore *store.Device if !JID.IsEmpty() { var err error - deviceStore, err = SqlContainer.GetDevice(JID) if err != nil { println(err.Error()) @@ -51,12 +48,16 @@ func ConnectToNumber(number string, jidString string, token string) { } clientLog := waLog.Stdout("Client", "ERROR", true) client := whatsmeow.NewClient(deviceStore, clientLog) + client.EnableAutoReconnect = true + // client. + println(client.LastSuccessfulConnect.String()) + // client.MessengerConfig = &whatsmeow.MessengerConfig{ // UserAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36", // BaseURL: "https://web.whatsapp.com", // } // client.PairPhone() - connection := &WhatsappConnection{Client: client, Number: number, ConnectionStatus: 0, SyncFinished: false, Token: token} + connection := &WhatsappConnection{Client: client, ConnectionStatus: 0, SyncFinished: false, Token: token} ConnectionMap[token] = connection client.AddEventHandler(connection.eventHandler) diff --git a/whatsapp/interfaces.go b/whatsapp/interfaces.go index 7700b64..f299c25 100644 --- a/whatsapp/interfaces.go +++ b/whatsapp/interfaces.go @@ -95,12 +95,15 @@ func (connection *WhatsappConnection) eventHandler(evt interface{}) { switch v := evt.(type) { case *events.LoggedOut: // Send Status - connection.ConnectionStatus = -1 + // connection.ConnectionStatus = -1 connection.Client.Logout() connection.Client.Store.Delete() println(connection.Number, " Logged Out") connection.Client.Disconnect() delete(ConnectionMap, connection.Token) + env.ServerConfig.Tokens[connection.Token] = "" + delete(env.ServerConfig.JID, connection.Token) + env.ServerConfig.Save() go connection.ConnectAndGetQRCode() case *events.Connected: // Send Status