G
G
gelerum2021-09-29 14:46:57
go
gelerum, 2021-09-29 14:46:57

How to separate a function for Handle'ra in telebot?

Hello, I recently started writing in go. I am writing a telegram bot on tucnak/telebot
There is this code:

bot, _ := tb.NewBot(tb.Settings{
    Token:  "bot",
    Poller: &tb.LongPoller{Timeout: 10 * time.Second},
  })
bot.Handle("/help", func(m *tb.Message) {
    bot.Send(m.Sender, "It is help")
  })

But I can’t take it into a separate function in any way because, I can’t transfer it to a function because, I have no idea where it comes from. How can I do it? accepts it as . func(m *tb.Message)m *tb.Messagebot.Handleinterface{}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Kulakov, 2021-09-29
@gelerum

Are you talking about it?

func handler(m *tb.Message) error {
  bot.Send(m.Sender, "It is help")
}

bot, _ := tb.NewBot(tb.Settings{
    Token:  "bot",
    Poller: &tb.LongPoller{Timeout: 10 * time.Second},
})
bot.Handle("/help", handler)

Or so?
func handler() func(*tb.Message) error {
  return func(m *tb.Message) error {
    bot.Send(m.Sender, "It is help")
  }
}

bot, _ := tb.NewBot(tb.Settings{
    Token:  "bot",
    Poller: &tb.LongPoller{Timeout: 10 * time.Second},
})
bot.Handle("/help", handler())

R
Rerurk, 2021-09-29
@Rerurk

Can I follow up with a question? And how to send a message in this way to a specific user, knowing his roomId?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question