Skip to content

Commit

Permalink
THIS IS DONE
Browse files Browse the repository at this point in the history
  • Loading branch information
keyurboss committed Apr 20, 2024
1 parent 8bd67a2 commit 2e2fc6a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 11 deletions.
26 changes: 26 additions & 0 deletions apis/append.token.go
Original file line number Diff line number Diff line change
@@ -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,
})
}
7 changes: 4 additions & 3 deletions apis/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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",
}
}
Expand Down
29 changes: 29 additions & 0 deletions apis/start.number.go
Original file line number Diff line number Diff line change
@@ -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,
})
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}()
}
Expand Down
11 changes: 6 additions & 5 deletions whatsapp/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())
Expand All @@ -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)

Expand Down
5 changes: 4 additions & 1 deletion whatsapp/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2e2fc6a

Please sign in to comment.