Documentation
¶
Index ¶
- func GetFile(botToken string, path string) (io.ReadCloser, error)
- func GetFileContentType(path string) (string, error)
- func GetUserProfilePhoto(botToken string, userId string) ([][]File, error)
- func SetWebhook(botToken string, webhookURL string) error
- type Chat
- type File
- type GetFileResponseBody
- type GetMeResponseBody
- type GetUpdatesResponseBody
- type GetUserProfilePhotoResponseBody
- type Location
- type Message
- type ResponseBody
- type SendLocationRequestBody
- type SendMessageRequestBody
- type Update
- type Updates
- type User
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
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
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 ¶
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 ¶
File defines the structure of file object
func GetFileInfo ¶ added in v0.0.3
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
GetFileResponseBody defines the structure of the body returned by the getFile method of the telegram API
type GetMeResponseBody ¶ added in v0.0.6
GetMeResponseBody defines the structure of the body returned by the getMe method of the telegram API
type GetUpdatesResponseBody ¶
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
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
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 ¶
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
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 ¶
SendMessageRequestBody defines the request body structure required for the `sendMessage` method for the telegram API
type Updates ¶ added in v0.0.4
type Updates []Update
Updates defines an array of updates
func GetUpdates ¶
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
}
}
Source Files
¶
- Chat.go
- File.go
- GetFile.go
- GetFileContentType.go
- GetFileInfo.go
- GetFileResponseBody.go
- GetMe.go
- GetMeResponseBody.go
- GetUpdates.go
- GetUpdatesResponseBody.go
- GetUserProfilePhoto.go
- GetUserProfilePhotoResponseBody.go
- Location.go
- Message.go
- ResponseBody.go
- SendLocation.go
- SendLocationRequestBody.go
- SendMedia.go
- SendMessage.go
- SendMessageRequestBody.go
- SetWebhook.go
- Update.go
- User.go