telegram

package module
v0.0.3-alpha.7 Latest Latest
Warning

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

Go to latest
Published: May 29, 2021 License: MIT Imports: 6 Imported by: 1

README

go-telegram

build

Introduction

Send and receive telegram messages with ease, witten in Go.

Features

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

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, filePath string) (io.ReadCloser, error)

GetFile retrieves the file by filePath from telegram API

func GetUpdates

func GetUpdates(botToken string, offset string) (*[]Update, 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 []Update
offset := 0
for {
	log.Println("Using offset: " + fmt.Sprint(offset))
	updates, err := GetUpdates(botToken, fmt.Sprint(offset))
	if err != nil {
		log.Println(err)
		break
	}
	if len(*updates) > 0 {
		allUpdates = append(allUpdates, *updates...)
		offset = int(allUpdates[len(*updates)-1].UpdateID) + 1
	} else {
		break
	}
}
for _, update := range allUpdates {
	log.Println("[RECEIVED MESSAGE] " + update.Message.Text)
}

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 {
	log.Println(err)
}

Types

type Chat

type Chat struct {
	ID        uint64 `json:"id"`
	Username  string `json:"username,omitempty"`
	FirstName string `json:"firstName,omitempty"`
	LastName  string `json:"lastName,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

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 GetUpdatesResponseBody

type GetUpdatesResponseBody struct {
	OK     bool      `json:"ok"`
	Result *[]Update `json:"result"`
}

GetUpdatesResponseBody defines the structure of the body returned by the getUpdates 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 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 := "12345"

if chatId != "" {
	// Sending a text message
	result, err := SendMessage(botToken, chatId, message)
	if err != nil {
		log.Println(err)
		return
	}
	log.Println("[SENT] " + result.Text)
}

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 SendMessageResponseBody

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

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

type Update

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

Update defines the structure of update object

Jump to

Keyboard shortcuts

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