Browse Source

add capabilities to restore client session at server startup

pull/4/head
Dimas Restu H 4 years ago
parent
commit
e663d5f10b
  1. 31
      internal/startup.go
  2. 4
      internal/whatsapp/whatsapp.go
  3. 26
      pkg/whatsapp/whatsapp.go

31
internal/startup.go

@ -1,5 +1,36 @@
package internal package internal
import (
"path"
"path/filepath"
"strings"
"github.com/dimaskiddo/go-whatsapp-multidevice-rest/pkg/log"
pkgWhatsApp "github.com/dimaskiddo/go-whatsapp-multidevice-rest/pkg/whatsapp"
)
func Startup() { func Startup() {
log.Print(nil).Info("Running Startup Tasks")
dbs, err := filepath.Glob("./dbs/*.db")
if err != nil {
log.Print(nil).Error("Error to Get Existing SQLite Database Files")
}
for _, db := range dbs {
jid := strings.TrimSuffix(filepath.Base(db), path.Ext(db))
maskJID := jid[0:len(jid)-4] + "xxxx"
log.Print(nil).Info("Restoring WhatsApp Client for " + maskJID)
err := pkgWhatsApp.WhatAppConnect(jid)
if err != nil {
log.Print(nil).Error(err.Error())
}
err = pkgWhatsApp.WhatsAppReconnect(jid)
if err != nil {
log.Print(nil).Error(err.Error())
}
}
} }

4
internal/whatsapp/whatsapp.go

@ -41,6 +41,10 @@ func Login(c echo.Context) error {
return router.ResponseInternalError(c, err.Error()) return router.ResponseInternalError(c, err.Error())
} }
if qrCodeImage == "WhatsApp Client is Reconnected" {
return router.ResponseSuccess(c, qrCodeImage)
}
var resLogin typWhatsApp.ResponseLogin var resLogin typWhatsApp.ResponseLogin
resLogin.QRCode = qrCodeImage resLogin.QRCode = qrCodeImage
resLogin.Timeout = qrCodeTimeout resLogin.Timeout = qrCodeTimeout

26
pkg/whatsapp/whatsapp.go

@ -116,10 +116,12 @@ func WhatsAppLogin(jid string) (string, int, error) {
} else { } else {
// Device ID is Exist // Device ID is Exist
// Reconnect WebSocket // Reconnect WebSocket
err := WhatsAppClient[jid].Connect()
err := WhatsAppReconnect(jid)
if err != nil { if err != nil {
return "", 0, err return "", 0, err
} }
return "WhatsApp Client is Reconnected", 0, nil
} }
} }
@ -127,6 +129,28 @@ func WhatsAppLogin(jid string) (string, int, error) {
return "", 0, errors.New("WhatsApp Client is not Valid") return "", 0, errors.New("WhatsApp Client is not Valid")
} }
func WhatsAppReconnect(jid string) error {
if WhatsAppClient[jid] != nil {
// Make Sure WebSocket Connection is Disconnected
WhatsAppClient[jid].Disconnect()
// Make Sure Store ID is not Empty
// To do Reconnection
if WhatsAppClient[jid].Store.ID != nil {
err := WhatsAppClient[jid].Connect()
if err != nil {
return err
}
return nil
}
return errors.New("WhatsApp Client Store ID is Empty, Please Re-Login and Scan QR Code Again")
}
return errors.New("WhatsApp Client is not Valid")
}
func WhatsAppLogout(jid string) error { func WhatsAppLogout(jid string) error {
if WhatsAppClient[jid] != nil { if WhatsAppClient[jid] != nil {
// Logout WhatsApp Client and Disconnect from WebSocket // Logout WhatsApp Client and Disconnect from WebSocket

Loading…
Cancel
Save