forked from asternic/wuzapi
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworker.go
157 lines (148 loc) · 4.07 KB
/
worker.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"database/sql"
"fmt"
"github.com/spf13/viper"
"math/rand"
"net/http"
"os"
"strings"
"time"
"wuzapi/model"
"wuzapi/service/Chatwoot"
)
type deliveries struct {
id int
sid string
rel_id int
holder_id int
tag_id int
contact_id int
parent_id int
jid string
message_type string
message_id string
content string
attachment string
attachment_name string
token string
delivery_status string
read_status int
priority int
attempt int
is_schedule int
is_group int
commit_tag int
payload string
sent_after_unix int
send_after string
read_at string
created_at string
updated_at string
}
func worker() {
fmt.Println("Worker started " + time.Now().Format("2006-01-02 15:04:05"))
db, err := sql.Open("sqlite", "./dbdata/deliveries.db")
if err != nil {
log.Fatal().Err(err).Msg("Could not open/create ./dbdata/deliveries.db")
os.Exit(1)
}
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Error().Msg(err.Error())
}
}(db)
for {
min := 1
max := 5
randSend := rand.Intn(max-min) + min
fmt.Println()
rows, err := db.Query("SELECT id, jid, message_id, message_type, content, attachment, attachment_name, is_group FROM deliveries WHERE delivery_status = 'pending' ORDER BY priority")
if err != nil {
return
}
defer func(rows *sql.Rows) {
err := rows.Close()
if err != nil {
log.Error().Msg(err.Error())
}
}(rows)
var result []deliveries
for rows.Next() {
var each = deliveries{}
var err = rows.Scan(&each.id, &each.jid, &each.message_id, &each.message_type, &each.content, &each.attachment, &each.attachment_name, &each.is_group)
if err != nil {
fmt.Println(err.Error())
return
}
result = append(result, each)
}
if err = rows.Err(); err != nil {
fmt.Println(err.Error())
return
}
now := time.Now().Format("2006-01-02 15:04:05")
if len(result) == 0 {
fmt.Println("No pending message " + now)
} else {
fmt.Println("Pending message: ", len(result), now)
i := 0
switch result[i].message_type {
case "text":
time.Sleep(time.Duration(randSend) * time.Second)
kode, respond := SendMessageProcess(result[i].jid, result[i].message_id, result[i].content, 1, nil, nil)
if viper.GetString("chatwoot.baseUrl") != "" && kode == http.StatusOK {
isGroup := strings.Contains(result[i].jid, "@g.us")
chat := result[i].jid
if isGroup {
chat = strings.Split(chat, "@")[0]
chat = chat[3:]
}
var oneSenderWebhook model.OneSenderWebhook
oneSenderWebhook.Chat = chat
oneSenderWebhook.SenderPhone = chat
oneSenderWebhook.MessageText = result[i].content
oneSenderWebhook.MessageType = "text"
oneSenderWebhook.IsFromMe = true
oneSenderWebhook.IsGroup = isGroup
oneSenderWebhook.MessageID = result[i].message_id
Chatwoot.IncomingMessageApi(oneSenderWebhook)
}
if kode == 200 {
_, err = db.Exec("UPDATE deliveries SET delivery_status=?, updated_at=? WHERE id=?", "sent", now, result[i].id)
if err != nil {
fmt.Println(err.Error())
return
}
log.Info().Msg("Message sent to " + result[i].jid)
fmt.Println(respond)
}
}
}
time.Sleep(1 * time.Second)
}
}
func receipt(id string, status string, timestamp time.Time) {
fmt.Println("Receipt: ", id, status, timestamp)
db, err := sql.Open("sqlite", "./dbdata/deliveries.db")
if err != nil {
log.Fatal().Err(err).Msg("Could not open/create ./dbdata/deliveries.db")
os.Exit(1)
}
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Error().Msg(err.Error())
}
}(db)
now := time.Now().Format("2006-01-02 15:04:05")
switch status {
case "delivered":
_, _ = db.Exec("UPDATE deliveries SET delivery_status=?, delivered_at=?, updated_at=? WHERE message_id=?", status, now, now, id)
break
case "read":
_, _ = db.Exec("UPDATE deliveries SET delivery_status=?, read_at=?, updated_at=? WHERE message_id=?", status, now, now, id)
break
}
}