telegram

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2021 License: MIT Imports: 9 Imported by: 1

README

go-telegram

build GitHub release (latest by date including pre-releases)

Introduction

Send and receive messages using the Telegram Bot API with ease. Supports plain text messages, media attachments and location data, written in Go.

Features

  • Send and receive text messages
  • Send and receive media files
  • Send and receive location data
  • Update webhook URL

Examples

See examples at https://pkg.go.dev/github.com/pravinba9495/go-telegram#pkg-examples

Documentation

The complete documentation is available at https://pkg.go.dev/github.com/pravinba9495/go-telegram

License

MIT

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetFile

func GetFile(botToken string, path string) (io.ReadCloser, error)

GetFile retrieves the file by path from telegram API

func GetFileContentType added in v0.0.5

func GetFileContentType(path string) (string, error)

GetFileContentType returns the content type of the file

Example
path := os.Getenv("TELEGRAM_FILE_PATH")
mtype, err := GetFileContentType(path)
if err != nil {
	panic(err)
}
fmt.Println(mtype)

func GetUserProfilePhoto added in v0.0.9

func GetUserProfilePhoto(botToken string, userId string) ([][]File, error)

GetUserProfilePhoto retrieves profile photos of a user

Example
// Bot token generated from BotFather
botToken := os.Getenv("TELEGRAM_BOT_TOKEN")
userId := os.Getenv("TELEGRAM_CHAT_ID")

files, err := GetUserProfilePhoto(botToken, userId)
if err != nil {
	panic(err)
}
fmt.Print(files)

func SetWebhook

func SetWebhook(botToken string, webhookURL string) error

SetWebhook sets the webhook URL for the telegram bot. Setting an empty url will remove the webhook integration

Example
// Bot token generated from BotFather
botToken := os.Getenv("TELEGRAM_BOT_TOKEN")

// Setting webhook
if err := SetWebhook(botToken, ""); err != nil {
	panic(err)
}

Types

type Chat

type Chat struct {
	ID        uint64 `json:"id"`
	Username  string `json:"username,omitempty"`
	FirstName string `json:"first_name,omitempty"`
	LastName  string `json:"last_name,omitempty"`
}

Chat defines the structure of chat object

type File

type File struct {
	FileID   string `json:"file_id"`
	FilePath string `json:"file_path,omitempty"`
}

File defines the structure of file object

func GetFileInfo added in v0.0.3

func GetFileInfo(botToken string, fileId string) (*File, error)

GetFileInfo retrieves basic info about a file and prepares it for downloading

Example
botToken := os.Getenv("TELEGRAM_BOT_TOKEN")
fileId := os.Getenv("TELEGRAM_FILE_ID")
file, err := GetFileInfo(botToken, fileId)
if err != nil {
	panic(err)
}
fmt.Println(file)

type GetFileResponseBody added in v0.0.3

type GetFileResponseBody struct {
	OK     bool  `json:"ok"`
	Result *File `json:"result"`
}

GetFileResponseBody defines the structure of the body returned by the getFile method of the telegram API

type GetMeResponseBody added in v0.0.6

type GetMeResponseBody struct {
	OK     bool  `json:"ok"`
	Result *User `json:"result"`
}

GetMeResponseBody defines the structure of the body returned by the getMe method of the telegram API

type GetUpdatesResponseBody

type GetUpdatesResponseBody struct {
	OK     bool     `json:"ok"`
	Result *Updates `json:"result"`
}

GetUpdatesResponseBody defines the structure of the body returned by the getUpdates method of the telegram API

type GetUserProfilePhotoResponseBody added in v0.0.9

type GetUserProfilePhotoResponseBody struct {
	OK     bool `json:"ok"`
	Result struct {
		Count  int      `json:"total_count"`
		Photos [][]File `json:"photos"`
	} `json:"result"`
}

GetUserProfilePhotoResponseBody defines the structure of the body returned by the getUserProfilePhotos method of the telegram API

type Location

type Location struct {
	Longitude float64 `json:"longitude,omitempty"`
	Latitude  float64 `json:"latitude,omitempty"`
}

Location defines the structure of location object

type Message

type Message struct {
	MessageID uint64    `json:"message_id"`
	Chat      *Chat     `json:"chat"`
	Text      string    `json:"text,omitempty"`
	Audio     *File     `json:"audio,omitempty"`
	Document  *File     `json:"document,omitempty"`
	Photo     *[]File   `json:"photo,omitempty"`
	Video     *File     `json:"video,omitempty"`
	Voice     *File     `json:"voice,omitempty"`
	VideoNote *File     `json:"video_note,omitempty"`
	Caption   string    `json:"caption,omitempty"`
	Animation *File     `json:"animation,omitempty"`
	Location  *Location `json:"location,omitempty"`
}

