tg/bot.go

270 lines
5.2 KiB
Go
Raw Permalink Normal View History

2023-08-19 09:12:26 +03:00
package tg
2023-07-09 01:28:59 +03:00
import (
2023-08-13 15:37:36 +03:00
"errors"
"sort"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
2023-07-09 01:28:59 +03:00
)
type Chat = tgbotapi.Chat
type User = tgbotapi.User
2023-08-13 15:37:36 +03:00
2023-07-09 01:28:59 +03:00
// The wrapper around Telegram API.
type Bot struct {
// Custom data value.
data any
api *tgbotapi.BotAPI
me User
2023-08-13 15:37:36 +03:00
// Private bot behaviour.
behaviour *Behaviour
// Group bot behaviour.
2023-09-30 09:55:45 +03:00
//groupBehaviour *GroupBehaviour
2023-08-13 15:37:36 +03:00
// Bot behaviour in channels.
2023-09-30 09:55:45 +03:00
//channelBehaviour *ChannelBehaviour
2023-08-13 15:37:36 +03:00
sessions SessionMap
2023-09-30 09:55:45 +03:00
//groupSessions GroupSessionMap
2023-07-09 01:28:59 +03:00
}
2023-08-13 15:37:36 +03:00
// Return the new bot with empty sessions and behaviour.
func NewBot(token string) (*Bot, error) {
bot, err := tgbotapi.NewBotAPI(token)
if err != nil {
return nil, err
}
return &Bot{
api: bot,
}, nil
2023-07-09 01:28:59 +03:00
}
2024-03-29 14:30:48 +03:00
func (bot *Bot) SetDebug(debug bool) *Bot {
bot.api.Debug = debug
return bot
}
func (bot *Bot) API() *tgbotapi.BotAPI {
return bot.api
}
func (bot *Bot) Me() User {
return bot.me
}
// Send the Renderable to the specified session client side.
// Can be used for both group and private sessions because
// SessionID represents both for chat IDs.
func (bot *Bot) Send(
sid SessionID, v Sendable,
2024-03-29 14:30:48 +03:00
) (*Message, error) {
2023-12-13 09:05:29 +03:00
config := v.SendConfig(sid, bot)
if config.Error != nil {
2024-03-29 14:30:48 +03:00
return nil, config.Error
2023-09-26 17:13:31 +03:00
}
msg, err := bot.api.Send(config.ToAPI())
2023-12-13 09:05:29 +03:00
if err != nil {
2024-03-29 14:30:48 +03:00
return nil, err
}
2024-03-29 14:30:48 +03:00
v.SetMessage(&msg)
return &msg, nil
}
2024-01-17 16:39:52 +03:00
func (bot *Bot) Sendf(
sid SessionID, format string, v ...any,
2024-03-29 14:30:48 +03:00
) (*Message, error){
2024-01-17 16:39:52 +03:00
return bot.Send(
sid,
2024-03-29 14:30:48 +03:00
Messagef(format, v...),
2024-01-17 16:39:52 +03:00
)
}
// Send to the session specified its ID raw chattable from the tgbotapi.
func (bot *Bot) SendRaw(
sid SessionID, v tgbotapi.Chattable,
) (*Message, error) {
msg, err := bot.api.Send(v)
if err != nil {
return nil, err
}
return &msg, nil
}
2023-09-30 09:55:45 +03:00
// Get session by its ID. Can be used for any scope
// including private, group and channel.
2024-03-29 14:30:48 +03:00
func (bot *Bot) GotSession(
sid SessionID,
) (*Session, bool) {
session, ok := bot.sessions[sid]
return session, ok
2023-08-13 15:37:36 +03:00
}
2024-03-29 14:30:48 +03:00
func (bot *Bot) SetData(v any) *Bot {
bot.data = v
return bot
}
func (bot *Bot) Data() any {
return bot.data
}
func (b *Bot) SetBehaviour(beh *Behaviour) *Bot {
2023-08-13 15:37:36 +03:00
b.behaviour = beh
b.sessions = make(SessionMap)
return b
}
2024-03-29 14:30:48 +03:00
func (b *Bot) SetSessions(sessions SessionMap) *Bot {
2023-08-13 15:37:36 +03:00
b.sessions = sessions
return b
}
2023-09-30 09:55:45 +03:00
/*func (b *Bot) WithGroupBehaviour(beh *GroupBehaviour) *Bot {
2023-08-13 15:37:36 +03:00
b.groupBehaviour = beh
b.groupSessions = make(GroupSessionMap)
return b
}
func (b *Bot) WithGroupSessions(sessions GroupSessionMap) *Bot {
b.groupSessions = sessions
return b
2023-09-30 09:55:45 +03:00
}*/
func (bot *Bot) DeleteCommands() {
//tgbotapi.NewBotCommandScopeAllPrivateChats(),
cfg := tgbotapi.NewDeleteMyCommands()
bot.api.Request(cfg)
2023-08-13 15:37:36 +03:00
}
// Setting the command on the user side.
func (bot *Bot) SetCommands(
scope tgbotapi.BotCommandScope,
2023-09-30 09:55:45 +03:00
cmdMap CommandMap,
) error {
// First the private commands.
names := []string{}
for name := range cmdMap {
names = append(names, string(name))
}
sort.Strings(names)
2024-03-29 14:30:48 +03:00
cmds := []Command{}
for _, name := range names {
cmds = append(
cmds,
cmdMap[CommandName(name)],
)
}
botCmds := []tgbotapi.BotCommand{}
for _, cmd := range cmds {
botCmds = append(botCmds, cmd.ToAPI())
}
//tgbotapi.NewBotCommandScopeAllPrivateChats(),
cfg := tgbotapi.NewSetMyCommandsWithScope(
scope,
botCmds...,
)
_, err := bot.api.Request(cfg)
if err != nil {
return err
}
return nil
}
2023-07-09 01:28:59 +03:00
// Run the bot with the Behaviour.
func (bot *Bot) Run() error {
2023-09-30 09:55:45 +03:00
if bot.behaviour == nil {
2023-08-13 15:37:36 +03:00
return errors.New("no behaviour defined")
}
if bot.behaviour != nil && bot.behaviour.Root == nil {
return errors.New("the root widget is not set, cannot run")
}
uc := tgbotapi.NewUpdate(0)
2023-09-30 09:55:45 +03:00
uc.Timeout = 10
updates := bot.api.GetUpdatesChan(uc)
handles := make(map[string] chan Update)
2023-08-13 15:37:36 +03:00
if bot.behaviour != nil {
chn := make(chan Update)
2023-08-13 15:37:36 +03:00
handles["private"] = chn
go bot.handlePrivate(chn)
}
2023-08-12 14:35:33 +03:00
2023-09-30 09:55:45 +03:00
/*if bot.groupBehaviour != nil {
commanders := make(map[CommandName] BotCommander)
for k, v := range bot.groupBehaviour.Commands {
commanders[k] = v
}
bot.SetCommands(
tgbotapi.NewBotCommandScopeAllGroupChats(),
commanders,
)
2023-08-13 15:37:36 +03:00
chn := make(chan *Update)
handles["group"] = chn
handles["supergroup"] = chn
go bot.handleGroup(chn)
2023-09-30 09:55:45 +03:00
}*/
2023-08-13 15:37:36 +03:00
me, _ := bot.API().GetMe()
bot.me = me
for up := range updates {
u := Update{
Update: up,
}
// Sometimes returns nil.
fromChat := u.FromChat()
if fromChat == nil {
continue
}
chn, ok := handles[fromChat.Type]
2024-03-29 14:30:48 +03:00
// Skipping non existent scope.
2023-08-13 15:37:36 +03:00
if !ok {
continue
2023-07-12 14:20:52 +03:00
}
2023-08-13 15:37:36 +03:00
chn <- u
2023-07-09 01:28:59 +03:00
}
2023-07-09 01:28:59 +03:00
return nil
}
2023-08-12 14:35:33 +03:00
// The function handles updates supposed for the private
// chat with the bot.
func (bot *Bot) handlePrivate(updates chan Update) {
var sid SessionID
2023-08-13 15:37:36 +03:00
for u := range updates {
sid = SessionID(u.FromChat().ID)
2024-03-29 14:30:48 +03:00
session, sessionOk := bot.sessions[sid]
if u.Message != nil && !sessionOk {
// Creating session if we have none
// but only on text messages.
session = bot.sessions.Add(bot, sid, PrivateSessionScope)
2023-09-26 17:13:31 +03:00
2024-03-29 14:30:48 +03:00
// Creating the root context
// that takes updates directly from
// the session.
rootContext := Context{
session: session,
2024-03-29 14:30:48 +03:00
update: u,
input: session.updates,
}
go rootContext.serve()
rootContext.input.Send(u)
2023-09-26 17:13:31 +03:00
continue
2023-08-13 15:37:36 +03:00
}
2023-08-19 13:25:47 +03:00
2024-03-29 14:30:48 +03:00
if sessionOk {
session.updates.Send(u)
2023-08-12 14:35:33 +03:00
}
}
}
2023-08-13 15:37:36 +03:00