Documentation
¶
Index ¶
- func GetFile(botToken string, filePath string) (io.ReadCloser, error)
- func GetFileContentType(path string) (string, error)
- func SetWebhook(botToken string, webhookURL string) error
- type Chat
- type File
- type GetFileResponseBody
- type GetUpdatesResponseBody
- type Location
- type Message
- type ResponseBody
- type SendLocationRequestBody
- type SendMessageRequestBody
- type Update
- type Updates
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 GetFileContentType ¶ added in v0.0.5
GetFileContentType returns the content type of the file
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)
}
fmt.Println("Done !")
Output: Done !
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 GetFileResponseBody ¶ added in v0.0.3
GetFileResponseBody defines the structure of the body returned by the getFile method of the telegram API
type GetUpdatesResponseBody ¶
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 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")
_, err := SendMedia(botToken, chatId, path)
if err != nil {
panic(err)
}
fmt.Println("Done !")
Output: Done !
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 {
fmt.Println(err)
return
}
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
}
}
fmt.Println("Done !")
Output: Using offset: 0 Done !