tgbot

package module
v0.0.0-...-1f93e80 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2021 License: MIT Imports: 10 Imported by: 0

README

TGBot

Go Reference

Go library for the Telegram Bot API.

Documentation

Overview

Package tgbot provides access to the Telegram Bot API.

Example
package main

import (
	"fmt"

	"github.com/fdschonborn/tgbot"
)

var bot *tgbot.Bot

func main() {
	me, _ := bot.GetMe()

	fmt.Println(me.IsBot)
}
Output:

true

Index

Examples

Constants

View Source
const (
	DefaultHost = "api.telegram.org"
)

Variables

View Source
var (
	DefaultClient = http.DefaultClient
)

Functions

This section is empty.

Types

type Bot

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

func New

func New(token string, options *BotOptions) (*Bot, error)

func (*Bot) Close

func (b *Bot) Close() error

func (*Bot) CopyMessage

func (b *Bot) CopyMessage(target int64, from int64, messageID int, options *CopyMessageOptions) (*MessageID, error)

func (*Bot) ForwardMessage

func (b *Bot) ForwardMessage(target int64, from int64, messageID int, options *ForwardMessageOptions) (*Message, error)

func (*Bot) GetChat

func (b *Bot) GetChat(target int64) (*Chat, error)
Example
package main

import (
	"fmt"

	"github.com/fdschonborn/tgbot"
)

var (
	bot    *tgbot.Bot
	target int64
)

func main() {
	chat, _ := bot.GetChat(target)

	fmt.Println(chat.ID != 0)
}
Output:

true

func (*Bot) GetMe

func (b *Bot) GetMe() (*User, error)

func (*Bot) GetUpdates

func (b *Bot) GetUpdates(options *GetUpdatesOptions) ([]*Update, error)
Example
package main

import (
	"fmt"

	"github.com/fdschonborn/tgbot"
)

var bot *tgbot.Bot

func main() {
	updates, _ := bot.GetUpdates(&tgbot.GetUpdatesOptions{
		Limit: 1,
	})

	fmt.Println(updates != nil)
}
Output:

true

func (*Bot) LogOut

func (b *Bot) LogOut() error

func (*Bot) PollUpdates

func (b *Bot) PollUpdates(options *GetUpdatesOptions) UpdateChan
Example
package main

import (
	"fmt"

	"github.com/fdschonborn/tgbot"
)

var bot *tgbot.Bot

func main() {
	channel := bot.PollUpdates(nil)

	for update := range channel {
		if update == nil {
			continue
		}

		fmt.Println(update.ID)
	}
}
Output:

func (*Bot) SendChatAction

func (b *Bot) SendChatAction(target int64, action ChatAction) error
Example
package main

import (
	"fmt"

	"github.com/fdschonborn/tgbot"
)

var (
	bot    *tgbot.Bot
	target int64
)

func main() {
	err := bot.SendChatAction(target, tgbot.ChatActionTyping)

	fmt.Println(err == nil)
}
Output:

true

func (*Bot) SendDice

func (b *Bot) SendDice(target int64, options *SendDiceOptions) (*Message, error)
Example
package main

import (
	"fmt"

	"github.com/fdschonborn/tgbot"
)

var (
	bot    *tgbot.Bot
	target int64
)

func main() {
	message, _ := bot.SendDice(target, &tgbot.SendDiceOptions{
		DisableNotification: true,
	})

	fmt.Println(message.Dice.Emoji == tgbot.DiceEmojiDefault)
}
Output:

true

func (*Bot) SendMessage

func (b *Bot) SendMessage(target int64, text string, options *SendMessageOptions) (*Message, error)
Example
package main

import (
	"fmt"

	"github.com/fdschonborn/tgbot"
)

var (
	bot    *tgbot.Bot
	target int64
)

func main() {
	message, _ := bot.SendMessage(target, "Hello!", &tgbot.SendMessageOptions{
		DisableNotification: true,
	})

	fmt.Println(message.Text == "Hello!")
}
Output:

true
Example (WithKeyboard)
package main

