tlbot

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

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

Go to latest
Published: Nov 17, 2016 License: MIT Imports: 12 Imported by: 0

README

tlbot

tlbot is a Go package to help writing Telegram bots

installation

go get github.com/igungor/tlbot

usage

Documentation

Also look at echobot

license

MIT. See LICENSE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action string
const (
	Typing            Action = "typing"
	UploadingPhoto    Action = "upload_photo"
	UploadingVideo    Action = "upload_video"
	UploadingAudio    Action = "upload_audio"
	UploadingDocument Action = "upload_document"
	FindingLocation   Action = "find_location"
)

Types of actions to broadcast

type Audio

type Audio struct {
	File
	Duration  int    `json:"duration"`
	Performer string `json:"performer"`
	Title     string `json:"title"`
	MimeType  string `json:"mime_type"`
}

Audio represents an audio file.

type Bot

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

Bot represent a Telegram bot.

func New

func New(token string) Bot

New creates a new Telegram bot with the given token, which is given by Botfather. See https://core.telegram.org/bots#botfather

func (Bot) Listen

func (b Bot) Listen(addr string) <-chan Message

Listen listens on the given address addr and returns a read-only Message channel.

func (Bot) SendChatAction

func (b Bot) SendChatAction(recipient int, action Action) error

SendChatAction broadcasts type of action to recipient, such as `typing`, `uploading a photo` etc.

func (Bot) SendLocation

func (b Bot) SendLocation(recipient int, location Location, opts *SendOptions) error

TODO(ig): implement

SendLocation sends location point on the map.

func (Bot) SendMessage

func (b Bot) SendMessage(recipient int, message string, mode ParseMode, preview bool, opts *SendOptions) error

SendMessage sends text message to the recipient. Callers can send plain text or markdown messages by setting mode parameter.

func (Bot) SendPhoto

func (b Bot) SendPhoto(recipient int, photo Photo, caption string, opts *SendOptions) error

SendPhoto sends given photo to recipient. Only remote URLs are supported for now. A trivial example is:

b := bot.New("your-token-here")
photo := bot.Photo{FileURL: "http://i.imgur.com/6S9naG6.png"}
err := b.SendPhoto(recipient, photo, "sample image", nil)

func (Bot) SetWebhook

func (b Bot) SetWebhook(webhook string) error

SetWebhook assigns bot's webhook url with the given url.

type Chat

type Chat struct {
	ID        int    `json:"id"`
	Type      string `json:"type"`
	Title     string `json:"title"`
	Username  string `json:"username"`
	FirstName string `json:"first_name"`
	LastName  string `json:"last_name"`
}

Chat represents a Telegram chat.

func (Chat) IsGroupChat

func (c Chat) IsGroupChat() bool

IsGroupChat reports whether the message is originally sent from a chat group.

type Contact

type Contact struct {
	PhoneNumber string `json:"phone_number"`
	FirstName   string `json:"first_name"`
	LastName    string `json:"last_name"`
	UserID      string `json:"user_id"`
}

Contact represents a phone contact.

type Document

type Document struct {
	File
	Filename  string `json:"file_name"`
	Thumbnail Photo  `json:"thumb"`
	MimeType  string `json:"mime_type"`
}

Document represents a general file (as opposed to photos and audio files).

type File

type File struct {
	// File is embedded in most of the types. So a `File` prefix is used
	FileID   string `json:"file_id"`
	FileSize int    `json:"file_size"`

	// URL to the file (custom field)
	FileURL string `json:"-"`

	// Path to the file on local filesystem (custom field)
	FilePath string `json:"-"`
}

func (File) Exists

func (f File) Exists() bool

Exists reports whether the file is already at Telegram servers.

func (File) IsLocal

func (f File) IsLocal() bool

IsLocal reports whether the file is the local filesystem.

func (File) IsRemote

func (f File) IsRemote() bool

IsRemote reports whether the file is on a remote server.

type Location

type Location struct {
	Lat  float64 `json:"latitude"`
	Long float64 `json:"longitude"`
}

Location represents a point on the map.

type Message

