-
Notifications
You must be signed in to change notification settings - Fork 0
/
referance.go
107 lines (96 loc) · 2.53 KB
/
referance.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
package filter
import (
"context"
"fmt"
"log"
"strings"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/rachmanzz/nextjob/modules/textparse"
"github.com/rachmanzz/nextjob/setup"
"github.com/rachmanzz/nextjob/types"
)
var ctx = context.Background()
var filterKeyboard = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("next", "--filter-open"),
tgbotapi.NewInlineKeyboardButtonData("finish", "--filter-close"),
),
)
func FilterReferance(ref string, botMsg *tgbotapi.Message) {
var bot = setup.BOT
var redis = setup.REDIS
msgConfig := tgbotapi.NewMessage(botMsg.Chat.ID, "")
keyID := fmt.Sprintf("filter_%d", botMsg.From.ID)
inputfilter := types.FilterRefObject{}
hasChange := false
if val, err := redis.Get(ctx, keyID).Result(); err == nil {
inputfilter.ToObject(val)
}
if ref == "location" {
hasChange = true
locations := strings.Split(botMsg.Text, ";")
inputfilter.Locations = append(inputfilter.Locations, locations...)
}
if ref == "type" {
hasChange = true
inputfilter.Type = strings.TrimSpace(botMsg.Text)
}
if ref == "currency" {
hasChange = true
inputfilter.Currency = strings.TrimSpace(botMsg.Text)
}
if ref == "salary" {
hasChange = true
salaryArr := strings.Split(botMsg.Text, "-")
if len(salaryArr) >= 1 {
inputfilter.SalaryFrom = salaryArr[0]
if len(salaryArr) == 2 {
inputfilter.SalaryTo = salaryArr[1]
}
}
}
if ref == "inline-arg" {
_, params := textparse.QueryParse(botMsg.Text)
if len(*params) == 0 {
msgConfig.Text = "invalid argument..."
msgConfig.ReplyMarkup = filterKeyboard
if _, err := bot.Send(msgConfig); err != nil {
log.Println(err.Error())
}
} else {
hasChange = true
}
for _, items := range *params {
for key, item := range items {
if key == "location[]" {
inputfilter.Locations = append(inputfilter.Locations, item)
}
if key == "type" {
inputfilter.Type = item
}
if key == "currency" {
inputfilter.Currency = item
}
if key == "salaryFrom" {
inputfilter.SalaryFrom = item
}
if key == "salaryTo" {
inputfilter.SalaryTo = item
}
}
}
}
if val, err := inputfilter.ToJSON(); err == nil && hasChange {
err := redis.Set(ctx, keyID, val, 8*time.Hour).Err()
if err != nil {
log.Println(err.Error())
} else {
msgConfig.Text = "set next filter or close ?"
msgConfig.ReplyMarkup = filterKeyboard
if _, err := bot.Send(msgConfig); err != nil {
log.Println(err.Error())
}
}
}
}