import (
	"fmt"

	"github.com/fdschonborn/tgbot"
)

var (
	bot    *tgbot.Bot
	target int64
)

func main() {
	message, _ := bot.SendMessage(target, "Hello!", &tgbot.SendMessageOptions{
		DisableNotification: true,
		ReplyMarkup: &tgbot.InlineKeyboardMarkup{
			InlineKeyboard: [][]*tgbot.InlineKeyboardButton{
				{
					{
						Text: "One",
						URL:  "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
					},
					{
						Text: "Dos",
						URL:  "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
					},
					{
						Text: "Tre",
						URL:  "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
					},
				},
			},
		},
	})

	fmt.Println(message.Text == "Hello!")
}
Output:

true

type BotOptions

type BotOptions struct {
	Client *http.Client
	Host   string
}

type Chat

type Chat struct {
	ID                    int64            `json:"id,omitempty"`
	Type                  ChatType         `json:"type,omitempty"`
	Title                 string           `json:"title,omitempty"`                    // Optional
	Username              string           `json:"username,omitempty"`                 // Optional
	FirstName             string           `json:"first_name,omitempty"`               // Optional
	LastName              string           `json:"last_name,omitempty"`                // Optional
	Photo                 *ChatPhoto       `json:"photo,omitempty"`                    // Optional
	Bio                   string           `json:"bio,omitempty"`                      // Optional
	Description           string           `json:"description,omitempty"`              // Optional
	InviteLink            string           `json:"invite_link,omitempty"`              // Optional
	PinnedMessage         *Message         `json:"pinned_message,omitempty"`           // Optional
	Permissions           *ChatPermissions `json:"permissions,omitempty"`              // Optional
	SlowModeDelay         int              `json:"slow_mode_delay,omitempty"`          // Optional
	MessageAutoDeleteTime int              `json:"message_auto_delete_time,omitempty"` // Optional
	StickerSetName        string           `json:"sticker_set_name,omitempty"`         // Optional
	CanSetStickerSet      bool             `json:"can_set_sticker_set,omitempty"`      // Optional
	LinkedChatID          int64            `json:"linked_chat_id,omitempty"`           // Optional
	Location              *ChatLocation    `json:"location,omitempty"`                 // Optional
}

func (*Chat) String

func (c *Chat) String() string

type ChatAction

type ChatAction string
const (
	ChatActionTyping          ChatAction = "typing"
	ChatActionUploadPhoto     ChatAction = "upload_photo"
	ChatActionRecordVideo     ChatAction = "record_video"
	ChatActionUploadVideo     ChatAction = "upload_video"
	ChatActionRecordVoice     ChatAction = "record_voice"
	ChatActionUploadVoice     ChatAction = "upload_voice"
	ChatActionUploadDocument  ChatAction = "upload_document"
	ChatActionFindLocation    ChatAction = "find_location"
	ChatActionRecordVideoNote ChatAction = "record_video_note"
	ChatActionUploadVideoNote ChatAction = "upload_video_note"
)

type ChatLocation

type ChatLocation struct {
	Location *Location `json:"location,omitempty"`
	Address  string    `json:"address,omitempty"`
}

type ChatPermissions

type ChatPermissions struct {
	CanSendMessages       bool `json:"can_send_messages,omitempty"`
	CanSendMediaMessages  bool `json:"can_send_media_messages,omitempty"`
	CanSendPolls          bool `json:"can_send_polls,omitempty"`
	CanSendOtherMessages  bool `json:"can_send_other_messages,omitempty"`
	CanAddWebPagePreviews bool `json:"can_add_web_page_previews,omitempty"`
	CanChangeInfo         bool `json:"can_change_info,omitempty"`
	CanInviteUsers        bool `json:"can_invite_users,omitempty"`
	CanPinMessages        bool `json:"can_pin_messages,omitempty"`
}

type ChatPhoto

type ChatPhoto struct {
	SmallFileID       string `json:"small_file_id,omitempty"`
	SmallFileUniqueID string `json:"small_file_unique_id,omitempty"`
	BigFileID         string `json:"big_file_id,omitempty"`
	BigFileUniqueID   string `json:"big_file_unique_id,omitempty"`
}

