Skip to content

Commit

Permalink
feat: add webhook in group chat
Browse files Browse the repository at this point in the history
  • Loading branch information
aldinokemal committed Jul 23, 2023
1 parent b15afdc commit bd71730
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&config.AppOs, "os", "", config.AppOs, `os name --os <string> | example: --os="Chrome"`)
rootCmd.PersistentFlags().StringVarP(&config.AppBasicAuthCredential, "basic-auth", "b", config.AppBasicAuthCredential, "basic auth credential | -b=yourUsername:yourPassword")
rootCmd.PersistentFlags().StringVarP(&config.WhatsappAutoReplyMessage, "autoreply", "", config.WhatsappAutoReplyMessage, `auto reply when received message --autoreply <string> | example: --autoreply="Don't reply this message"`)
rootCmd.PersistentFlags().StringVarP(&config.WhatsappAutoReplyWebhook, "webhook", "w", config.WhatsappAutoReplyMessage, `auto reply when received message --webhook <string> | example: --webhook="https://yourcallback.com/callback"`)
rootCmd.PersistentFlags().StringVarP(&config.WhatsappWebhook, "webhook", "w", config.WhatsappWebhook, `forward event to webhook --webhook <string> | example: --webhook="https://yourcallback.com/callback"`)
}

func runRest(_ *cobra.Command, _ []string) {
Expand Down
2 changes: 1 addition & 1 deletion src/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (

WhatsappLogLevel = "ERROR"
WhatsappAutoReplyMessage string
WhatsappAutoReplyWebhook string
WhatsappWebhook string
WhatsappSettingMaxFileSize int64 = 50000000 // 50MB
WhatsappSettingMaxVideoSize int64 = 100000000 // 100MB
)
46 changes: 35 additions & 11 deletions src/pkg/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"mime"
"net/http"
"os"
"regexp"
"strings"
"sync/atomic"
"time"
Expand Down Expand Up @@ -208,15 +209,17 @@ func handler(rawEvt interface{}) {
}
}

if !isGroupJid(evt.Info.Chat.String()) && !strings.Contains(evt.Info.SourceString(), "broadcast") {
if config.WhatsappAutoReplyMessage != "" {
_, _ = cli.SendMessage(context.Background(), evt.Info.Sender, &waProto.Message{Conversation: proto.String(config.WhatsappAutoReplyMessage)})
}
if config.WhatsappAutoReplyMessage != "" &&
!isGroupJid(evt.Info.Chat.String()) &&
!strings.Contains(evt.Info.SourceString(), "broadcast") {
_, _ = cli.SendMessage(context.Background(), evt.Info.Sender, &waProto.Message{Conversation: proto.String(config.WhatsappAutoReplyMessage)})
}

if config.WhatsappAutoReplyWebhook != "" {
if err := sendAutoReplyWebhook(evt); err != nil {
logrus.Error("Failed to send webhoook", err)
}
if config.WhatsappWebhook != "" &&
!strings.Contains(evt.Info.SourceString(), "broadcast") &&
!isFromMySelf(evt.Info.SourceString()) {
if err := forwardToWebhook(evt); err != nil {
logrus.Error("Failed forward to webhook", err)
}
}
case *events.Receipt:
Expand Down Expand Up @@ -257,8 +260,9 @@ func handler(rawEvt interface{}) {
}
}

func sendAutoReplyWebhook(evt *events.Message) error {
logrus.Info("Sending webhook to", config.WhatsappAutoReplyWebhook)
// forwardToWebhook is a helper function to forward event to webhook url
func forwardToWebhook(evt *events.Message) error {
logrus.Info("Forwarding event to webhook:", config.WhatsappWebhook)
client := &http.Client{Timeout: 10 * time.Second}
imageMedia := evt.Message.GetImageMessage()
stickerMedia := evt.Message.GetStickerMessage()
Expand Down Expand Up @@ -352,7 +356,7 @@ func sendAutoReplyWebhook(evt *events.Message) error {
return pkgError.WebhookError(fmt.Sprintf("Failed to marshal body: %v", err))
}

req, err := http.NewRequest(http.MethodPost, config.WhatsappAutoReplyWebhook, bytes.NewBuffer(postBody))
req, err := http.NewRequest(http.MethodPost, config.WhatsappWebhook, bytes.NewBuffer(postBody))
if err != nil {
return pkgError.WebhookError(fmt.Sprintf("error when create http object %v", err))
}
Expand All @@ -363,10 +367,30 @@ func sendAutoReplyWebhook(evt *events.Message) error {
return nil
}

// isGroupJid is a helper function to check if the message is from group
func isGroupJid(jid string) bool {
return strings.Contains(jid, "@g.us")
}

// isFromMySelf is a helper function to check if the message is from my self (logged in account)
func isFromMySelf(jid string) bool {
return extractPhoneNumber(jid) == extractPhoneNumber(cli.Store.ID.String())
}

// extractPhoneNumber is a helper function to extract the phone number from a JID
func extractPhoneNumber(jid string) string {
regex := regexp.MustCompile(`\d+`)
// Find all matches of the pattern in the JID
matches := regex.FindAllString(jid, -1)
// The first match should be the phone number
if len(matches) > 0 {
return matches[0]
}
// If no matches are found, return an empty string
return ""
}

// DownloadMedia is a helper function to download media from whatsapp
func DownloadMedia(storageLocation string, mediaFile whatsmeow.DownloadableMessage) (path string, err error) {
if mediaFile == nil {
logrus.Info("Skip download because data is nil")
Expand Down

0 comments on commit bd71730

Please sign in to comment.