gogram

package module
v0.0.0-...-cc90098 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2019 License: MIT Imports: 12 Imported by: 0

README

gogram

Telegram bot framework.

  import (
    "github.com/irisked/gogram/args"
    "github.com/irisked/gogram/option"
    "github.com/irisked/gogram/telegram/keyboard/inline"
    "github.com/irisked/gogram"
  )
	bot := gogram.New("<token>")
	bot.OnMessage(func (ctx *gogram.Context) {
		ctx.Reply(args.Text("test response"))
	})
	bot.OnEditedMessage(func (ctx *gogram.Context) {
		ctx.Reply(args.Text("test edited response"))
	})
	bot.OnSticker(func (ctx *gogram.Context) {
		kb := inline.NewKeyboard(
			inline.NewButton("test", inline.Callback("test-callback", "1")),
			inline.NewButton("test2", inline.Callback("test", "1")),
			inline.NewButton("test2", inline.Callback("test-callback1", "1")),
		)
		ctx.Reply(args.Text("stiker response"), option.Keyboard(kb))
	})
	bot.OnCommand("test", func (ctx *gogram.Context) {
		ctx.Reply(args.Markdown("```test```"))
	})
	bot.OnCallbackQuery(func (ctx *gogram.Context) {
		ctx.Reply(args.Markdown("```test-generic```"))
	})
	bot.OnCallback("test-callback", func (ctx *gogram.Context) {
		ctx.Reply(args.Markdown("```test-callback```"))
	})
	bot.OnCallback("test-callback1", func (ctx *gogram.Context) {
		ctx.Reply(args.Markdown("```test-callback1```"))
	})
	bot.StartPolling()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	State map[string]interface{}
	// contains filtered or unexported fields
}

Context its gogram context.

func NewContext

func NewContext(telegram telegram.Telegram, update types.Update) *Context

NewContext creates context.

func (*Context) AnswerCallbackQuery

func (c *Context) AnswerCallbackQuery(options ...method.AnswerCallbackQueryOption) (bool, error)

AnswerCallbackQuery performs answerCallbackQuery telegram api method.

options:

option.Text(text string), option.ShowAlert(), option.URL(url string), option.CacheTime(time int)

Reference: https://core.telegram.org/bots/api#answercallbackquery

func (*Context) CallbackQuery

func (c *Context) CallbackQuery() *types.CallbackQuery

CallbackQuery returns edited message.

func (*Context) ChannelPost

func (c *Context) ChannelPost() *types.Message

ChannelPost returns edited message.

func (*Context) Chat

func (c *Context) Chat() *types.Chat

Chat returns chat.

func (*Context) ChatID

func (c *Context) ChatID() int64

ChatID returns chat id.

func (*Context) ChosenInlineResult

func (c *Context) ChosenInlineResult() *types.ChosenInlineResult

ChosenInlineResult returns edited message.

func (*Context) DeleteChatPhoto

func (c *Context) DeleteChatPhoto() (bool, error)

DeleteChatPhoto performs deleteChatPhoto telegram api method.

Reference: https://core.telegram.org/bots/api#deletechatphoto

func (*Context) DeleteChatStickerSet

func (c *Context) DeleteChatStickerSet() (bool, error)

DeleteChatStickerSet performs deleteChatStickerSet telegram api method.

Reference: https://core.telegram.org/bots/api#deletechatstickerset

func (*Context) DeleteMessage

func (c *Context) DeleteMessage(messageID int) (bool, error)

DeleteMessage performs deleteMessage telegram api method.

https://core.telegram.org/bots/api#deletemessage

func (*Context) EditMessageCaption

func (c *Context) EditMessageCaption(caption args.TextData, options ...method.EditMessageCaptionOption) (*types.Message, error)

EditMessageCaption performs editMessageCaption telegram api method. Returns pointer to the message, if message was sent by the bot or nil-pointer otherwise.

options:

option.Keyboard(keyboard markup.Keyboard)

Reference: https://core.telegram.org/bots/api#editmessagecaption

func (*Context) EditMessageMedia

func (c *Context) EditMessageMedia(media args.InputMediaConfig, options ...method.EditMessageMediaOption) (*types.Message, error)

