-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegom.go
93 lines (70 loc) · 1.78 KB
/
telegom.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
package main
import (
"fmt"
"strings"
"telegom/database"
"telegom/response"
"telegom/api"
)
type HandleTelegom func(ServerResponse, *api.Update)
type TeleGom struct {
handlers map[string]HandleTelegom
}
func InitTeleGom() *TeleGom {
return &TeleGom{
handlers: map[string]HandleTelegom{
"/help": helpDefault,
"/recurseNotFount": recurseNotFountDefault,
},
}
}
func (tg *TeleGom) Handle(command string, s HandleTelegom) {
if tg.handlers == nil {
tg.handlers = make(map[string]HandleTelegom)
}
switch command {
case "/help":
tg.handlers[command] = s
case "/recurseNotFount":
tg.handlers[command] = s
default:
tg.handlers[command] = s
}
}
func (tg *TeleGom) ServeToTelegram(w ServerResponse, r *api.Update) {
MGDB := database.NewMongoClientConversation(mongoToken)
defer MGDB.CancelConection()
mssg := r.Message.Text
// Implement detector of commands
indexOfChater := strings.Index(mssg, "/")
if indexOfChater == 0 {
handler, ok := tg.handlers[mssg]
if ok {
res := &response.Response{
FromID: r.Message.From.ID,
}
handler(res, r)
} else {
res := &response.Response{
FromID: r.Message.From.ID,
}
tg.handlers["/help"](res, r)
}
} else if indexOfChater <= -1 {
ConversationContinued, err := MGDB.FindConversation(r.Message.From.ID)
if err != nil || ConversationContinued == nil {
fmt.Printf("Problema al buscar en base de datos: %s\n", err)
ConversationContinued.Command = "/recurseNotFount"
}
// Implement Follow a Conversation
Hdr, _ := tg.handlers[ConversationContinued.Command]
WT := &response.Response{
FromID: *&r.Message.From.ID,
}
Hdr(WT, r)
} else {
// Si el "/" se encuentra en el mensaje eg.{"Na/Sodio", "Amigo/Friend"}
s, _ := tg.handlers[r.EditedMessage.Text]
s(w, r)
}
}