type ChatType

type ChatType string
const (
	ChatTypePrivate    ChatType = "private"
	ChatTypeGroup      ChatType = "group"
	ChatTypeSupergroup ChatType = "supergroup"
	ChatTypeChannel    ChatType = "channel"
)

type CopyMessageOptions

type CopyMessageOptions struct {
	Caption                  string          `json:"caption,omitempty"`
	ParseMode                ParseMode       `json:"parse_mode,omitempty"`
	CaptionEntities          []MessageEntity `json:"caption_entities,omitempty"`
	DisableNotification      bool            `json:"disable_notification,omitempty"`
	ReplyToMessageID         int             `json:"reply_to_message_id,omitempty"`
	AllowSendingWithoutReply bool            `json:"allow_sending_without_reply,omitempty"`
	ReplyMarkup              ReplyMarkup     `json:"reply_markup,omitempty"`
}

type Dice

type Dice struct {
	Emoji DiceEmoji `json:"emoji,omitempty"`
	Value int       `json:"value,omitempty"`
}

type DiceEmoji

type DiceEmoji string
const (
	DiceEmojiDefault     DiceEmoji = "🎲"
	DiceEmojiDarts       DiceEmoji = "🎯"
	DiceEmojiBasketball  DiceEmoji = "🏀"
	DiceEmojiFootball    DiceEmoji = "⚽"
	DiceEmojiBowling     DiceEmoji = "🎳"
	DiceEmojiSlotMachine DiceEmoji = "🎰"
)

type Error

type Error struct {
	Description string
	Code        int
	Parameters  *ResponseParameters
}

func (*Error) Error

func (e *Error) Error() string

type ForceReply

type ForceReply struct {
	ForceReply            bool   `json:"force_reply,omitempty"`
	InputFieldPlaceholder string `json:"input_field_placeholder,omitempty"` // Optional
	Selective             bool   `json:"selective,omitempty"`               // Optional
}

type ForwardMessageOptions

type ForwardMessageOptions struct {
	DisableNotification bool `json:"disable_notification,omitempty"`
}

type GetUpdatesOptions

type GetUpdatesOptions struct {
	Offset         int          `json:"offset,omitempty"`
	Limit          int          `json:"limit,omitempty"`
	Timeout        int          `json:"timeout,omitempty"`
	AllowedUpdates []UpdateType `json:"allowed_updates,omitempty"`
}

type InlineKeyboardButton

type InlineKeyboardButton struct {
	Text                         string `json:"text"`
	URL                          string `json:"url,omitempty"`                              // Optional
	CallbackData                 string `json:"callback_data,omitempty"`                    // Optional
	SwitchInlineQuery            string `json:"switch_inline_query,omitempty"`              // Optional
	SwitchInlineQueryCurrentChat string `json:"switch_inline_query_current_chat,omitempty"` // Optional
	Pay                          bool   `json:"pay,omitempty"`                              // Optional

}

type InlineKeyboardMarkup

type InlineKeyboardMarkup struct {
	InlineKeyboard [][]*InlineKeyboardButton `json:"inline_keyboard,omitempty"`
}

type KeyboardButton

type KeyboardButton struct {
	Text            string                 `json:"text,omitempty"`
	RequestContact  bool                   `json:"request_contact,omitempty"`  // Optional
	RequestLocation bool                   `json:"request_location,omitempty"` // Optional
	RequestPoll     KeyboardButtonPollType `json:"request_poll,omitempty"`     // Optional
}

type KeyboardButtonPollType

type KeyboardButtonPollType struct {
	Type string `json:"type,omitempty"`
}

type Location

type Location struct {
	Longitude            float64 `json:"longitude,omitempty"`
	Latitude             float64 `json:"latitude,omitempty"`
	HorizontalAccuracy   float64 `json:"horizontal_accuracy,omitempty"`    // Optional
	LivePeriod           int     `json:"live_period,omitempty"`            // Optional
	Heading              int     `json:"heading,omitempty"`                // Optional
	ProximityAlertRadius int     `json:"proximity_alert_radius,omitempty"` // Optional
}