type Message struct {
	// Unique message identifier
	ID int `json:"message_id"`

	// Sender (optional. can be empty for messages sent to channel)
	From User `json:"from"`

	// Date is when the message was sent in Unix time
	Unixtime int `json:"date"`

	// Conversation the message belongs to — user in case of a private chat,
	// group in case of a group chat
	Chat Chat `json:"chat"`

	// For forwarded messages, sender of the original message (Optional)
	ForwardFrom User `json:"forward_from"`

	// For forwarded messages, date the original message was sent in
	// Unix time (Optional)
	ForwardDate int `json:"forward_date"`

	// For replies, the original message. Note that the Message
	// object in this field will not contain further reply_to_message fields
	// even if it itself is a reply (Optional)
	ReplyTo *Message `json:"reply_to_message"`

	// For text messages, the actual UTF-8 text of the message (Optional)
	Text string `json:"text"`

	// Message is an audio file, information about the file (Optional)
	Audio Audio `json:"audio"`

	// Message is a general file, information about the file (Optional)
	Document Document `json:"document"`

	// Message is a photo, available sizes of the photo (Optional)
	Photos []Photo `json:"photo"`

	// Message is a sticker, information about the sticker (Optional)
	Sticker Sticker `json:"sticker"`

	// Message is a video, information about the video (Optional)
	Video Video `json:"video"`

	// Message is a shared contact, information about the contact (Optional)
	Contact Contact `json:"contact"`

	// Message is a shared location, information about the location (Optional)
	Location Location `json:"location"`

	// A new member was added to the group, information about them
	// (this member may be bot itself) (Optional)
	JoinedUser User `json:"new_chat_participant"`

	// A member was removed from the group, information about them
	// (this member may be bot itself) (Optional)
	LeftUser User `json:"left_chat_participant"`

	// A group title was changed to this value (Optional)
	NewChatTitle string `json:"new_chat_title"`

	// A group photo was change to this value (Optional)
	NewChatPhoto []Photo `json:"new_chat_photo"`

	// Informs that the group photo was deleted (Optional)
	ChatPhotoDeleted bool `json:"delete_chat_photo"`

	// Informs that the group has been created (Optional)
	GroupChatCreated bool `json:"group_chat_created"`
}

Message represents a message to be sent.

func (Message) Args

func (m Message) Args() []string

Args returns all words after the first word in the message text. First word is meant to be the command name and can be accessed with Command method.

func (Message) Command

func (m Message) Command() string

Command returns the command's name: the first word in the message text. If message text starts with a `/`, function returns the command name, or else empty string.

func (Message) IsReply

func (m Message) IsReply() bool

IsReply reports whether the message is a reply to another message.

func (Message) IsService

func (m Message) IsService() bool

IsService reports whether the message is a Telegram service message, not a user sent message.

func (Message) String

func (m Message) String() string

String returns a human-readable representation of Message.

func (Message) Time

func (m Message) Time() time.Time

Time returns the moment of message in UTC time.

type ParseMode

type ParseMode string
const (
	ModeNone     ParseMode = ""
	ModeMarkdown ParseMode = "markdown"
)

Parse modes

type Photo

type Photo struct {
	File
	Width  int `json:"width"`
	Height int `json:"height"`
}

Photo represents one size of a photo or a file/sticker thumbnail.

type ReplyMarkup

type ReplyMarkup struct {
	Keyboard   [][]string `json:"keyboard,omitempty"`
	Resize     bool       `json:"resize_keyboard,omitempty"`
	OneTime    bool       `json:"one_time_keyboard,omitempty"`
	Selective  bool       `json:"selective,omitempty"`
	Hide       bool       `json:"hide_keyboard,omitempty"`
	ForceReply bool       `json:"force_reply,omitempty"`
}

type SendOptions

type SendOptions struct {
	// If the message is a reply, ID of the original message
	ReplyToMessageID int

	ReplyMarkup ReplyMarkup
}

type Sticker

type Sticker struct {
	File
	Width     int   `json:"width"`
	Height    int   `json:"height"`
	Thumbnail Photo `json:"thumb"`
}

Sticker represents a sticker.

type Update

type Update struct {
	ID      int     `json:"update_id"`
	Payload Message `json:"message"`
}

type User

type User struct {
	ID        int    `json:"id"`
	Username  string `json:"username"`
	FirstName string `json:"first_name"`
	LastName  string `json:"last_name"`
}

User represents a Telegram user or bot.

type Video

type Video struct {
	File
	Width     int    `json:"width"`
	Height    int    `json:"height"`
	Duration  int    `json:"duration"`
	Thumbnail Photo  `json:"thumb"`
	MimeType  string `json:"mime_type"`
	Caption   string `json:"caption"`
}

Video represents a video file.

type Voice

type Voice struct {
	File
	Duration int    `json:"duration"`
	MimeType string `json:"mime_type"`
}

Voice represents an voice note.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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