telebot icon indicating copy to clipboard operation
telebot copied to clipboard

how to use handle?

Open pd5200 opened this issue 1 year ago • 4 comments

package main

import ( "fmt" telebot "gopkg.in/telebot.v3" "log" )

func main() { // 机器人初始化 pref := telebot.Settings{ Token: "6x....", Poller: &telebot.LongPoller{Timeout: 10}, Verbose: true, // Enable debug logging } bot, err := telebot.NewBot( pref) if err != nil { log.Fatal(err) }

// 处理器1
handler1 := func(c telebot.Context) error {
	fmt.Println("Handler 1 executing")
	return c.Reply("Handler 1 executed")
}

// 处理器2
handler2 := func(c telebot.Context) error {
	fmt.Println("Handler 2 executing")
	return c.Reply("Handler 2 executed")
}

// 中间件
middleware := func(next telebot.HandlerFunc) telebot.HandlerFunc {
	return func(c telebot.Context) error {
		// 在处理器之前执行的逻辑
		fmt.Println("Middleware executing before handler")

		// 调用下一个中间件或处理器
		err := next(c)

		// 在处理器之后执行的逻辑
		fmt.Println("Middleware executing after handler")

		return err
	}
}

// 分组1
group1 := bot.Group()
group1.Use(middleware)
group1.Handle(telebot.OnText, handler1)

// 分组2
group2 := bot.Group()
group2.Use(middleware)
group2.Handle(telebot.OnText, handler2)

// 启动机器人
bot.Start()

} ////output

Middleware executing before handler Handler 2 executing

handler2 := func(c telebot.Context) error { fmt.Println("Handler 2 executing") return c.Reply("Handler 2 executed") }

This one is effective, but handler1 doesn't produce any output. How can we ensure that both groups have information output and are effective?

pd5200 avatar Jan 19 '24 14:01 pd5200

I want to determine whether it's from a group or a private chat to respond. I want to use middleware to filter the conditions. So the above method cannot be used. Is there a simpler method?

pd5200 avatar Jan 19 '24 15:01 pd5200

i want like this:

// filter for: any text message from anywhere bot.Handle(filter.OnAnyText, yourHandlerFunc)

// filter for: only supergroup and any text message bot.Handle(filter.OnAnyText, yourHandlerFunc,filter.OnlySupergroup)

// filter for: only (supergroup or group) and any text message bot.Handle(filter.OnAnyText, yourHandlerFunc,filter.OnlySupergroupOrGroup)

// filter for: only private and any text message bot.Handle(filter.OnAnyText, yourHandlerFunc, filter.OnlyPrivate)

// filter for: only private and text message == "ping" bot.Handle(filter.OnText("ping"), yourHandlerFunc, filter.OnlyPrivate)

pd5200 avatar Jan 20 '24 03:01 pd5200

I want to determine whether it's from a group or a private chat to respond. I want to use middleware to filter the conditions. So the above method cannot be used. Is there a simpler method?

bot.Handle(telebot.OnText, func(c telebot.Context) error {
	switch c.Chat().Type {
	case telebot.ChatPrivate:
		// yourPrivateHandlerFunc(c)
		break
	case telebot.ChatGroup:
		break
	case telebot.ChatChannel:
		// support ChatPrivate ChatGroup ChatSuperGroup ChatChannel ChatChannelPrivate
		break
	}
	return nil
})

Look at this example, Does it meet your needs.

Vincent7978 avatar Jan 20 '24 08:01 Vincent7978