tbwrap

package module
v0.0.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 7, 2021 License: MIT Imports: 7 Imported by: 0

README

tbwrap

Wrapper library for github.com/tucnak/telebot to create simple text-based Telegram bots

Installation

go get github.com/enrico5b1b4/tbwrap

Examples

Ping Bot - A bot which responds to your ping with a pong
Greet Bot - A bot which greets you
Todo List Bot - A bot that keeps a todo list for you
Joke Bot - A bot that tells you a joke
Weather Bot - A bot that tells you the weather forecast

Usage

Simple
package main

import (
	"log"

	"github.com/enrico5b1b4/tbwrap"
)

func main() {
	telegramBotToken := "TELEGRAM_BOT_TOKEN"

	botConfig := tbwrap.Config{
		Token: telegramBotToken,
	}
	telegramBot, err := tbwrap.NewBot(botConfig)
	if err != nil {
		log.Fatal(err)
	}

	telegramBot.Handle(`/ping`, func(c tbwrap.Context) error {
		_, err := c.Send("pong!")

		return err
	})
	telegramBot.Start()
}
Handlers

There are three ways you can register handlers to respond to a user's message

Handle

Invokes handler if message matches the text provided

// ...
func main() {
	// ...
	telegramBot.Handle(`/ping`, func(c tbwrap.Context) error {
		// ...
		_, err := c.Send("...")

		return err
	})
	// ...
}
HandleRegExp

Invokes handler if message matches the regular expression provided

// ...
func main() {
	// ...
	telegramBot.HandleRegExp(`\/greet (?P<name>.*)`, func(c tbwrap.Context) error {
		// ...
		_, err := c.Send("...")

		return err
    })
	// ...
}
HandleMultiRegExp

Invokes handler if message matches any of the regular expressions provided

// ...
func main() {
	// ...
	telegramBot.HandleMultiRegExp([]string{
		`\/greet (?P<name>.*)`,
		`\/welcome (?P<name>.*)`,
	}, func(c tbwrap.Context) error {
		// ...
		_, err := c.Send("...")

		return err
    })
	// ...
}
Messages

Read the user's incoming message with c.Text()

// ...

func main() {
	// ...
	telegramBot.HandleRegExp(`\/greet (?P<name>.*)`, func(c tbwrap.Context) error {
		// ...
		fmt.Println(c.Text()) // "/greet Enrico"

		_, err := c.Send("Hello!")

		return err
    })
	// ...
}

Use regular expression named capturing groups to parametrise incoming messages

// ...

type GreetMessage struct {
    Name string `regexpGroup:"name"`
}

func main() {
	// ...
	telegramBot.HandleRegExp(`\/greet (?P<name>.*)`, func(c tbwrap.Context) error {
		message := new(GreetMessage)
		if err := c.Bind(message); err != nil {
			return err
		}

		fmt.Println(message.Name) // "Enrico"

		_, err := c.Send(fmt.Sprintf("Hello %s!", message.Name))

		return err
    })
	// ...
}
Private bot

You can make the bot respond only to certain chats (users or groups) by passing a list of ids.

// ...
func main() {
	// ...
	botConfig := tbwrap.Config{
		Token:  telegramBotToken,
		AllowedChats:  []int{"123456","234567","-999999"}, // only user and group chats with these ids can interact with the bot
	}
	telegramBot, err := tbwrap.NewBot(botConfig)
	if err != nil {
		log.Fatal(err)
	}
	// ...
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewPollerWithAllowedChats

func NewPollerWithAllowedChats(pollTimout time.Duration, chats []int) *tb.MiddlewarePoller

Types

type Bot

type Bot interface {
	Respond(callback *tb.Callback, responseOptional ...*tb.CallbackResponse) error
	Send(to tb.Recipient, what interface{}, options ...interface{}) (*tb.Message, error)
	Delete(chatID int64, messageID int) error
}

type Config

type Config struct {
	Token        string
	AllowedChats []int
	TBot         TeleBot
}

type Context

type Context interface {
	Param(key string) string
	Bind(i interface{}) error
	Message() *tb.Message
	ChatID() int64
	Text() string
	Callback() *tb.Callback
	Respond(callback *tb.Callback, response ...*tb.CallbackResponse) error
	Send(msg string, options ...interface{}) (*tb.Message, error)
	Delete(chatID int64, messageID int) error
}

func NewContext

func NewContext(
	bot Bot,
	message *tb.Message,
	callback *tb.Callback,
	route *regexp.Regexp,
) Context

type HandlerFunc

type HandlerFunc func(c Context) error

type Route

type Route struct {
	Path    *regexp.Regexp
	Handler HandlerFunc
}

type TeleBot

type TeleBot interface {
	Handle(endpoint interface{}, handler interface{})
	Delete(message tb.Editable) error
	Respond(callback *tb.Callback, responseOptional ...*tb.CallbackResponse) error
	Send(to tb.Recipient, what interface{}, options ...interface{}) (*tb.Message, error)
	Start()
}

type WrapBot

type WrapBot struct {
	// contains filtered or unexported fields
}

func NewBot

func NewBot(cfg Config) (*WrapBot, error)

func (*WrapBot) Delete

func (b *WrapBot) Delete(chatID int64, messageID int) error

func (*WrapBot) Handle

func (b *WrapBot) Handle(path string, handler HandlerFunc)

func (*WrapBot) HandleButton

func (b *WrapBot) HandleButton(path *tb.InlineButton, handler HandlerFunc)

func (*WrapBot) HandleMultiRegExp

func (b *WrapBot) HandleMultiRegExp(paths []string, handler HandlerFunc)

func (*WrapBot) HandleRegExp

func (b *WrapBot) HandleRegExp(path string, handler HandlerFunc)

func (*WrapBot) Respond

func (b *WrapBot) Respond(callback *tb.Callback, responseOptional ...*tb.CallbackResponse) error

func (*WrapBot) Send

func (b *WrapBot) Send(to tb.Recipient, what interface{}, options ...interface{}) (*tb.Message, error)

func (*WrapBot) Start

func (b *WrapBot) Start()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL