-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram.go
151 lines (126 loc) · 3.13 KB
/
telegram.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
// Copyright 2022 Kirill Scherba <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Teonet telegram bot module
package main
import (
"errors"
"log"
"sync"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
const (
waitCommand = iota // Wait a command state
waitToken // Wait login token state
)
// Bot receiver
type Bot struct {
*tgbotapi.BotAPI
BotState
teo *Teonet
}
// NewBot create new telegram bot
func NewBot(token string, teo *Teonet) (bot *Bot, err error) {
bot = new(Bot)
b, err := tgbotapi.NewBotAPI(token)
if err != nil {
err = errors.New("can't create telegram bot, err: " + err.Error())
return
}
bot.BotState.m = make(map[string]BotStateData)
bot.BotAPI = b
bot.teo = teo
// b.Debug = true
log.Printf("Authorized on account %s", b.Self.UserName)
return
}
// Run receive and process users messages
func (b *Bot) Run() {
conf := tgbotapi.NewUpdate(0)
conf.Timeout = 30
updates := b.GetUpdatesChan(conf)
// com := &Command{b.teo}
for update := range updates {
// Ignore any non-Message Updates
if update.Message == nil {
continue
}
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
// Commands arguments
// args := update.Message.CommandArguments()
// Send answer to message
sendAnswer := func(update tgbotapi.Update, state int, text string, markdown ...bool) {
// Set new state
b.SetState(update.Message.From.UserName, state)
// Send answer to user
msg := tgbotapi.NewMessage(update.Message.Chat.ID, text)
if len(markdown) > 0 && markdown[0] {
msg.ParseMode = tgbotapi.ModeMarkdownV2
}
// msg.ReplyToMessageID = update.Message.MessageID
b.Send(msg)
}
// Check command
var state int
var answer string
switch update.Message.Command() {
// fortune command
case "fortune":
var err error
answer, err = b.teo.Fortune()
state = waitCommand
if err != nil {
answer = err.Error()
}
// Check State argument, Text or Unknown command
default:
text := update.Message.Text
// Check command state
state = b.State(update.Message.From.UserName)
switch state {
// Set login token
// case waitToken:
// continue
// Text or Unknown command
default:
answer = "I don't know anything about '" + text + "'.\n\n" +
"I can tell you some fortune message:\n\n"
msg, err := b.teo.Fortune()
if err != nil {
msg = err.Error()
}
answer += msg
}
}
// Send answer to user and sent new user state
sendAnswer(update, state, answer)
}
}
// BotState is telegram bot state receiver
type BotState struct {
m map[string]BotStateData
sync.RWMutex
}
// BotStateData is BotState data structure
type BotStateData struct {
state int
}
// SetState save users state
func (s *BotState) SetState(user string, state int) {
s.Lock()
defer s.Unlock()
d := s.m[user]
d.state = state
s.m[user] = d
}
// State return users state
func (s *BotState) State(user string) (state int) {
s.RLock()
defer s.RUnlock()
d, ok := s.m[user]
if !ok {
state = waitCommand
}
state = d.state
return
}