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

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

How to split the interface passed to the method

问题

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

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

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

func main() {
    b, err := tb.NewBot(tb.Settings{
        Token:  "TOKEN_HERE",
        Poller: &tb.LongPoller{Timeout: 10 * time.Second},
    })

    if err != nil {
        log.Fatal(err)
        return
    }
    // 我想要拆分这个(接口)
    b.Handle("/hello", func(m *tb.Message) {
        b.Send(m.Sender, "Hello World!")
    })

    b.Start()
}

这是我尝试编译的代码:

func main() {
    b, err := tb.NewBot(tb.Settings{
        Token:  "TOKEN_HERE",
        Poller: &tb.LongPoller{Timeout: 10 * time.Second},
    })

    if err != nil {
        log.Fatal(err)
        return
    }
                    
    b.Handle("/hello", handleHello(b))

    b.Start()
}

func handleHello(b *tb.Bot) {
    b.Send(m.Sender, "Hello World!")
}

我在这段代码中遇到了一个错误: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

func main() {
	b, err := tb.NewBot(tb.Settings{
		Token:  "TOKEN_HERE",
		Poller: &tb.LongPoller{Timeout: 10 * time.Second},
	})

	if err != nil {
		log.Fatal(err)
		return
	}
    // i want to split this(interface)
	b.Handle("/hello", func(m *tb.Message) {
		b.Send(m.Sender, "Hello World!")
	})

	b.Start()
}

Here is what I have tried to compile :

func main() {
	b, err := tb.NewBot(tb.Settings{
		Token:  "TOKEN_HERE",
		Poller: &tb.LongPoller{Timeout: 10 * time.Second},
	})

	if err != nil {
		log.Fatal(err)
		return
	}
                    
	b.Handle("/hello", handleHello(b))

	b.Start()
}

func handleHello(b *tb.Bot) {
		b.Send(m.Sender, "Hello World!")
}

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的实例封装到一个类型中,该类型将消息处理程序定义为方法。

package main

import (
	"log"
	"time"

	tb "gopkg.in/tucnak/telebot.v2"
)

func main() {
	mux, err := tb.NewBot(tb.Settings{
		Token:  "TOKEN_HERE",
		Poller: &tb.LongPoller{Timeout: 10 * time.Second},
	})

	if err != nil {
		log.Fatal(err)
		return
	}

	bot := botHandlers{Bot: mux}

	mux.Handle("/hello", bot.handleHello)

	mux.Start()
}

type botHandlers struct {
	*tb.Bot
}

func (b botHandlers) handleHello(m *tb.Message) {
	b.Send(m.Sender, "Hello World!")
}

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.

package main

import (
	"log"
	"time"

	tb "gopkg.in/tucnak/telebot.v2"
)

func main() {
	mux, err := tb.NewBot(tb.Settings{
		Token:  "TOKEN_HERE",
		Poller: &tb.LongPoller{Timeout: 10 * time.Second},
	})

	if err != nil {
		log.Fatal(err)
		return
	}

	bot := botHandlers{Bot: mux}

	mux.Handle("/hello", bot.handleHello)

	mux.Start()
}

type botHandlers struct {
	*tb.Bot
}

func (b botHandlers) handleHello(m *tb.Message) {
	b.Send(m.Sender, "Hello World!")
}

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

答案2

得分: 0

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

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

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

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

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

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

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

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

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

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:

确定