yagolangtbotapi

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 6, 2022 License: MIT Imports: 16 Imported by: 0

README

Telegram bot api client for golang

yet another Golang framework for Telegram Bot API client (Telegram Passport included)

I've seen, there's a lot good frameworks for this stuff, but unfortunately without passport :(

go get -u github.com/udev-21/ya-golang-tbot-api

Disclaimer

This project is my PET project and under MIT license :).

Overview

This is an API client library for Telegram Bot API.

Of course PR's and Forks are welcome :)

Features

  • Full Telegram Bot API 6.0 support
  • Zero dependency
  • Filters

Simple Echo Bot

All examples you can find under examples folder here's one of them:

package main

import (
	gtbotapi "github.com/udev-21/ya-golang-tbot-api"
	"github.com/udev-21/ya-golang-tbot-api/filter"
	"github.com/udev-21/ya-golang-tbot-api/method"
)

func handle(ctx gtbotapi.Context) error {
	text := "pong" // response text
	payload := method.NewSendMessage(text)

	payload.WithReplyToMessageID(ctx.Message().MessageID) //  reply to specific message

	receiver := ctx.Chat() // choose message reciever chat
	_, err := ctx.Send(receiver, payload)
	// apiResponse, err := ctx.Send(reciver, payload) // use this one if you need process api response

	return err
}

// replace value with your BOT TOKEN which gives you @botfather on telegram 
const TOKEN = "BOT:TOKEN" 

func main() {
	bot := gtbotapi.NewBotAPI(TOKEN)

	// if you send "ping" any chat this one will reply as "pong"
	bot.Handle(filter.OnText("ping"), handle)

	bot.Start()
}

Filters

Full list of filters you can find in here

Here's some examples for using them:

// 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) 

Documentation

Index

Constants