type Message

type Message struct {
	ID                    int              `json:"message_id,omitempty"`
	From                  *User            `json:"from,omitempty"`        // Optional
	SenderChat            *Chat            `json:"sender_chat,omitempty"` // Optional
	Date                  int              `json:"date,omitempty"`
	Chat                  *Chat            `json:"chat,omitempty"`
	ForwardFrom           *User            `json:"forward_from,omitempty"`            // Optional
	ForwardFromChat       *Chat            `json:"forward_from_chat,omitempty"`       // Optional
	ForwardFromMesageID   int              `json:"forward_from_mesage_id,omitempty"`  // Optional
	ForwardSignature      string           `json:"forward_signature,omitempty"`       // Optional
	ForwardSenderName     string           `json:"forward_sender_name,omitempty"`     // Optional
	ForwardDate           int              `json:"forward_date,omitempty"`            // Optional
	ReplyToMessage        *Message         `json:"reply_to_message,omitempty"`        // Optional
	ViaBot                *User            `json:"via_bot,omitempty"`                 // Optional
	EditDate              int              `json:"edit_date,omitempty"`               // Optional
	MediaGroupID          string           `json:"media_group_id,omitempty"`          // Optional
	AuthorSignature       string           `json:"author_signature,omitempty"`        // Optional
	Text                  string           `json:"text,omitempty"`                    // Optional
	Entities              []*MessageEntity `json:"entities,omitempty"`                // Optional
	Photo                 []*PhotoSize     `json:"photo,omitempty"`                   // Optional
	Caption               string           `json:"caption,omitempty"`                 // Optional
	CaptionEntities       []*MessageEntity `json:"caption_entities,omitempty"`        // Optional
	Dice                  *Dice            `json:"dice,omitempty"`                    // Optional
	Location              *Location        `json:"location,omitempty"`                // Optional
	NewChatMembers        []*User          `json:"new_chat_members,omitempty"`        // Optional
	LeftChatMember        *User            `json:"left_chat_member,omitempty"`        // Optional
	NewChatTitle          string           `json:"new_chat_title,omitempty"`          // Optional
	NewChatPhoto          []*PhotoSize     `json:"new_chat_photo,omitempty"`          // Optional
	DeleteChatPhoto       bool             `json:"delete_chat_photo,omitempty"`       // Optional
	GroupChatCreated      bool             `json:"group_chat_created,omitempty"`      // Optional
	SupergroupChatCreated bool             `json:"supergroup_chat_created,omitempty"` // Optional
	ChannelChatCreated    bool             `json:"channel_chat_created,omitempty"`    // Optional
	MigrateToChatID       int64            `json:"migrate_to_chat_id,omitempty"`      // Optional
	MigrateFromChatID     int64            `json:"migrate_from_chat_id,omitempty"`    // Optional
	PinnedMessage         *Message         `json:"pinned_message,omitempty"`          // Optional
	ConnectedWebsite      string           `json:"connected_website,omitempty"`       // Optional

}

func (*Message) Command

func (m *Message) Command() string

func (*Message) CommandArguments

func (m *Message) CommandArguments() []string

func (*Message) EditTime

func (m *Message) EditTime() time.Time

func (*Message) ForwardTime

func (m *Message) ForwardTime() time.Time

func (*Message) IsCommand

func (m *Message) IsCommand() bool

func (*Message) Time

func (m *Message) Time() time.Time

func (*Message) Type

func (m *Message) Type() MessageType

type MessageEntity

type MessageEntity struct {
	Type     MessageEntityType `json:"type,omitempty"`
	Offset   int               `json:"offset,omitempty"`
	Length   int               `json:"length,omitempty"`
	URL      *url.URL          `json:"url,omitempty"`      // Optional
	User     *User             `json:"user,omitempty"`     // Optional
	Language string            `json:"language,omitempty"` // Optional
}

type MessageEntityType

