tgbot

package module
v0.0.0-...-9c91b00 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2022 License: Apache-2.0 Imports: 9 Imported by: 0

README

tgbot

tgbot is a simple lightweight no-dependencies library for Telegram Bot API

Overview

Usage

Initialization
import "github.com/Laaguini/tgbot"

func main() {
    bot := &tgbot.New{
        Token:           "TOKEN",
        PollingInterval: 500 * time.Millisecond,
    }
    
    bot.Poll()
}
Getting bot info
// Info as JSON string
fmt.Println(bot.Info())
Handling text messages
// Any text message 
bot.OnAnyMessage(func(message tgbot.Message) {
    fmt.Printf("%s From: %s Message: %s \n", time.Now().Format("15:51:15"), message.From.Username, message.Text)
})
    
// Message that starts from '/', e.g '/start'
bot.OnCommand("start", func(message tgbot.Message) {
    bot.Send(message.Chat.Id, "Hi, "+message.From.FirstName+" !")
})
    
// Handling not a command
bot.OnMessage(func(message tgbot.Message) {
    bot.Send(message.Chat.Id, message.Text)
})
Handling stickers
bot.OnSticker(func(message tgbot.Message) {
    bot.Send(message.Chat.Id, message.Sticker.Emoji)
})
Handling files
// Donwnloading voice message
bot.OnVoice(func(message tgbot.Message) {
    voice := message.Voice
    fileReader := bot.GetFile(voice.Id)

    out, err := os.Create("./files/" + voice.Id + voice.Extensions[0])
    if err != nil {
        fmt.Println(err)
    }
    defer out.Close()
    defer fileReader.Close()

    _, err = io.Copy(out, fileReader)
    if err ! nil {
        fmt.Println(err)
    }
})

// Logging audio messsage info 
bot.OnAudio(func(message tgbot.Message) {
    audio := message.Audio	
    fmt.Println(audio.Name + " " + strconv.Itoa(audio.Duration) + " " + strconv.Itoa(audio.Size))
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Audio

type Audio struct {
	Id         string `json:"file_id"`
	UniqueId   string `json:"file_unique_id"`
	Duration   int
	Performer  string
	Title      string
	Name       string `json:"file_name"`
	MimeType   string `json:"mime_type"`
	Size       int    `json:"file_size"`
	Thumb      Photosize
	Extensions []string
}

type Chat

type Chat struct {
	Id        int
	FirstName string `json:"first_name"`
	Username  string
	Type      string
}

type CommandHandler

type CommandHandler struct {
	Command string
	Handler func(message Message)
}

type Document

type Document struct {
	Id       string `json:"file_id"`
	UniqueId string `json:"file_unique_id"`
	Name     string `json:"file_name"`
	MimeType string `json:"mime_type"`
	Size     int    `json:"file_size"`
	Thumb    Photosize
}

type File

type File struct {
	Id       string `json:"file_id"`
	UniqueId string `json:"file_unique_id"`
	Size     int    `json:"file_size"`
	Path     string `json:"file_path"`
}

type GetArgs

type GetArgs struct {
	Action string
	Params []Param
}

type Handlers

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

type Message

type Message struct {
	Id                 int `json:"message_id"`
	ThreadId           int `json:"message_thread_id"`
	From               User
	ForwardFrom        User
	ForwardFromChat    User
	ForwardedMessageId int `json:"forward_from_message_id"`
	ForwardDate        int
	SenderChat         Chat
	Chat               Chat
	Date               int
	Text               string
	ReplyToMessage     *Message
	ViaBot             User
	EditDate           int
	Sticker            Sticker
	Audio              Audio
	Document           Document
	Video              Video
	VideoNote          VideoNote
	Voice              Voice
}

type New

type New struct {
	Token           string
	PollingInterval time.Duration
	Handlers        Handlers
}

func (*New) GetFile

func (i *New) GetFile(fileId string) io.ReadCloser

func (*New) GetUpdates

func (i *New) GetUpdates(offset int) []Update

func (*New) HandleAudio

func (i *New) HandleAudio(message Message)

func (*New) HandleDocument

func (i *New) HandleDocument(message Message)

func (*New) HandleMessage

func (i *New) HandleMessage(message Message)

func (*New) HandleSticker

func (i *New) HandleSticker(message Message)

func (*New) HandleVideo

func (i *New) HandleVideo(message Message)

func (*New) HandleVideoNote

func (i *New) HandleVideoNote(message Message)

func (*New) HandleVoice

func (i *New) HandleVoice(message Message)

func (*New) Info

func (i *New) Info() string

func (*New) OnAnyMessage

func (i *New) OnAnyMessage(handler func(message Message))

func (*New) OnAudio

func (i *New) OnAudio(handler func(message Message))

func (*New) OnCommand

func (i *New) OnCommand(command string, handler func(message Message))

func (*New) OnDocument

func (i *New) OnDocument(handler func(message Message))

func (*New) OnMessage

func (i *New) OnMessage(handler func(message Message))

func (*New) OnSticker

func (i *New) OnSticker(handler func(message Message))

func (*New) OnVideo

func (i *New) OnVideo(handler func(message Message))

func (*New) OnVideoNote

func (i *New) OnVideoNote(handler func(message Message))

func (*New) OnVoice

func (i *New) OnVoice(handler func(message Message))

func (*New) Poll

func (i *New) Poll()

func (*New) Send

func (i *New) Send(chatId int, text string)

type Param

type Param struct {
	Name  string
	Value string
}

type Photosize

type Photosize struct {
	Width  int
	Height int
	Size   int `json:"file_size"`
}

type PostArgs

type PostArgs struct {
	Action string
	Body   interface{}
}

type Sticker

type Sticker struct {
	Id       string `json:"file_id"`
	Type     string
	Thumb    Photosize
	Emoji    string
	SetName  string `json:"set_name"`
	FileSize int    `json:"file_size"`
}

type Update

type Update struct {
	Id                int `json:"update_id"`
	Message           *Message
	EditedMessage     *Message
	ChannelPost       *Message
	EditedChannelPost *Message
}

type UpdateResult

type UpdateResult struct {
	Ok      bool
	Updates []Update `json:"result"`
}

type User

type User struct {
	Id           int
	IsBot        bool
	FirstName    string `json:"first_name"`
	Username     string
	LanguageCode string
}

type Video

type Video struct {
	Id         string `json:"file_id"`
	UniqueId   string `json:"file_unique_id"`
	Width      int
	Height     int
	Duration   int
	Name       string `json:"file_name"`
	MimeType   string `json:"mime_type"`
	Size       int    `json:"file_size"`
	Thumb      Photosize
	Extensions []string
}

type VideoNote

type VideoNote struct {
	Id       string `json:"file_id"`
	UniqueId string `json:"file_unique_id"`
	Length   int
	Duration int
	Size     int `json:"file_size"`
	Thumb    Photosize
}

type Voice

type Voice struct {
	Id         string `json:"file_id"`
	UniqueId   string `json:"file_unique_id"`
	Duration   int
	Size       int `json:"file_size"`
	Thumb      Photosize
	MimeType   string `json:"mime_type"`
	Extensions []string
}

Jump to

Keyboard shortcuts

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