View Source
const (
	LogLevelInfo = iota
	LogLevelError
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BotAPI

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

func NewBotAPI

func NewBotAPI(token string) *BotAPI

func (*BotAPI) BanChatSenderChat added in v0.1.0

func (ba *BotAPI) BanChatSenderChat(chat *types.Chat, senderChatID int64) error

func (*BotAPI) Close added in v0.1.0

func (ba *BotAPI) Close() (*types.ApiResponse, error)
func (ba *BotAPI) CreateChatInviteLink(chat *types.Chat, content *method.CreateChatInviteLink) (*types.ChatInviteLink, error)

func (*BotAPI) DecryptPassportData added in v0.1.0

func (b *BotAPI) DecryptPassportData(passportData *types.PassportData) error

func (*BotAPI) DeleteChatPhoto added in v0.1.0

func (ba *BotAPI) DeleteChatPhoto(chat *types.Chat) error

func (*BotAPI) DeleteChatStickerSet added in v0.1.0

func (ba *BotAPI) DeleteChatStickerSet(chat *types.Chat) error

func (*BotAPI) DeleteMessage added in v0.1.0

func (ba *BotAPI) DeleteMessage(chat *types.Chat, messageID int64) error

func (*BotAPI) DeleteMyCommands added in v0.1.0

func (ba *BotAPI) DeleteMyCommands(payload *method.DeleteMyCommands) error

func (*BotAPI) DeleteStickerFromSet added in v0.1.0

func (ba *BotAPI) DeleteStickerFromSet(stickerFileID string) error

func (*BotAPI) DownloadFile added in v0.1.0

func (ba *BotAPI) DownloadFile(file *types.File, dest io.Writer) error
func (ba *BotAPI) EditChatInviteLink(chat *types.Chat, content *method.EditChatInviteLink) (*types.ChatInviteLink, error)
func (ba *BotAPI) ExportChatInviteLink(chat *types.Chat) (*string, error)

func (*BotAPI) GetChat added in v0.1.0

func (ba *BotAPI) GetChat(chatID string) (*types.Chat, error)

func (*BotAPI) GetChatAdministrators added in v0.1.0

func (ba *BotAPI) GetChatAdministrators(chat *types.Chat) (types.ChatMembers, error)

func (*BotAPI) GetChatMember added in v0.1.0

func (ba *BotAPI) GetChatMember(chat *types.Chat, userID int64) (*types.ChatMember, error)

func (*BotAPI) GetChatMemberCount added in v0.1.0

func (ba *BotAPI) GetChatMemberCount(chat *types.Chat) (*int64, error)

func (*BotAPI) GetChatMenuButton added in v0.1.0

func (ba *BotAPI) GetChatMenuButton(chat *types.Chat) (*types.MenuButton, error)

func (*BotAPI) GetFile added in v0.1.0

func (ba *BotAPI) GetFile(fileID string) (*types.File, error)

func (*BotAPI) GetMe

func (ba *BotAPI) GetMe() (*types.User, error)

func (*BotAPI) GetMyCommands added in v0.1.0

func (ba *BotAPI) GetMyCommands(payload *method.GetMyCommands) ([]types.BotCommand, error)

func (*BotAPI) GetMyDefaultAdministratorRights added in v0.1.0

func (ba *BotAPI) GetMyDefaultAdministratorRights(gmdar *method.GetMyDefaultAdministratorRights) (*types.ChatAdministratorRights, error)

func (*BotAPI) GetStickerSet added in v0.1.0

func (ba *BotAPI) GetStickerSet(name string) (*types.StickerSet, error)

func (*BotAPI) GetUpdates

func (ba *BotAPI) GetUpdates(payload myTypes.Sendable) ([]types.Update, error)

func (*BotAPI) Handle

func (ba *BotAPI) Handle(condition Middleware, handler HandlerFunc, additionalMiddlewares ...Middleware)

func (*BotAPI) LeaveChat added in v0.1.0

func (ba *BotAPI) LeaveChat(chat *types.Chat) error

func (*BotAPI) LogOut added in v0.1.0

func (ba *BotAPI) LogOut() (*types.ApiResponse, error)

func (*BotAPI) ProcessUpdate

func (ba *BotAPI) ProcessUpdate(update types.Update)
func (ba *BotAPI) RevokeChatInviteLink(chat *types.Chat, content *method.RevokeChatInviteLink) (*types.ChatInviteLink, error)

func (*BotAPI) Send

func (ba *BotAPI) Send(reciever interface{}, payload myTypes.Sendable) (*types.ApiResponse, error)

func (*BotAPI) SetChatAdministratorCustomTitle added in v0.1.0

func (ba *BotAPI) SetChatAdministratorCustomTitle(chat *types.Chat, userID int64, title string) error

func (*BotAPI) SetChatMenuButton added in v0.1.0

func (ba *BotAPI) SetChatMenuButton(button *method.SetChatMenuButton) error

func (*BotAPI) SetChatPermissions added in v0.1.0

func (ba *BotAPI) SetChatPermissions(chat *types.Chat, permissions *types.ChatPermissions) error

func (*BotAPI) SetChatStickerSet added in v0.1.0

func (ba *BotAPI) SetChatStickerSet(chat *types.Chat, stickerSetName string) error

func (*BotAPI) SetMyCommands added in v0.1.0

func (ba *BotAPI) SetMyCommands(payload *method.SetMyCommands) error

func (*BotAPI) SetMyDefaultAdministratorRights added in v0.1.0

func (ba *BotAPI) SetMyDefaultAdministratorRights(smdar *method.SetMyDefaultAdministratorRights) error

func (*BotAPI) SetStickerPositionInSet added in v0.1.0

func (ba *BotAPI) SetStickerPositionInSet(stickerFileID string, position int64) error

func (*BotAPI) SetWebhook added in v1.0.1

func (ba *BotAPI) SetWebhook(payload *method.SetWebhook) error

func (*BotAPI) Start

func (b *BotAPI) Start()

func (*BotAPI) Stop added in v1.0.0

func (b *BotAPI) Stop()

Stop gracefully shuts the poller down.

func (*BotAPI) TestEnvironment

func (ba *BotAPI) TestEnvironment()

func (*BotAPI) UnBanChatSenderChat added in v0.1.0

func (ba *BotAPI) UnBanChatSenderChat(chat *types.Chat, senderChatID int64) error

func (*BotAPI) UploadStickerFile added in v0.1.0

func (ba *BotAPI) UploadStickerFile(file *method.UploadStickerFile) (*types.File, error)

func (*BotAPI) WithLogger

func (ba *BotAPI) WithLogger(logger *log.Logger) *BotAPI

func (*BotAPI) WithPrivateKey

func (ba *BotAPI) WithPrivateKey(privKey rsa.PrivateKey) *BotAPI

type Context

type Context interface {
	// Bot returns the bot instance.
	Bot() *BotAPI

	Send(to interface{}, paylad myTypes.Sendable) (*types.ApiResponse, error)

	// Update returns the original update.
	Update() types.Update

	// Message - New incoming message of any kind — text, photo, sticker, etc.
	Message() *types.Message

	// EditedMessage - New version of a message that is known to the bot and was edited
	EditedMessage() *types.Message

	// ChannelPost - New incoming channel post of any kind — text, photo, sticker, etc.
	ChannelPost() *types.Message

	// ChannelPostEdited - New version of a channel post that is known to the bot and was edited
	ChannelPostEdited() *types.Message

	// InlineQuery - New incoming inline query
	InlineQuery() *types.InlineQuery

	// InlineQuery - New incoming inline query
	ChoosenInlineResult() *types.ChosenInlineResult

	// Callback returns stored callback if such presented.
	CallbackQuery() *types.CallbackQuery

	// ShippingQuery returns stored shipping query if such presented.
	ShippingQuery() *types.ShippingQuery

	// PreCheckoutQuery returns stored pre checkout query if such presented.
	PreCheckoutQuery() *types.PreCheckoutQuery

	// Poll returns stored poll if such presented.
	Poll() *types.Poll

	// PollAnswer returns stored poll answer if such presented.
	PollAnswer() *types.PollAnswer

	// MyChatMember - The bot's chat member status was updated in a chat.
	// For private chats, this update is received only when the bot is blocked or unblocked by the user.
	MyChatMember() *types.ChatMemberUpdated

	// ChatMember - A chat member's status was updated in a chat.
	// The bot must be an administrator in the chat and must explicitly specify “chat_member” in the list of allowed_updates to receive these updates.
	ChatMember() *types.ChatMemberUpdated

	// ChatJoinRequest
	ChatJoinRequest() *types.ChatJoinRequest

	// User returns the current recipient, depending on the context type.
	// Returns nil if user is not presented.
	Sender() *types.User

	// Chat returns the current chat, depending on the context type.
	// Returns nil if chat is not presented.
	Chat() *types.Chat

	// after this line goes "bot embedded" methods
	SetWebhook(payload *method.SetWebhook) error
	GetFile(fileID string) (*types.File, error)
	DownloadFile(file *types.File, dest io.Writer) error
	LeaveChat(chat *types.Chat) error
	GetChat(chatID string) (*types.Chat, error)
	GetChatAdministrators(chat *types.Chat) (types.ChatMembers, error)
	DeleteChatPhoto(chat *types.Chat) error
	CreateChatInviteLink(chat *types.Chat, content *method.CreateChatInviteLink) (*types.ChatInviteLink, error)
	EditChatInviteLink(chat *types.Chat, content *method.EditChatInviteLink) (*types.ChatInviteLink, error)
	RevokeChatInviteLink(chat *types.Chat, content *method.RevokeChatInviteLink) (*types.ChatInviteLink, error)
	DeleteMessage(chat *types.Chat, messageID int64) error
	SetChatStickerSet(chat *types.Chat, stickerSetName string) error
	DeleteChatStickerSet(chat *types.Chat) error
	GetChatMember(chat *types.Chat, userID int64) (*types.ChatMember, error)
	GetChatMemberCount(chat *types.Chat) (*int64, error)
	GetStickerSet(name string) (*types.StickerSet, error)
	ExportChatInviteLink(chat *types.Chat) (*string, error)
	SetChatAdministratorCustomTitle(chat *types.Chat, userID int64, title string) error
	BanChatSenderChat(chat *types.Chat, senderChatID int64) error
	UnBanChatSenderChat(chat *types.Chat, senderChatID int64) error
	SetChatPermissions(chat *types.Chat, permissions *types.ChatPermissions) error
	SetMyCommands(payload *method.SetMyCommands) error
	DeleteMyCommands(payload *method.DeleteMyCommands) error
	GetMyCommands(payload *method.GetMyCommands) ([]types.BotCommand, error)
	SetChatMenuButton(button *method.SetChatMenuButton) error
	// pass nil to chat for get default one
	GetChatMenuButton(chat *types.Chat) (*types.MenuButton, error)
	SetMyDefaultAdministratorRights(smdar *method.SetMyDefaultAdministratorRights) error
	GetMyDefaultAdministratorRights(gmdar *method.GetMyDefaultAdministratorRights) (*types.ChatAdministratorRights, error)
	UploadStickerFile(file *method.UploadStickerFile) (*types.File, error)
	SetStickerPositionInSet(stickerFileID string, position int64) error
	DeleteStickerFromSet(stickerFileID string) error

	DecryptPassportData(passportData *types.PassportData) error
}

type HandlerFunc

type HandlerFunc func(Context) error

type Middleware

type Middleware func(HandlerFunc) HandlerFunc

type Poller

type Poller interface {
	Poll(b *BotAPI, dest chan types.Update, stop chan struct{})
}

func NewLongPoller

func NewLongPoller() Poller

Directories

Path Synopsis
passport command

Jump to

Keyboard shortcuts

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