Browse Source

feat: add new webhook endpoint (#25)

* feat: add new webhook endpoint

* chore: upgrade version

* chore: update docs

* feat: update variable name become webhook

* feat: remove unused struct
pull/26/head v3.6.0
Aldino Kemal 3 years ago
committed by GitHub
parent
commit
325a5110f3
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      readme.md
  2. 1
      src/cmd/root.go
  3. 3
      src/config/settings.go
  4. 42
      src/utils/whatsapp.go

2
readme.md

@ -13,6 +13,8 @@
- `--debug true` - `--debug true`
- Auto reply message - Auto reply message
- `--autoreply="Don't reply this message"` - `--autoreply="Don't reply this message"`
- Weebhook for receive message
- --webhook="http://yourwebhook.site/handler"
### Required (without docker) ### Required (without docker)

1
src/cmd/root.go

@ -37,6 +37,7 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&config.AppDebug, "debug", "d", config.AppDebug, "hide or displaying log with --debug <true/false> | example: --debug=true") rootCmd.PersistentFlags().BoolVarP(&config.AppDebug, "debug", "d", config.AppDebug, "hide or displaying log with --debug <true/false> | example: --debug=true")
rootCmd.PersistentFlags().StringVarP(&config.AppBasicAuthCredential, "basic-auth", "b", config.AppBasicAuthCredential, "basic auth credential | yourUsername:yourPassword") rootCmd.PersistentFlags().StringVarP(&config.AppBasicAuthCredential, "basic-auth", "b", config.AppBasicAuthCredential, "basic auth credential | 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.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"`)
} }
func runRest(cmd *cobra.Command, args []string) { func runRest(cmd *cobra.Command, args []string) {

3
src/config/settings.go

@ -8,7 +8,7 @@ import (
type Browser string type Browser string
var ( var (
AppVersion string = "v3.5.1"
AppVersion string = "v3.6.0"
AppPort string = "3000" AppPort string = "3000"
AppDebug bool = false AppDebug bool = false
AppOs string = fmt.Sprintf("AldinoKemal") AppOs string = fmt.Sprintf("AldinoKemal")
@ -22,6 +22,7 @@ var (
WhatsappLogLevel string = "ERROR" WhatsappLogLevel string = "ERROR"
WhatsappAutoReplyMessage string WhatsappAutoReplyMessage string
WhatsappAutoReplyWebhook string
WhatsappSettingMaxFileSize int64 = 30000000 // 10MB WhatsappSettingMaxFileSize int64 = 30000000 // 10MB
WhatsappSettingMaxVideoSize int64 = 30000000 // 30MB WhatsappSettingMaxVideoSize int64 = 30000000 // 30MB
) )

42
src/utils/whatsapp.go

@ -1,6 +1,7 @@
package utils package utils
import ( import (
"bytes"
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -15,6 +16,7 @@ import (
waLog "go.mau.fi/whatsmeow/util/log" waLog "go.mau.fi/whatsmeow/util/log"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"mime" "mime"
"net/http"
"os" "os"
"strings" "strings"
"sync/atomic" "sync/atomic"
@ -181,6 +183,12 @@ func handler(rawEvt interface{}) {
if config.WhatsappAutoReplyMessage != "" { if config.WhatsappAutoReplyMessage != "" {
_, _ = cli.SendMessage(context.Background(), evt.Info.Sender, "", &waProto.Message{Conversation: proto.String(config.WhatsappAutoReplyMessage)}) _, _ = cli.SendMessage(context.Background(), evt.Info.Sender, "", &waProto.Message{Conversation: proto.String(config.WhatsappAutoReplyMessage)})
} }
if config.WhatsappAutoReplyWebhook != "" {
go func() {
_ = sendAutoReplyWebhook(evt)
}()
}
case *events.Receipt: case *events.Receipt:
if evt.Type == events.ReceiptTypeRead || evt.Type == events.ReceiptTypeReadSelf { if evt.Type == events.ReceiptTypeRead || evt.Type == events.ReceiptTypeReadSelf {
log.Infof("%v was read by %s at %s", evt.MessageIDs, evt.SourceString(), evt.Timestamp) log.Infof("%v was read by %s at %s", evt.MessageIDs, evt.SourceString(), evt.Timestamp)
@ -218,3 +226,37 @@ func handler(rawEvt interface{}) {
log.Debugf("App state event: %+v / %+v", evt.Index, evt.SyncActionValue) log.Debugf("App state event: %+v / %+v", evt.Index, evt.SyncActionValue)
} }
} }
func sendAutoReplyWebhook(evt *events.Message) error {
client := &http.Client{Timeout: 10 * time.Second}
body := map[string]interface{}{
"from": evt.Info.SourceString(),
"message": evt.Message.GetConversation(),
"image": evt.Message.GetImageMessage(),
"video": evt.Message.GetVideoMessage(),
"audio": evt.Message.GetAudioMessage(),
"document": evt.Message.GetDocumentMessage(),
"location": evt.Message.GetLocationMessage(),
"sticker": evt.Message.GetStickerMessage(),
"live_location": evt.Message.GetLiveLocationMessage(),
"view_once": evt.Message.GetViewOnceMessage(),
"list": evt.Message.GetListMessage(),
"order": evt.Message.GetOrderMessage(),
"contact": evt.Message.GetContactMessage(),
"forwarded": evt.Message.GetGroupInviteMessage(),
}
postBody, _ := json.Marshal(body)
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, config.WhatsappAutoReplyWebhook, bytes.NewBuffer(postBody))
if err != nil {
log.Errorf("error when create http object %v", err)
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Errorf("error when submit webhook %v", err)
return err
}
defer resp.Body.Close()
return nil
}
Loading…
Cancel
Save