EditMessageMedia performs editMessageMedia telegram api method. Returns pointer to the message, if message was sent by the bot or nil-pointer otherwise.

options:

option.Keyboard(keyboard markup.Keyboard)

Reference: https://core.telegram.org/bots/api#editmessagemedia

func (*Context) EditMessageReplyMarkup

func (c *Context) EditMessageReplyMarkup(kb keyboard.Keyboard) (*types.Message, error)

EditMessageReplyMarkup performs editMessageReplyMarkup telegram api method. Returns pointer to the message, if message was sent by the bot or nil-pointer otherwise.

options:

option.Keyboard(keyboard markup.Keyboard)

Reference: https://core.telegram.org/bots/api#editmessagereplymarkup

func (*Context) EditMessageText

func (c *Context) EditMessageText(text args.TextData, options ...method.EditMessageTextOption) (*types.Message, error)

EditMessageText performs editMessageText telegram api method. Returns pointer to the message, if message was sent by the bot or nil-pointer otherwise.

options:

option.DisableWebPagePreview(), option.Keyboard(keyboard markup.Keyboard)

Reference: https://core.telegram.org/bots/api#editmessagetext

func (*Context) EditedChannelPost

func (c *Context) EditedChannelPost() *types.Message

EditedChannelPost returns edited message.

func (*Context) EditedMessage

func (c *Context) EditedMessage() *types.Message

EditedMessage returns edited message.

func (c *Context) ExportChatInviteLink() (string, error)

ExportChatInviteLink performs exportChatInviteLink telegram api method.

Reference: https://core.telegram.org/bots/api#exportchatinvitelink

func (*Context) From

func (c *Context) From() *types.User

From returns user that sends an update.

func (*Context) GetChat

func (c *Context) GetChat() (types.Chat, error)

GetChat performs getChat telegram api method.

Reference: https://core.telegram.org/bots/api#getchat

func (*Context) GetChatAdministrators

func (c *Context) GetChatAdministrators() (types.ChatMember, error)

GetChatAdministrators performs getChatAdministrators telegram api method.

Reference: https://core.telegram.org/bots/api#getchatadministrators

func (*Context) GetChatMember

func (c *Context) GetChatMember(userID int) (types.ChatMember, error)

GetChatMember performs getChatMember telegram api method.

Reference: https://core.telegram.org/bots/api#getchatmember

func (*Context) GetChatMembersCount

func (c *Context) GetChatMembersCount() (int, error)

GetChatMembersCount performs getChatMembersCount telegram api method.

Reference: https://core.telegram.org/bots/api#getchatmemberscount

func (*Context) InlineQuery

func (c *Context) InlineQuery() *types.InlineQuery

InlineQuery returns edited message.

func (*Context) KickChatMember

func (c *Context) KickChatMember(userID int, options ...method.KickChatMemberOption) (bool, error)

KickChatMember performs kickChatMember telegram api method.

options:

option.UntilDate(date int)

Reference: https://core.telegram.org/bots/api#kickchatmember TODO: swap userID <-> ChatID in params

func (*Context) LeaveChat

func (c *Context) LeaveChat() (bool, error)

LeaveChat performs leaveChat telegram api method.

Reference: https://core.telegram.org/bots/api#leavechat

func (*Context) Message

func (c *Context) Message() *types.Message

Message returns message.

func (*Context) PinChatMessage

func (c *Context) PinChatMessage(messageID int, options ...method.PinChatMessageOption) (bool, error)

PinChatMessage performs pinChatMessage telegram api method.

Reference: https://core.telegram.org/bots/api#pinchatmessage

func (*Context) PreCheckoutQuery

func (c *Context) PreCheckoutQuery() *types.PreCheckoutQuery

PreCheckoutQuery returns edited message.

func (*Context) PromoteChatMember

func (c *Context) PromoteChatMember(userID int, options ...method.PromoteChatMemberOption) (bool, error)

PromoteChatMember performs promoteChatMember telegram api method.

options:

option.CanChangeInfo(), option.CanPostMessages(), option.CanDeleteMessages(), option.CanEditMessages() option.CanInviteUsers(), option.CanRestrictUsers(), option.CanPinMessages(), option.CanPromoteMembers()

Reference: https://core.telegram.org/bots/api#promotechatmember

