-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdates_parser.go
97 lines (84 loc) · 2.79 KB
/
updates_parser.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
package botmeans
import (
"github.com/go-telegram-bot-api/telegram-bot-api"
"sync"
)
//ChatIdentifier defines something that knows which chat it belongs to
type ChatIdentifier interface {
ChatId() int64
}
//SessionFactory creates the session from given session base
type SessionFactory func(base SessionBase) (SessionInterface, error)
//ActionExecuterFactory creates Executers from given session, cmd, args and source message
type ActionExecuterFactory func(
SessionBase,
SessionFactory,
actionExecuterFactoryConfig,
chan Executer,
)
//BotMessageFactory loads bot message from given chat id, msg id and callback id
type BotMessageFactory func(int64, int64, string) BotMessageInterface
//CmdParserFunc returns command for the given update
type CmdParserFunc func(tgbotapi.Update) string
//ArgsParserFunc returns args of command for the given update
type ArgsParserFunc func(tgbotapi.Update) Args
type parserConfig struct {
sessionFactory SessionFactory
actionExecuterFactory ActionExecuterFactory
botMessageFactory BotMessageFactory
cmdParser CmdParserFunc
argsParser ArgsParserFunc
}
func createTGUpdatesParser(
tgUpdateChan <-chan tgbotapi.Update,
pC parserConfig,
) chan Executer {
cmdQueueChan := make(chan Executer)
go func() {
wg := sync.WaitGroup{}
for tgUpdate := range tgUpdateChan {
wg.Add(1)
go func() {
var chatId, userId int64
var username string
var msg *tgbotapi.Message
var msgId int64
var callbackID string
switch {
case tgUpdate.Message != nil:
chatId = tgUpdate.Message.Chat.ID
userId = int64(tgUpdate.Message.From.ID)
username = tgUpdate.Message.From.UserName
msg = tgUpdate.Message
case tgUpdate.CallbackQuery != nil:
chatId = tgUpdate.CallbackQuery.Message.Chat.ID
userId = int64(tgUpdate.CallbackQuery.From.ID)
username = tgUpdate.CallbackQuery.From.UserName
msg = tgUpdate.CallbackQuery.Message
msgId = int64(msg.MessageID)
callbackID = tgUpdate.CallbackQuery.ID
case tgUpdate.EditedMessage != nil:
chatId = tgUpdate.EditedMessage.Chat.ID
userId = int64(tgUpdate.EditedMessage.From.ID)
username = tgUpdate.EditedMessage.From.UserName
msg = tgUpdate.EditedMessage
case tgUpdate.InlineQuery != nil:
case tgUpdate.ChosenInlineResult != nil:
}
pC.actionExecuterFactory(
SessionBase{TelegramUserID: userId, TelegramUserName: username, TelegramChatID: chatId},
pC.sessionFactory,
actionExecuterFactoryConfig{
func() string { return pC.cmdParser(tgUpdate) },
func() Args { return pC.argsParser(tgUpdate) },
func() BotMessageInterface { return pC.botMessageFactory(chatId, msgId, callbackID) },
},
cmdQueueChan)
wg.Done()
}()
}
wg.Wait()
close(cmdQueueChan)
}()
return cmdQueueChan
}