This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotmsg.go
126 lines (107 loc) · 3.26 KB
/
botmsg.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package wabot
import (
"bytes"
"io"
"io/ioutil"
"os"
"strings"
"github.com/Rhymen/go-whatsapp"
)
// Command struct
type Command struct {
prefix string
groups []string
function func(whatsapp.TextMessage)
}
// List of implemented commands
var commands []Command
var imageHandleFunction func(whatsapp.ImageMessage)
var stickerHandleFunction func(whatsapp.StickerMessage)
var defaultTextHandleFunction func(whatsapp.TextMessage)
// handleBotMsg checks if a message is a command and executes the first possible command
func handleBotMsg(message whatsapp.TextMessage) {
// fmt.Printf("Received: %s\n - from Group:%s\n", message.Text, fmt.Sprintf("%s-%s", GetPhoneNumber(), MessageToGroupID(message)))
// Run through command list and execute if possible
for i := 0; i < len(commands); i++ {
// if no groups set || group is set && command is right
// if (len(commands[i].groups) == 0 || arrayContains(commands[i].groups, JidToGroupName(message.Info.RemoteJid))) && strings.HasPrefix(strings.Split(strings.ToLower(message.Text), " ")[0], strings.ToLower(commands[i].prefix)) { // Group names
if (len(commands[i].groups) == 0 || arrayContains(commands[i].groups, JidToGroupID(message.Info.RemoteJid))) && strings.HasPrefix(strings.Split(strings.ToLower(message.Text), " ")[0], strings.ToLower(commands[i].prefix)) { // Group Jid
go commands[i].function(message)
break
}
}
// Predefined function that can be turned off
if useNicknames && strings.HasPrefix(strings.ToLower(message.Text), "/nick") && len(message.Text) > 7 {
SetUserNickname(MessageToJid(message), message.Text[6:])
if len(nicknameUpdateText) > 0 {
WriteTextMessage(nicknameUpdateText, message.Info.RemoteJid)
}
}
// No command found? Try to run the default code
go defaultTextHandleFunction(message)
}
func arrayContains(array []string, group string) bool {
for _, a := range array {
if a == group {
return true
}
}
return false
}
// WriteTextMessage sends a given string as text
func WriteTextMessage(text string, remoteJid string) {
// Create the msg struct
msg := whatsapp.TextMessage{
Info: whatsapp.MessageInfo{
RemoteJid: remoteJid,
},
Text: text,
}
// And send it
conn.Send(msg)
}
// SendImageMessage takes img type like "image/png" / If caption len is 0 there won't be a text
func SendImageMessage(caption string, img io.Reader, imgType string, remoteJid string) {
var msg whatsapp.ImageMessage
// Create a Thumbnail
var buffer bytes.Buffer
tee := io.TeeReader(img, &buffer)
thumbnail, _ := ioutil.ReadAll(&buffer)
if len(caption) > 0 {
// Create the struct
msg = whatsapp.ImageMessage{
Info: whatsapp.MessageInfo{
RemoteJid: remoteJid,
},
Caption: caption,
Type: imgType,
Content: tee,
Thumbnail: thumbnail,
}
} else {
// Create the struct
msg = whatsapp.ImageMessage{
Info: whatsapp.MessageInfo{
RemoteJid: remoteJid,
},
Type: imgType,
Content: tee,
Thumbnail: thumbnail,
}
}
// And send it
conn.Send(msg)
}
// SendStickerMessage only uses .webp files
func SendStickerMessage(img *os.File, remoteJid string) {
// Create the struct
msg := whatsapp.StickerMessage{
Info: whatsapp.MessageInfo{
RemoteJid: remoteJid,
},
Type: "image/webp",
Content: img,
}
// And send it
conn.Send(msg)
}