gotbot

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2020 License: MIT Imports: 11 Imported by: 0

README

Gotbot

Gotbot is a small framework for creating Telegram Bots written in Golang.

Test

Bots are special Telegram accounts designed to handle messages automatically. Users can interact with bots by sending them command messages in private or group chats. These accounts serve as an interface for code running somewhere on your server.

Gotbot offers a framework to build bots that handle text requsets. You pass bot a set of commands and framework executes provided command handlers ensuring all command parameters have been collected from user.

Telebot is used to communicate with Telegram API. Here is an example "helloworld" bot, written with gotbot:

import (
    "fmt"

    "github.com/variar/gotbot"
)

func main() {
    bot, err := gotbot.NewBot("SECRET TOKEN")
    if err != nil {
        return
    }

    bot.AddCommand("/start",
      gotbot.NewCommandHandler(
      func(parsedParams map[string]string, replySender gotbot.ReplySender) {
        reply := fmt.Sprintf("Hello, %s!", replySender.FirstName())
        replySender.SendReply(reply)
      })
    )

    commandWithParam := gotbot.NewCommandHandler(
      func(parsedParams map[string]string, replySender gotbot.ReplySender) {
        reply := fmt.Sprintf("Param1 = %s!", parsedParams["param1"])
        replySender.SendReply(reply)
      }
    )

    parameter := gotbot.CommandParameter{
  		Name:        "param1",
  		AskQuestion: "Please, send param1",
  		Parse:       func(text string) (string, error) { return text, nil }
    }

    commandWithParam.AddParameter(&parameter)
	  bot.AddCommand("/command", commandWithParam)

    bot.Start()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bot

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

func NewBot

func NewBot(token string, statKey string, httpClient *http.Client, logger Logger) (*Bot, error)

func (*Bot) AddCommand

func (bot *Bot) AddCommand(handler *CommandHandler) *Bot

func (*Bot) SetChatProcessorFactory

func (bot *Bot) SetChatProcessorFactory(chatProcessorFactory ChatProcessorFactory) *Bot

func (*Bot) SetMenu

func (bot *Bot) SetMenu(menu *Menu) *Bot

func (*Bot) Start

func (bot *Bot) Start()

type ChatProcessor

type ChatProcessor func(messageID int, message string) (ChatProcessor, string)

type ChatProcessorFactory

type ChatProcessorFactory func(replySender ReplySender, logger Logger) ChatProcessor

type CommandHandler

type CommandHandler struct {
	Name string
	// contains filtered or unexported fields
}

func NewCommandHandler

func NewCommandHandler(name string, processer ProcessCommand) *CommandHandler

func (*CommandHandler) AddParameter

func (command *CommandHandler) AddParameter(parameter *CommandParameter) *CommandHandler

type CommandParameter

type CommandParameter struct {
	Name          string
	AskQuestion   string
	ParseText     TextParameterParser
	ParseLocation LocationParameterParser
	InlineHandler InlineParameterHandler
}

type InlineParameterHandler

type InlineParameterHandler interface {
	GetReplyMarkup() interface{}
	ParseCallback(data string) (string, error)
}

type Location

type Location struct {
	Lon float64
	Lat float64
}

type LocationParameterParser

type LocationParameterParser func(loc Location) (string, error)

type Logger

type Logger interface {
	Error(args ...interface{})
	Errorf(template string, args ...interface{})

	Info(args ...interface{})
	Infof(template string, args ...interface{})
}
type Menu struct {
	Name   string
	Items  []MenuEntry
	Parent *Menu
}

func NewMenu

func NewMenu(name string) *Menu
func (menu *Menu) AddCommand(name string, command *CommandHandler) *Menu
func (menu *Menu) AddSumbenu(submenu *Menu) *Menu
type MenuEntry struct {
	Name    string
	Command *CommandHandler
	Submenu *Menu
}
func (menuEntry *MenuEntry) IsCommand() bool

type ProcessCommand

type ProcessCommand func(parsedParams map[string]string, replySender ReplySender)

type ReplySender

type ReplySender interface {
	SendReply(reply string)
	SendReplyWithMarkup(reply string, markup interface{})
	UpdateMessage(messageID int, text string, markup *tgbotapi.InlineKeyboardMarkup)
	AskOptions(reply string, options []string)
	FirstName() string
}

type TextParameterParser

type TextParameterParser func(input string) (string, error)

type TgBot

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

func NewDefaultTgBot

func NewDefaultTgBot(tgToken string, statKey string, logger Logger) (*TgBot, error)

func NewTgBot

func NewTgBot(tgToken string, statKey string, httpClient *http.Client, logger Logger) (*TgBot, error)

func (*TgBot) BotStat

func (bot *TgBot) BotStat(user *tgbotapi.User, message tgbotapi.MessageConfig) *chatbase.Message

func (*TgBot) CallbacStat

func (bot *TgBot) CallbacStat(callback *tgbotapi.CallbackQuery, intent string) *chatbase.Message

func (*TgBot) GetApi

func (bot *TgBot) GetApi() *tgbotapi.BotAPI

func (*TgBot) GetStat

func (bot *TgBot) GetStat() *chatbase.Client

func (*TgBot) MessageStat

func (bot *TgBot) MessageStat(message *tgbotapi.Message, intent string) *chatbase.Message

func (*TgBot) QueryStat

func (bot *TgBot) QueryStat(query *tgbotapi.InlineQuery, intent string) *chatbase.Message

func (*TgBot) Run

func (bot *TgBot) Run(updatesTimeout int, handler UpdateHandler)

func (*TgBot) RunAsync

func (bot *TgBot) RunAsync(updatesTimeout int, waitGroup *sync.WaitGroup, doneChannel <-chan bool, handler UpdateHandler)

func (*TgBot) SendBotReply

func (bot *TgBot) SendBotReply(user *tgbotapi.User, message tgbotapi.MessageConfig)

func (*TgBot) SendStat

func (bot *TgBot) SendStat(statMessage *chatbase.Message)

type UpdateHandler

type UpdateHandler func(update tgbotapi.Update)

Jump to

Keyboard shortcuts

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