type MessageEntityType string
const (
	MessageEntityTypeMention       MessageEntityType = "mention"
	MessageEntityTypeHashtag       MessageEntityType = "hashtag"
	MessageEntityTypeCashtag       MessageEntityType = "cashtag"
	MessageEntityTypeBotCommand    MessageEntityType = "bot_command"
	MessageEntityTypeURL           MessageEntityType = "url"
	MessageEntityTypeEmail         MessageEntityType = "email"
	MessageEntityTypePhoneNumber   MessageEntityType = "phone_number"
	MessageEntityTypeBold          MessageEntityType = "bold"
	MessageEntityTypeItalic        MessageEntityType = "italic"
	MessageEntityTypeUnderline     MessageEntityType = "underline"
	MessageEntityTypeStrikethrough MessageEntityType = "strikethrough"
	MessageEntityTypeCode          MessageEntityType = "code"
	MessageEntityTypePre           MessageEntityType = "pre"
	MessageEntityTypeTextLink      MessageEntityType = "text_link"
	MessageEntityTypeTextMention   MessageEntityType = "text_mention"
)

type MessageID

type MessageID struct {
	ID int `json:"message_id,omitempty"`
}

type MessageType

type MessageType string
const (
	MessageTypeText                          MessageType = "text"
	MessageTypeAnimation                     MessageType = "animation"
	MessageTypeAudio                         MessageType = "audio"
	MessageTypeDocument                      MessageType = "document"
	MessageTypePhoto                         MessageType = "photo"
	MessageTypeSticker                       MessageType = "sticker"
	MessageTypeVideo                         MessageType = "video"
	MessageTypeVideoNote                     MessageType = "video_note"
	MessageTypeVoice                         MessageType = "voice"
	MessageTypeContact                       MessageType = "contact"
	MessageTypeDice                          MessageType = "dice"
	MessageTypeGame                          MessageType = "game"
	MessageTypePoll                          MessageType = "poll"
	MessageTypeVenue                         MessageType = "venue"
	MessageTypeLocation                      MessageType = "location"
	MessageTypeNewChatMembers                MessageType = "new_chat_members"
	MessageTypeLeftChatMember                MessageType = "left_chat_member"
	MessageTypeNewChatTitle                  MessageType = "new_chat_title"
	MessageTypeNewChatPhoto                  MessageType = "new_chat_photo"
	MessageTypeDeleteChatPhoto               MessageType = "delete_chat_photo"
	MessageTypeGroupChatCreated              MessageType = "group_chat_created"
	MessageTypeSupergroupChatCreated         MessageType = "supergroup_chat_created"
	MessageTypeChannelChatCreated            MessageType = "channel_chat_created"
	MessageTypeMessageAutoDeleteTimerChanged MessageType = "message_auto_delete_timer_changed"
	MessageTypeInvoice                       MessageType = "invoice"
	MessageTypeSuccessfulPayment             MessageType = "successful_payment"
	MessageTypeProximityAlertTriggered       MessageType = "proximity_alert_triggered"
	MessageTypeVoiceChatScheduled            MessageType = "voice_chat_scheduled"
	MessageTypeVoiceChatStarted              MessageType = "voice_chat_started"
	MessageTypeVoiceChatEnded                MessageType = "voice_chat_ended"
	MessageTypeVoiceChatParticipantsInvited  MessageType = "voice_chat_participants_invited"
)

type ParseMode

type ParseMode string
const (
	ParseModeMarkdownV2 ParseMode = "MarkdownV2"
	ParseModeMarkdown   ParseMode = "Markdown"
	ParseModeHTML       ParseMode = "HTML"
)

type PhotoSize

type PhotoSize struct {
	FileID       string `json:"file_id,omitempty"`
	FileUniqueID string `json:"file_unique_id,omitempty"`
	Width        int    `json:"width,omitempty"`
	Height       int    `json:"height,omitempty"`
	FileSize     int    `json:"file_size,omitempty"` // Optional
}

type ReplyKeyboardMarkup

