forked from asternic/wuzapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
60 lines (49 loc) · 1.57 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"strconv"
)
func Find(slice []string, val string) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}
// Update entry in User map
func updateUserInfo(values interface{}, field string, value string) interface{} {
log.Debug().Str("field",field).Str("value",value).Msg("User info updated")
values.(Values).m[field] = value
return values
}
// webhook for regular messages
func callHook(myurl string, payload map[string]string, id int) {
log.Info().Str("url",myurl).Msg("Sending POST to client "+strconv.Itoa(id))
// Log the payload map
log.Debug().Msg("Payload:")
for key, value := range payload {
log.Debug().Str(key, value).Msg("")
}
_, err := clientHttp[id].R().SetFormData(payload).Post(myurl)
if err != nil {
log.Debug().Str("error",err.Error())
}
}
// webhook for messages with file attachments
func callHookFile(myurl string, payload map[string]string, id int, file string) error {
log.Info().Str("file", file).Str("url", myurl).Msg("Sending POST")
resp, err := clientHttp[id].R().
SetFiles(map[string]string{
"file": file,
}).
SetFormData(payload).
Post(myurl)
if err != nil {
log.Error().Err(err).Str("url", myurl).Msg("Failed to send POST request")
return fmt.Errorf("failed to send POST request: %w", err)
}
// Optionally, you can log the response status
log.Info().Int("status", resp.StatusCode()).Msg("POST request completed")
return nil
}