Initial
This commit is contained in:
		
							
								
								
									
										43
									
								
								bot/bot.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								bot/bot.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,43 @@
 | 
			
		||||
package bot
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"log"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	tb "gopkg.in/tucnak/telebot.v3"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Handler struct {
 | 
			
		||||
	Endpoint interface{}
 | 
			
		||||
	Handler  tb.HandlerFunc
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Bot struct {
 | 
			
		||||
	Bot   *tb.Bot
 | 
			
		||||
	Token string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (b *Bot) Init() error {
 | 
			
		||||
	bot, err := tb.NewBot(tb.Settings{
 | 
			
		||||
		Token:  b.Token,
 | 
			
		||||
		Poller: &tb.LongPoller{Timeout: 10 * time.Second},
 | 
			
		||||
	})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Print(err)
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	b.Bot = bot
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Add handler methods to the bot
 | 
			
		||||
func (b *Bot) AddHandlers(handlers []Handler) error {
 | 
			
		||||
	if len(handlers) != 0 {
 | 
			
		||||
		for i := range handlers {
 | 
			
		||||
			b.Bot.Handle(handlers[i].Endpoint, handlers[i].Handler)
 | 
			
		||||
		}
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
	return errors.New("no handlers are declared")
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										16
									
								
								bot/handlers.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								bot/handlers.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,16 @@
 | 
			
		||||
package bot
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	tb "gopkg.in/tucnak/telebot.v3"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var HandlersV1 = []Handler{
 | 
			
		||||
	{
 | 
			
		||||
		Endpoint: tb.OnText,
 | 
			
		||||
		Handler: func(c tb.Context) error {
 | 
			
		||||
			m := c.Message()
 | 
			
		||||
			c.Bot().Send(m.Sender, m.Text)
 | 
			
		||||
			return nil
 | 
			
		||||
		},
 | 
			
		||||
	},
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user