Message defines the structure of message object

func SendLocation added in v0.0.5

func SendLocation(botToken string, chatId string, location Location) (*Message, error)

SendLocation sends a location to a recipient

Example
botToken := os.Getenv("TELEGRAM_BOT_TOKEN")
chatId := os.Getenv("TELEGRAM_CHAT_ID")
location := Location{
	Longitude: 0,
	Latitude:  0,
}

message, err := SendLocation(botToken, chatId, location)
if err != nil {
	panic(err)
}
fmt.Println(message.Location)

func SendMedia added in v0.0.4

func SendMedia(botToken string, chatId string, path string) (*Message, error)

SendMedia sends a media file to a recipient

Example
// Bot token generated from BotFather
botToken := os.Getenv("TELEGRAM_BOT_TOKEN")

chatId := os.Getenv("TELEGRAM_CHAT_ID")
path := os.Getenv("TELEGRAM_FILE_PATH")

message, err := SendMedia(botToken, chatId, path)
if err != nil {
	panic(err)
}
fmt.Println(message)

func SendMessage

func SendMessage(botToken string, chatId string, text string) (*Message, error)

SendMessage sends a text message to a recipient

Example
// Bot token generated from BotFather
botToken := os.Getenv("TELEGRAM_BOT_TOKEN")
message := "Hi, I am a message from the telegram bot."
chatId := os.Getenv("TELEGRAM_CHAT_ID")

if chatId != "" {
	// Sending a text message
	result, err := SendMessage(botToken, chatId, message)
	if err != nil {
		panic(err)
	}
	fmt.Println("[SENT] " + result.Text)
}
Output:
[SENT] Hi, I am a message from the telegram bot.

type ResponseBody added in v0.0.4

type ResponseBody struct {
	OK     bool     `json:"ok"`
	Result *Message `json:"result"`
}

ResponseBody defines the structure of the body returned by the `sendMessage` method of the telegram API

type SendLocationRequestBody added in v0.0.4

type SendLocationRequestBody struct {
	ChatID    string  `json:"chat_id"`
	Longitude float64 `json:"longitude"`
	Latitude  float64 `json:"latitude"`
}

SendLocationRequestBody defines the request body structure required for the `sendLocation` method for the telegram API

type SendMessageRequestBody

type SendMessageRequestBody struct {
	ChatID string `json:"chat_id"`
	Text   string `json:"text"`
}

SendMessageRequestBody defines the request body structure required for the `sendMessage` method for the telegram API

type Update

type Update struct {
	UpdateID uint64   `json:"update_id"`
	Message  *Message `json:"message"`
}

Update defines the structure of update object

type Updates added in v0.0.4

type Updates []Update

Updates defines an array of updates

func GetUpdates

func GetUpdates(botToken string, offset string) (*Updates, error)

GetUpdates retrieves incoming updates using long polling

Example
// Bot token generated from BotFather
botToken := os.Getenv("TELEGRAM_BOT_TOKEN")

// Retrieving all updates until the server returns empty response
var allUpdates Updates
offset := 0
for {
	fmt.Println("Using offset: " + fmt.Sprint(offset))
	updates, err := GetUpdates(botToken, fmt.Sprint(offset))
	if err != nil {
		panic(err)
	}
	if len(*updates) > 0 {
		allUpdates = append(allUpdates, *updates...)
		offset = int(allUpdates[len(*updates)-1].UpdateID) + 1
		if len(*updates) < 100 {
			break
		}
	} else {
		break
	}
}

type User added in v0.0.6

type User struct {
	ID        uint64 `json:"id"`
	Username  string `json:"username,omitempty"`
	FirstName string `json:"firstName,omitempty"`
	LastName  string `json:"lastName,omitempty"`
}

User defines the structure of user object

func GetMe added in v0.0.6

func GetMe(botToken string) (*User, error)

GetMe retrieves basic info about the bot, useful for testing bot's auth token

Example
botToken := os.Getenv("TELEGRAM_BOT_TOKEN")
user, err := GetMe(botToken)
if err != nil {
	panic(err)
}
fmt.Println(user)

Jump to

Keyboard shortcuts

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