如何拆分传递给方法的接口。

huangapple go评论113阅读模式
英文:

How to split the interface passed to the method

问题

我使用这个库tucnak/telebot来构建一个Telegram机器人。

方法b.Handle()有两个参数,如Handle(endpoint interface{}, handler interface{})所示。

以下是我用于入门的代码:

  1. func main() {
  2. b, err := tb.NewBot(tb.Settings{
  3. Token: "TOKEN_HERE",
  4. Poller: &tb.LongPoller{Timeout: 10 * time.Second},
  5. })
  6. if err != nil {
  7. log.Fatal(err)
  8. return
  9. }
  10. // 我想要拆分这个(接口)
  11. b.Handle("/hello", func(m *tb.Message) {
  12. b.Send(m.Sender, "Hello World!")
  13. })
  14. b.Start()
  15. }

这是我尝试编译的代码:

  1. func main() {
  2. b, err := tb.NewBot(tb.Settings{
  3. Token: "TOKEN_HERE",
  4. Poller: &tb.LongPoller{Timeout: 10 * time.Second},
  5. })
  6. if err != nil {
  7. log.Fatal(err)
  8. return
  9. }
  10. b.Handle("/hello", handleHello(b))
  11. b.Start()
  12. }
  13. func handleHello(b *tb.Bot) {
  14. b.Send(m.Sender, "Hello World!")
  15. }

我在这段代码中遇到了一个错误:undefined m,在m.Sender()和我无法将m作为该函数调用的参数使用,因为出现了相同的错误。我不明白m是从哪里来的。

英文:

I use this library tucnak/telebot to build a telegram bot.

Method b.Handle() have two parameters such as Handle(endpoint interface{}, handler interface{})`.

Here is the code i use for a starter

  1. func main() {
  2. b, err := tb.NewBot(tb.Settings{
  3. Token: "TOKEN_HERE",
  4. Poller: &tb.LongPoller{Timeout: 10 * time.Second},
  5. })
  6. if err != nil {
  7. log.Fatal(err)
  8. return
  9. }
  10. // i want to split this(interface)
  11. b.Handle("/hello", func(m *tb.Message) {
  12. b.Send(m.Sender, "Hello World!")
  13. })
  14. b.Start()
  15. }

Here is what I have tried to compile :

  1. func main() {
  2. b, err := tb.NewBot(tb.Settings{
  3. Token: "TOKEN_HERE",
  4. Poller: &tb.LongPoller{Timeout: 10 * time.Second},
  5. })
  6. if err != nil {
  7. log.Fatal(err)
  8. return
  9. }
  10. b.Handle("/hello", handleHello(b))
  11. b.Start()
  12. }
  13. func handleHello(b *tb.Bot) {
  14. b.Send(m.Sender, "Hello World!")
  15. }

I have an error with this code : undefined m, in m.Sender() and I can't use m as a parameter for that function call, because of the same error. I don't understand where that m comes from.

答案1

得分: 2

机器人的责任是监听一些套接字,无论什么类型,并在关联的路径处理程序上收到消息时调用您的“函数处理程序”。

因此,您不应该像这样调用处理程序:b.Handle("/hello", handleHello(b))。而是将函数处理程序传递给机器人b.Handle("/hello", handleHello)。让机器人将新消息作为参数调用该函数,例如func(m *tb.Message)

要保留对b的引用,您可以按照Sinan Coment的描述进行操作。编写一个函数,该函数以机器人作为参数,并返回一个接收消息作为参数的函数。

机器人实例b充当muxer,您可以重用该术语来改进代码的含义。

muxer被定义为“[...]请求多路复用器。它将每个传入请求的URL与注册的模式列表进行匹配,并调用与URL最匹配的模式的处理程序。”

不过,我建议您将telebot.Bot的实例封装到一个类型中,该类型将消息处理程序定义为方法。

  1. package main
  2. import (
  3. "log"
  4. "time"
  5. tb "gopkg.in/tucnak/telebot.v2"
  6. )
  7. func main() {
  8. mux, err := tb.NewBot(tb.Settings{
  9. Token: "TOKEN_HERE",
  10. Poller: &tb.LongPoller{Timeout: 10 * time.Second},
  11. })
  12. if err != nil {
  13. log.Fatal(err)
  14. return
  15. }
  16. bot := botHandlers{Bot: mux}
  17. mux.Handle("/hello", bot.handleHello)
  18. mux.Start()
  19. }
  20. type botHandlers struct {
  21. *tb.Bot
  22. }
  23. func (b botHandlers) handleHello(m *tb.Message) {
  24. b.Send(m.Sender, "Hello World!")
  25. }

https://play.golang.org/p/6ng6WSIp8Er

英文:

The bot responsibility is to listen some sockets, whatever, and calls your function handlers when a message arrives on the associated path handler.

Thus, you should not try to call your handlers like in b.Handle("/hello", handleHello(b)). Instead pass the function handler to the bot b.Handle("/hello", handleHello). Let the bot call that function with the new message as a parameter like in func(m *tb.Message).

To retain a reference to b, you can proceed as described by Sinan Coment. Write a function that takes in parameter the bot and returns a function that receives the message as a parameter.

The bot instance b acts as a muxer, you can re use that terminology to improve the meaning of your code.

A muxer is defined as [...] a request multiplexer. It matches the URL of each incoming request against a list of registered patterns and calls the handler for the pattern that most closely matches the URL.

Though, I want to suggest you to wrap that instance of telebot.Bot into a type that defines message handlers as methods.

  1. package main
  2. import (
  3. "log"
  4. "time"
  5. tb "gopkg.in/tucnak/telebot.v2"
  6. )
  7. func main() {
  8. mux, err := tb.NewBot(tb.Settings{
  9. Token: "TOKEN_HERE",
  10. Poller: &tb.LongPoller{Timeout: 10 * time.Second},
  11. })
  12. if err != nil {
  13. log.Fatal(err)
  14. return
  15. }
  16. bot := botHandlers{Bot: mux}
  17. mux.Handle("/hello", bot.handleHello)
  18. mux.Start()
  19. }
  20. type botHandlers struct {
  21. *tb.Bot
  22. }
  23. func (b botHandlers) handleHello(m *tb.Message) {
  24. b.Send(m.Sender, "Hello World!")
  25. }

https://play.golang.org/p/6ng6WSIp8Er

答案2

得分: 0

你好,以下是翻译好的内容:

正如你所看到的,hello处理程序在这里接受一个名为m的参数。

  1. b.Handle("/hello", func(m *tb.Message) {

所以你应该返回一个接受相同参数的函数。

  1. func handleHello(b *tb.Bot) func(m *tb.Message) {
  2. return func(m *tb.Message) {
  3. b.Send(m.Sender, "Hello World!")
  4. }
  5. }
英文:

As you can see hello handler takes a parameter called m here.

  1. b.Handle("/hello", func(m *tb.Message) {

so you should return a function that takes the same parameter.

  1. func handleHello(b *tb.Bot) func(m *tb.Message) {
  2. return func(m *tb.Message) {
  3. b.Send(m.Sender, "Hello World!")
  4. }
  5. }

huangapple
  • 本文由 发表于 2021年9月26日 18:04:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/69333827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定