func (*Context) Reply

func (c *Context) Reply(text args.TextData, options ...method.SendMessageOption) (types.Message, error)

Reply performs sendMessage telegram api method.

options:

option.DisableWebPagePreview(), option.DisableNotification() option.ReplyToMessageID(id int), option.Keyboard(keyboard markup.Keyboard)

Reference: https://core.telegram.org/bots/api#sendmessage

func (*Context) ReplyWithAnimation

func (c *Context) ReplyWithAnimation(animation *args.AnimationConfig, options ...method.SendAnimationOption) (types.Message, error)

ReplyWithAnimation performs sendAnimation telegram api method.

options:

option.DisableWebPagePreview(), option.ReplyToMessageID(id int), option.Keyboard(keyboard markup.Keyboard)

Reference: https://core.telegram.org/bots/api#sendanimation

func (*Context) ReplyWithAudio

func (c *Context) ReplyWithAudio(audio *args.AudioConfig, options ...method.SendAudioOption) (types.Message, error)

ReplyWithAudio performs sendAudio telegram api method.

options:

option.DisableWebPagePreview(), option.ReplyToMessageID(id int), option.Keyboard(keyboard markup.Keyboard)

Reference: https://core.telegram.org/bots/api#sendaudio

func (*Context) ReplyWithChatAction

func (c *Context) ReplyWithChatAction(action types.ChatAction) (types.Message, error)

ReplyWithChatAction performs sendChatAction telegram api method.

Reference: https://core.telegram.org/bots/api#sendchataction

func (*Context) ReplyWithContact

func (c *Context) ReplyWithContact(contact types.Contact, options ...method.SendContactOption) (types.Message, error)

ReplyWithContact performs sendContact telegram api method.

options:

option.DisableWebPagePreview(), option.ReplyToMessageID(id int), option.Keyboard(keyboard markup.Keyboard)

Reference: https://core.telegram.org/bots/api#sendcontact

func (*Context) ReplyWithDocument

func (c *Context) ReplyWithDocument(document *args.DocumentConfig, options ...method.SendDocumentOption) (types.Message, error)

ReplyWithDocument performs sendDocument telegram api method.

options:

option.DisableWebPagePreview(), option.ReplyToMessageID(id int), option.Keyboard(keyboard markup.Keyboard)

Reference: https://core.telegram.org/bots/api#senddocument

func (*Context) ReplyWithLocation

func (c *Context) ReplyWithLocation(location types.Location, options ...method.SendLocationOption) (types.Message, error)

ReplyWithLocation performs sendLocation telegram api method.

options:

option.DisableWebPagePreview(), option.ReplyToMessageID(id int), option.Keyboard(keyboard markup.Keyboard)

Reference: https://core.telegram.org/bots/api#sendlocation

func (*Context) ReplyWithMediaGroup

func (c *Context) ReplyWithMediaGroup(mediaGroup []args.MediaGroupConfig, options ...method.SendMediaGroupOption) (types.Message, error)

ReplyWithMediaGroup performs sendMediaGroup telegram api method.

options:

option.DisableWebPagePreview(), option.ReplyToMessageID(id int),

Reference: https://core.telegram.org/bots/api#sendmediagroup

func (*Context) ReplyWithPhoto

func (c *Context) ReplyWithPhoto(photo *args.PhotoConfig, options ...method.SendPhotoOption) (types.Message, error)

ReplyWithPhoto performs sendPhoto telegram api method.

options:

option.DisableWebPagePreview(), option.ReplyToMessageID(id int), option.Keyboard(keyboard markup.Keyboard)

Reference: https://core.telegram.org/bots/api#sendphoto

func (*Context) ReplyWithVenue

func (c *Context) ReplyWithVenue(venue types.Venue, options ...method.SendVenueOption) (types.Message, error)

ReplyWithVenue performs sendVenue telegram api method.

options:

option.DisableWebPagePreview(), option.ReplyToMessageID(id int), option.Keyboard(keyboard markup.Keyboard), option.Foursquare(id, type string)

Reference: https://core.telegram.org/bots/api#sendvenue

func (*Context) ReplyWithVideo

func (c *Context) ReplyWithVideo(video *args.VideoConfig, options ...method.SendVideoOption) (types.Message, error)

