-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram.go
More file actions
196 lines (170 loc) · 4.38 KB
/
telegram.go
File metadata and controls
196 lines (170 loc) · 4.38 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package telegram
import (
"context"
"fmt"
"strconv"
"strings"
"sync"
"github.com/FrameworkOSS/event"
commands "github.com/FrameworkOSS/feature_commands"
debugger "github.com/FrameworkOSS/feature_debugger"
"github.com/FrameworkOSS/feature_telegram/metadata"
tg "github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
)
type Telegram struct {
token string
bot *tg.Bot
ctx context.Context
lockResp sync.Mutex
resps []*event.Event
c *commands.Commands
}
func (t *Telegram) handlerPortal(e *event.Event) error {
fmt.Printf("%s\n", debugger.DebugEvent(e))
if channel := e.GetChannel(); channel != "" {
ptr := strings.Split(channel, ":")
if len(ptr) == 2 && ptr[0] == t.ID() {
chatID := ptr[1]
t.bot.SendMessage(t.ctx, &tg.SendMessageParams{
ChatID: chatID,
Text: string(e.GetData()),
ParseMode: models.ParseModeHTML,
})
}
}
return nil
}
func (t *Telegram) handlerTelegram(ctx context.Context, b *tg.Bot, update *models.Update) {
if update != nil {
if msg := update.Message; msg != nil {
if msg.From != nil && msg.Text != "" {
if msg.From.IsBot {
return
}
text := msg.Text
if text[0] == '/' {
text = text[1:]
}
channel := fmt.Sprintf("%s:%d", t.ID(), msg.Chat.ID)
e, err := t.c.NewCommandLineEvent(t.ID(), strings.Split(text, " ")...)
if err != nil {
go t.storeResp(event.NewEventError(t.ID(), err).SetChannel(channel))
return
}
e.SetChannel(channel)
go t.storeResp(e)
fmt.Printf("@%s: %s\n", msg.From.Username, msg.Text)
return
}
}
if inline := update.InlineQuery; inline != nil {
if inline.From != nil && inline.Query != "" {
q := inline.Query
fmt.Printf("@%s: %s\n", inline.From.Username, q)
if q[0] == '/' {
q = q[1:]
}
matches := make([]string, 0)
for _, cmd := range t.c.Commands() {
if q == cmd[:len(q)] {
matches = append(matches, cmd)
}
}
results := make([]models.InlineQueryResult, len(matches))
for i := 0; i < len(matches); i++ {
cmd := t.c.Command(matches[i])
results[i] = &models.InlineQueryResultArticle{
ID: strconv.Itoa(i + 1),
Title: cmd.GetID(),
Description: cmd.GetAbout(),
InputMessageContent: &models.InputTextMessageContent{
MessageText: cmd.GetUsage(),
},
}
}
b.AnswerInlineQuery(ctx, &tg.AnswerInlineQueryParams{
InlineQueryID: inline.ID,
Results: results,
})
return
}
}
}
}
func NewTelegram(token string, c *commands.Commands) *Telegram {
t := new(Telegram)
t.token = token
t.resps = make([]*event.Event, 0)
t.c = c
return t
}
func (t *Telegram) API() int {
return metadata.API
}
func (t *Telegram) ID() string {
return metadata.ID
}
func (t *Telegram) Name() string {
return metadata.Name
}
func (t *Telegram) Authors() []string {
return strings.Split(metadata.Authors, ",")
}
func (t *Telegram) Description() string {
return metadata.Description
}
func (t *Telegram) Version() string {
return metadata.Version
}
func (t *Telegram) Open() (err error) {
opts := []tg.Option{
tg.WithDefaultHandler(t.handlerTelegram),
}
bot, err := tg.New(t.token, opts...)
if err != nil {
return err
}
ctx := context.Background()
go bot.Start(ctx)
t.bot = bot
t.ctx = ctx
t.storeResp(event.NewEventReady(t.ID(), true))
return
}
func (t *Telegram) Close() (errs []error, retry bool) {
if t.bot == nil {
return []error{fmt.Errorf("telegram: no open bot to close")}, false
}
fail, err := t.bot.Close(t.ctx)
if err != nil {
return []error{fmt.Errorf("telegram: failed to close: %v", err)}, true
}
if fail {
return []error{fmt.Errorf("telegram: got false when suggesting to close")}, true
}
return
}
func (t *Telegram) Input(e *event.Event) error {
go t.handlerPortal(e)
return nil
}
func (t *Telegram) Output() (e *event.Event, err error) {
e = t.readResp()
return
}
func (t *Telegram) storeResp(e *event.Event) {
e.SetProducer(t.ID())
t.lockResp.Lock()
defer t.lockResp.Unlock()
t.resps = append(t.resps, e)
}
func (t *Telegram) readResp() (e *event.Event) {
t.lockResp.Lock()
defer t.lockResp.Unlock()
if len(t.resps) > 0 {
e = t.resps[0]
t.resps = t.resps[1:]
}
return
}