tgbot

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2022 License: Apache-2.0 Imports: 4 Imported by: 2

README

Telegram bot framework GoDoc Go Report Card

tgbot is an extension for telegram-bot-api package. It helps you easily bind functions to handle any messages and callback queries

Getting started

Install package:

go get -u github.com/wawan93/bot-framework

Usage

package main

import (
	"github.com/go-telegram-bot-api/telegram-bot-api"
	"github.com/wawan93/bot-framework"
)

func Start(bot *tgbot.BotFramework, update *tgbotapi.Update) error {
	chatID := bot.GetChatID(update)
	msg := tgbotapi.NewMessage(chatID, "Hello, World!")
	_, err := bot.Send(msg)
	return err
}

func main() {
	token := "123:YOUR-TOKEN"
	api, _ := tgbotapi.NewBotAPI(token)

	u := tgbotapi.NewUpdate(0)
	updates, _ := api.GetUpdatesChan(u)
  
	// extend api
	bot := tgbot.NewBotFramework(api)
  
	// bind handler Start for "/start" command in chat 0 (any chat)
	bot.RegisterCommand("/start", Start, 0)

	// endless loop handles updates from channel
	bot.HandleUpdates(updates)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoHandlersError = errors.New("no handlers for update")

Functions

This section is empty.

Types

type BotFramework

type BotFramework struct {
	tgbotapi.BotAPI

	ErrorHandler func(u tgbotapi.Update, err error)
	// contains filtered or unexported fields
}

BotFramework main object to work with. Instantiate using NewBotFramework

func NewBotFramework

func NewBotFramework(api *tgbotapi.BotAPI) *BotFramework

NewBotFramework creates new bot instance

func (*BotFramework) GetChatID

func (bot *BotFramework) GetChatID(update *tgbotapi.Update) int64

GetChatID returns chat ID from update message and callback query

func (*BotFramework) HandleUpdate

func (bot *BotFramework) HandleUpdate(update *tgbotapi.Update) error

HandleUpdate handles single update from channel

func (*BotFramework) HandleUpdates

func (bot *BotFramework) HandleUpdates(ch tgbotapi.UpdatesChannel)

HandleUpdates handles all updates from channel. save for panics

func (*BotFramework) RegisterAudioHandler

func (bot *BotFramework) RegisterAudioHandler(f CommonHandler, chatID int64) error

RegisterAudioHandler binds handler for audio message from given chat If chatID = 0, command will work in any chat

func (*BotFramework) RegisterCallbackQueryHandler

func (bot *BotFramework) RegisterCallbackQueryHandler(f CommonHandler, dataStartsWith string, chatID int64) error

RegisterCallbackQueryHandler binds handler for callback data If chatID = 0, command will work in any chat

func (*BotFramework) RegisterCommand

func (bot *BotFramework) RegisterCommand(name string, f CommonHandler, chatID int64) error

RegisterCommand binds handler for commands If chatID=0, command will work in any chat Commands name can be any string For example:

bot.RegisterCommand("/start", SomeStartHandler, -1001234567)

binds same handler for "/start", "/start@your_bot", "/start@your_bot someReferralCode"

bot.RegisterCommand("🔔 Subscribe", SomeSubscribeHandler, 0)

binds handler for message text "🔔 Subscribe"

func (*BotFramework) RegisterContactHandler

func (bot *BotFramework) RegisterContactHandler(f CommonHandler, chatID int64) error

RegisterContactHandler binds handler for contact message from given chat If chatID = 0, command will work in any chat

func (*BotFramework) RegisterFileHandler

func (bot *BotFramework) RegisterFileHandler(f CommonHandler, chatID int64) error

RegisterFileHandler binds handler for file from given chat If chatID = 0, command will work in any chat

func (*BotFramework) RegisterInlineQueryHandler

func (bot *BotFramework) RegisterInlineQueryHandler(f CommonHandler, query string, userID int64) error

RegisterInlineQueryHandler binds handler for query If userID = 0, command will work for any user

func (*BotFramework) RegisterLocationHandler

func (bot *BotFramework) RegisterLocationHandler(f CommonHandler, chatID int64) error

RegisterLocationHandler binds handler for location message from given chat If chatID = 0, command will work in any chat

func (*BotFramework) RegisterPhotoHandler

func (bot *BotFramework) RegisterPhotoHandler(f CommonHandler, chatID int64) error

RegisterPhotoHandler binds handler for photo message from given chat If chatID = 0, command will work in any chat

func (*BotFramework) RegisterPlainTextHandler

func (bot *BotFramework) RegisterPlainTextHandler(f CommonHandler, chatID int64) error

RegisterPlainTextHandler binds handler for plain text message from given chat If chatID = 0, command will work in any chat

func (*BotFramework) RegisterStickerHandler

func (bot *BotFramework) RegisterStickerHandler(f CommonHandler, chatID int64) error

RegisterStickerHandler binds handler for sticker from given chat If chatID = 0, command will work in any chat

func (*BotFramework) RegisterUniversalHandler

func (bot *BotFramework) RegisterUniversalHandler(f CommonHandler, chatID int64) error

RegisterUniversalHandler binds handler for any message from given chat If chatID = 0, command will work in any chat

func (*BotFramework) RegisterVenueHandler

func (bot *BotFramework) RegisterVenueHandler(f CommonHandler, chatID int64) error

RegisterVenueHandler binds handler for venue message from given chat If chatID = 0, command will work in any chat

func (*BotFramework) RegisterVideoHandler

func (bot *BotFramework) RegisterVideoHandler(f CommonHandler, chatID int64) error

RegisterVideoHandler binds handler for video message from given chat If chatID = 0, command will work in any chat

func (*BotFramework) RegisterVideoNoteHandler

func (bot *BotFramework) RegisterVideoNoteHandler(f CommonHandler, chatID int64) error

RegisterVideoNoteHandler binds handler for video_note message from given chat If chatID = 0, command will work in any chat

func (*BotFramework) RegisterVoiceHandler

func (bot *BotFramework) RegisterVoiceHandler(f CommonHandler, chatID int64) error

RegisterVoiceHandler binds handler for voice message from given chat If chatID = 0, command will work in any chat

func (*BotFramework) UnregisterAudioHandler

func (bot *BotFramework) UnregisterAudioHandler(chatID int64) error

UnregisterAudioHandler deletes handler for given chat

func (*BotFramework) UnregisterCallbackQueryHandler

func (bot *BotFramework) UnregisterCallbackQueryHandler(dataStartsWith string, chatID int64) error

UnregisterCallbackQueryHandler deletes handler for given chat

func (*BotFramework) UnregisterCommand

func (bot *BotFramework) UnregisterCommand(name string, chatID int64) error

UnregisterCommand deletes handler for command name in given chat

func (*BotFramework) UnregisterContactHandler

func (bot *BotFramework) UnregisterContactHandler(chatID int64) error

UnregisterContactHandler deletes handler for given chat

func (*BotFramework) UnregisterFileHandler

func (bot *BotFramework) UnregisterFileHandler(chatID int64) error

UnregisterFileHandler deletes handler for given chat

func (*BotFramework) UnregisterInlineQueryHandler

func (bot *BotFramework) UnregisterInlineQueryHandler(query string, userID int64) error

UnregisterInlineQueryHandler deletes handler for given user

func (*BotFramework) UnregisterLocationHandler

func (bot *BotFramework) UnregisterLocationHandler(chatID int64) error

UnregisterLocationHandler deletes handler for given chat

func (*BotFramework) UnregisterPhotoHandler

func (bot *BotFramework) UnregisterPhotoHandler(chatID int64) error

UnregisterPhotoHandler deletes handler for given chat

func (*BotFramework) UnregisterPlainTextHandler

func (bot *BotFramework) UnregisterPlainTextHandler(chatID int64) error

UnregisterPlainTextHandler deletes handler for given chat

func (*BotFramework) UnregisterStickerHandler

func (bot *BotFramework) UnregisterStickerHandler(chatID int64) error

UnregisterStickerHandler deletes handler for given chat

func (*BotFramework) UnregisterUniversalHandler

func (bot *BotFramework) UnregisterUniversalHandler(chatID int64) error

UnregisterUniversalHandler deletes handler for given chat

func (*BotFramework) UnregisterVenueHandler

func (bot *BotFramework) UnregisterVenueHandler(chatID int64) error

UnregisterVenueHandler deletes handler for given chat

func (*BotFramework) UnregisterVideoHandler

func (bot *BotFramework) UnregisterVideoHandler(chatID int64) error

UnregisterVideoHandler deletes handler for given chat

func (*BotFramework) UnregisterVideoNoteHandler

func (bot *BotFramework) UnregisterVideoNoteHandler(chatID int64) error

UnregisterVideoNoteHandler deletes handler for given chat

func (*BotFramework) UnregisterVoiceHandler

func (bot *BotFramework) UnregisterVoiceHandler(chatID int64) error

UnregisterVoiceHandler deletes handler for given chat

type CommonHandler

type CommonHandler func(bot *BotFramework, update *tgbotapi.Update) error

CommonHandler is a short type alias for handler function

Jump to

Keyboard shortcuts

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