ReplyWithVideo performs sendVideo telegram api method.

options:

option.DisableWebPagePreview(), option.ReplyToMessageID(id int), option.Keyboard(keyboard markup.Keyboard)

Reference: https://core.telegram.org/bots/api#sendvideo

func (*Context) ReplyWithVideoNote

func (c *Context) ReplyWithVideoNote(videoNote *args.VideoNoteConfig, options ...method.SendVideoNoteOption) (types.Message, error)

ReplyWithVideoNote performs sendVideoNote telegram api method.

options:

option.DisableWebPagePreview(), option.ReplyToMessageID(id int), option.Keyboard(keyboard markup.Keyboard)

Reference: https://core.telegram.org/bots/api#sendvideonote

func (*Context) ReplyWithVoice

func (c *Context) ReplyWithVoice(voice *args.VoiceConfig, options ...method.SendVoiceOption) (types.Message, error)

ReplyWithVoice performs sendVoice telegram api method.

options:

option.DisableWebPagePreview(), option.ReplyToMessageID(id int), option.Keyboard(keyboard markup.Keyboard)

Reference: https://core.telegram.org/bots/api#sendvoice

func (*Context) RestrictChatMember

func (c *Context) RestrictChatMember(userID int, options ...method.RestrictChatMemberOption) (bool, error)

RestrictChatMember performs restrictChatMember telegram api method.

options:

option.CanSendMessages(), options.CanSendMediaMessages(), option.CanSendOtherMessages(), option.CanAddWebPagePreview()

Reference: https://core.telegram.org/bots/api#restrictchatmember

func (*Context) SetChatDescription

func (c *Context) SetChatDescription(description string) (bool, error)

SetChatDescription performs setChatDescription telegram api method.

Reference: https://core.telegram.org/bots/api#setchatdescription

func (*Context) SetChatPhoto

func (c *Context) SetChatPhoto(file *os.File) (bool, error)

SetChatPhoto performs setChatPhoto telegram api method.

Reference: https://core.telegram.org/bots/api#setchatphoto

func (*Context) SetChatStickerSet

func (c *Context) SetChatStickerSet(stickerSetName string) (bool, error)

SetChatStickerSet performs setChatStickerSet telegram api method.

Reference: https://core.telegram.org/bots/api#setchatstickerset

func (*Context) SetChatTitle

func (c *Context) SetChatTitle(title string) (bool, error)

SetChatTitle performs setChatTitle telegram api method.

Reference: https://core.telegram.org/bots/api#setchattitle

func (*Context) ShippingQuery

func (c *Context) ShippingQuery() *types.ShippingQuery

ShippingQuery returns edited message.

func (*Context) Telegram

func (c *Context) Telegram() telegram.Telegram

Telegram returns telegram.

func (*Context) UnbanChatMember

func (c *Context) UnbanChatMember(userID int) (bool, error)

UnbanChatMember performs unbanChatMember telegram api method.

options:

option.UntilDate(date int)

Reference: https://core.telegram.org/bots/api#unbanchatmember

func (*Context) UnpinChatMessage

func (c *Context) UnpinChatMessage() (bool, error)

UnpinChatMessage performs unpinChatMessage telegram api method.

Reference: https://core.telegram.org/bots/api#unpinchatmessage

func (*Context) Update

func (c *Context) Update() types.Update

Update returns raw update.

type Dispatcher

type Dispatcher interface {
	OnPath(string, Handler)
	OnUpdate(types.UpdateType, Handler)
	OnCallbackQuery(Handler)
	OnCallback(string, Handler)
	OnInlineQuery(Handler)
	OnShippingQuery(Handler)
	OnPreCheckoutQuery(Handler)
	OnChosenInlineResult(Handler)
	OnMessage(Handler)
	OnEditedMessage(Handler)
	OnText(Handler)
	OnAudio(Handler)
	OnDocument(Handler)
	OnAnimation(Handler)
	OnPhoto(Handler)
	OnSticker(Handler)
	OnVideo(Handler)
	OnVideoNote(Handler)
	OnVoice(Handler)
	OnContact(Handler)
	OnLocation(Handler)
	OnVenue(Handler)
	OnNewChatMembers(Handler)
	OnLeftChatMember(Handler)
	OnNewChatPhoto(Handler)
	OnDeleteChatPhoto(Handler)
	OnInvoice(Handler)
	OnSuccessfulPayment(Handler)
	OnCommand(string, Handler)
	OnStart(Handler)
	OnHelp(Handler)
	Handle(*Context)
}

