-
Notifications
You must be signed in to change notification settings - Fork 0
/
finch.go
106 lines (82 loc) · 2.34 KB
/
finch.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
// Package finch is a framework for Telegram Bots.
package finch
import (
"net/http"
"strings"
"github.com/getsentry/raven-go"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
var sentryEnabled = false
// Finch is a Telegram Bot, including API, Config, and Commands.
type Finch struct {
API *tgbotapi.BotAPI
Config Config
Commands []*CommandState
Inline InlineCommand
}
// NewFinch returns a new Finch instance, with Telegram API setup.
func NewFinch(token string) *Finch {
return NewFinchWithClient(token, &http.Client{})
}
// NewFinchWithClient returns a new Finch instance,
// using a different net/http Client.
func NewFinchWithClient(token string, client *http.Client) *Finch {
bot := &Finch{}
c, _ := LoadConfig()
bot.Config = *c
if token == "" {
val := bot.Config.Get("token")
if val == nil {
panic("no token provided")
}
token = val.(string)
}
api, err := tgbotapi.NewBotAPIWithClient(token, tgbotapi.APIEndpoint, client)
if err != nil {
panic(err)
}
bot.API = api
bot.Commands = commands
bot.Inline = inline
return bot
}
// Start initializes commands, and starts listening for messages.
func (f *Finch) Start() {
if v := f.Config.Get("sentry_dsn"); v != nil {
sentryEnabled = true
raven.SetDSN(v.(string))
}
if v := f.Config.Get("sentry_env"); v != nil {
raven.SetEnvironment(v.(string))
}
f.commandInit()
u := tgbotapi.NewUpdate(0)
u.Timeout = 86400
updates := f.API.GetUpdatesChan(u)
for update := range updates {
go f.commandRouter(update)
}
}
// StartWebhook initializes commands,
// then registers a webhook for the bot to listen on
func (f *Finch) StartWebhook(endpoint string) {
f.commandInit()
f.API.ListenForWebhook(endpoint)
}
// SendMessage sends a message with various changes, and does not return the Message.
//
// At some point, this may do more handling as needed.
func (f *Finch) SendMessage(message tgbotapi.MessageConfig) error {
message.Text = strings.Replace(message.Text, "@@", "@"+f.API.Self.UserName, -1)
_, err := f.API.Send(message)
if err != nil && sentryEnabled {
raven.CaptureError(err, nil)
}
return err
}
// QuickReply quickly sends a message as a reply.
func (f *Finch) QuickReply(message tgbotapi.Message, text string) error {
msg := tgbotapi.NewMessage(message.Chat.ID, text)
msg.ReplyToMessageID = message.MessageID
return f.SendMessage(msg)
}