type ReplyKeyboardMarkup struct {
	Keyboard              [][]*KeyboardButton `json:"keyboard,omitempty"`
	ResizeKeyboard        bool                `json:"resize_keyboard,omitempty"`         // Optional
	OneTimeKeyboard       bool                `json:"one_time_keyboard,omitempty"`       // Optional
	InputFieldPlaceholder string              `json:"input_field_placeholder,omitempty"` // Optional
	Selective             bool                `json:"selective,omitempty"`               // Optional
}

type ReplyKeyboardRemove

type ReplyKeyboardRemove struct {
	RemoveKeyboard bool `json:"remove_keyboard,omitempty"`
	Selective      bool `json:"selective,omitempty"` // Optional
}

type ReplyMarkup

type ReplyMarkup interface {
	// contains filtered or unexported methods
}

type Response

type Response struct {
	Ok          bool                `json:"ok,omitempty"`
	Description string              `json:"description,omitempty"` // Optional
	Result      json.RawMessage     `json:"result,omitempty"`      // Optional
	ErrorCode   int                 `json:"error_code,omitempty"`  // Optional
	Parameters  *ResponseParameters `json:"parameters,omitempty"`  // Optional
}

type ResponseParameters

type ResponseParameters struct {
	MigrateToChatID int64 `json:"migrate_to_chat_id,omitempty"` // Optional
	RetryAfter      int   `json:"retry_after,omitempty"`        // Optional
}

type SendDiceOptions

type SendDiceOptions struct {
	Emoji                    DiceEmoji   `json:"emoji,omitempty"`
	DisableNotification      bool        `json:"disable_notification,omitempty"`
	ReplyToMessageID         int         `json:"reply_to_message_id,omitempty"`
	AllowSendingWithoutReply bool        `json:"allow_sending_without_reply,omitempty"`
	ReplyMarkup              ReplyMarkup `json:"reply_markup,omitempty"`
}

type SendMessageOptions

type SendMessageOptions struct {
	ParseMode                ParseMode        `json:"parse_mode,omitempty"`
	Entities                 []*MessageEntity `json:"entities,omitempty"`
	DisableWebPagePreview    bool             `json:"disable_web_page_preview,omitempty"`
	DisableNotification      bool             `json:"disable_notification,omitempty"`
	ReplyToMessageID         int              `json:"reply_to_message_id,omitempty"`
	AllowSendingWithoutReply bool             `json:"allow_sending_without_reply,omitempty"`
	ReplyMarkup              ReplyMarkup      `json:"reply_markup,omitempty"`
}

type Update

type Update struct {
	ID                int      `json:"update_id,omitempty"`
	Message           *Message `json:"message,omitempty"`             // Optional
	EditedMessage     *Message `json:"edited_message,omitempty"`      // Optional
	ChannelPost       *Message `json:"channel_post,omitempty"`        // Optional
	EditedChannelPost *Message `json:"edited_channel_post,omitempty"` // Optional

}

func (*Update) Type

func (u *Update) Type() UpdateType

type UpdateChan

type UpdateChan <-chan *Update

type UpdateType

type UpdateType string
const (
	UpdateTypeMessage           UpdateType = "message"
	UpdateTypeEditedMessage     UpdateType = "edited_message"
	UpdateTypeChannelPost       UpdateType = "channel_post"
	UpdateTypeEditedChannelPost UpdateType = "edited_channel_post"
)

type User

type User struct {
	ID                      int64        `json:"id,omitempty"`
	IsBot                   bool         `json:"is_bot,omitempty"`
	FirstName               string       `json:"first_name,omitempty"`                  // Optional
	LastName                string       `json:"last_name,omitempty"`                   // Optional
	Username                string       `json:"username,omitempty"`                    // Optional
	LanguageCode            language.Tag `json:"language_code,omitempty"`               // Optional
	CanJoinGroups           bool         `json:"can_join_groups,omitempty"`             // Optional
	CanReadAllGroupMessages bool         `json:"can_read_all_group_messages,omitempty"` // Optional
	SupportsInlineQueries   bool         `json:"supports_inline_queries,omitempty"`     // Optional
}

func (*User) String

func (u *User) String() string

Jump to

Keyboard shortcuts

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