Dispatcher interface

func NewDispatcher

func NewDispatcher() Dispatcher

NewDispatcher creates dispatcher

type Gogram

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

Gogram its a telegram bot framework

func New

func New(token string) *Gogram

New creates Gogram

func (*Gogram) OnAnimation

func (g *Gogram) OnAnimation(handler Handler)

OnAnimation sets handler for animation message.

func (*Gogram) OnAudio

func (g *Gogram) OnAudio(handler Handler)

OnAudio sets handler for audio message.

func (*Gogram) OnCallback

func (g *Gogram) OnCallback(path string, handler Handler)

OnCallback sets handler for specific callback query.

func (*Gogram) OnCallbackQuery

func (g *Gogram) OnCallbackQuery(handler Handler)

OnCallbackQuery sets handler for callback query.

func (*Gogram) OnChosenInlineResult

func (g *Gogram) OnChosenInlineResult(handler Handler)

OnChosenInlineResult sets handler for chosen inline result.

func (*Gogram) OnCommand

func (g *Gogram) OnCommand(command string, handler Handler)

OnCommand sets handler for command message.

func (*Gogram) OnContact

func (g *Gogram) OnContact(handler Handler)

OnContact sets handler for contact message.

func (*Gogram) OnDeleteChatPhoto

func (g *Gogram) OnDeleteChatPhoto(handler Handler)

OnDeleteChatPhoto sets handler for delete chat photo.

func (*Gogram) OnDocument

func (g *Gogram) OnDocument(handler Handler)

OnDocument sets handler for document message.

func (*Gogram) OnEditedMessage

func (g *Gogram) OnEditedMessage(handler Handler)

OnEditedMessage sets handler for edited message.

func (*Gogram) OnHelp

func (g *Gogram) OnHelp(handler Handler)

OnHelp sets handler for help command.

func (*Gogram) OnInlineQuery

func (g *Gogram) OnInlineQuery(handler Handler)

OnInlineQuery sets handler for inline query.

func (*Gogram) OnInvoice

func (g *Gogram) OnInvoice(handler Handler)

OnInvoice sets handler for invoice.

func (*Gogram) OnLeftChatMember

func (g *Gogram) OnLeftChatMember(handler Handler)

OnLeftChatMember sets handler for left chat member.

func (*Gogram) OnLocation

func (g *Gogram) OnLocation(handler Handler)

OnLocation sets handler for location message.

func (*Gogram) OnMessage

func (g *Gogram) OnMessage(handler Handler)

OnMessage sets handler for message.

func (*Gogram) OnNewChatMembers

func (g *Gogram) OnNewChatMembers(handler Handler)

OnNewChatMembers sets handler for new chat members.

func (*Gogram) OnNewChatPhoto

func (g *Gogram) OnNewChatPhoto(handler Handler)

OnNewChatPhoto sets handler for new chat photo.

func (*Gogram) OnPath

func (g *Gogram) OnPath(path string, handler Handler)

OnPath sets handler for path.

func (*Gogram) OnPhoto

func (g *Gogram) OnPhoto(handler Handler)

OnPhoto sets handler for photo message.

func (*Gogram) OnPreCheckoutQuery

func (g *Gogram) OnPreCheckoutQuery(handler Handler)

OnPreCheckoutQuery sets handler for pre checkout query.

func (*Gogram) OnShippingQuery

func (g *Gogram) OnShippingQuery(handler Handler)

OnShippingQuery sets handler for shipping query.

func (*Gogram) OnStart

func (g *Gogram) OnStart(handler Handler)

OnStart sets handler for start command.

func (*Gogram) OnSticker

func (g *Gogram) OnSticker(handler Handler)

OnSticker sets handler for sticker message.

func (*Gogram) OnSuccessfulPayment

func (g *Gogram) OnSuccessfulPayment(handler Handler)

OnSuccessfulPayment sets handler for successfull payment.

func (*Gogram) OnText

func (g *Gogram) OnText(handler Handler)

OnText sets handler for text message.

func (*Gogram) OnUpdate

func (g *Gogram) OnUpdate(ut types.UpdateType, handler Handler)

OnUpdate sets handler for update type.

func (*Gogram) OnVenue

func (g *Gogram) OnVenue(handler Handler)

OnVenue sets handler for venue message.

func (*Gogram) OnVideo

func (g *Gogram) OnVideo(handler Handler)

OnVideo sets handler for video message.

func (*Gogram) OnVideoNote

func (g *Gogram) OnVideoNote(handler Handler)

OnVideoNote sets handler for video note message.

func (*Gogram) OnVoice

func (g *Gogram) OnVoice(handler Handler)

OnVoice sets handler for voice message.

func (*Gogram) StartPolling

func (g *Gogram) StartPolling()

StartPolling starts bot using long poling.

func (*Gogram) StopPolling

func (g *Gogram) StopPolling()

StopPolling stops long polling.

type Handler

type Handler func(ctx *Context)

Handler function.

type Poller

type Poller interface {
	StartPolling(chan<- types.Update) chan<- interface{}
	IsPolling() bool
}

Poller interface.

func NewPoller

func NewPoller(tg *telegram.Telegram, limit int) Poller

NewPoller creates poller.

type Storage

type Storage interface {
	Insert(string, Handler)
	Find(string) (Handler, bool)
	Map() map[string]Handler
}

Storage interface

func NewStorage

func NewStorage() Storage

NewStorage creates storage

type Tree

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

Tree implements a radix tree. This can be treated as a Dictionary abstract data type. The main advantage over a standard hash map is prefix-based lookups and ordered iteration,

func NewTree

func NewTree() *Tree

NewTree returns an empty Tree

func NewTreeFromMap

func NewTreeFromMap(m map[string]Handler) *Tree

NewTreeFromMap returns a new tree containing the keys from an existing map

func (*Tree) Delete

func (t *Tree) Delete(s string) (Handler, bool)

Delete is used to delete a key, returning the previous value and if it was deleted

func (*Tree) DeletePrefix

func (t *Tree) DeletePrefix(s string) int

DeletePrefix is used to delete the subtree under a prefix Returns how many nodes were deleted Use this to delete large subtrees efficiently

func (*Tree) Get

func (t *Tree) Get(s string) (Handler, bool)

Get is used to lookup a specific key, returning the value and if it was found

func (*Tree) Insert

func (t *Tree) Insert(s string, v Handler) (Handler, bool)

Insert is used to add a newentry or update an existing entry. Returns if updated.

func (*Tree) Len

func (t *Tree) Len() int

Len is used to return the number of elements in the tree

func (*Tree) LongestPrefix

func (t *Tree) LongestPrefix(s string) (string, Handler, bool)

LongestPrefix is like Get, but instead of an exact match, it will return the longest prefix match.

func (*Tree) Maximum

func (t *Tree) Maximum() (string, Handler, bool)

Maximum is used to return the maximum value in the tree

func (*Tree) Minimum

func (t *Tree) Minimum() (string, Handler, bool)

Minimum is used to return the minimum value in the tree

func (*Tree) ToMap

func (t *Tree) ToMap() map[string]Handler

ToMap is used to walk the tree and convert it into a map

func (*Tree) Walk

func (t *Tree) Walk(fn WalkFn)

Walk is used to walk the tree

func (*Tree) WalkPath

func (t *Tree) WalkPath(path string, fn WalkFn)

WalkPath is used to walk the tree, but only visiting nodes from the root down to a given leaf. Where WalkPrefix walks all the entries *under* the given prefix, this walks the entries *above* the given prefix.

func (*Tree) WalkPrefix

func (t *Tree) WalkPrefix(prefix string, fn WalkFn)

WalkPrefix is used to walk the tree under a prefix

type WalkFn

type WalkFn func(s string, v Handler) bool

WalkFn is used when walking the tree. Takes a key and value, returning if iteration should be terminated.

Directories

Path Synopsis
method
Package method implements all available telegram bot api methods.
Package method implements all available telegram bot api methods.

Jump to

Keyboard shortcuts

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