Documentation
¶
Overview ¶
Example (GetUpdates) ¶
Get updates
// 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)
}
Example (SendMessage) ¶
Send a text message
// 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)
}
Example (SetWebhook) ¶
Set webhook
// Bot token generated from BotFather
botToken := os.Getenv("TELEGRAM_BOT_TOKEN")
// Setting webhook
if err := SetWebhook(botToken, ""); err != nil {
log.Println(err)
}
Index ¶
- func GetFile(botToken string, filePath string) (io.ReadCloser, error)
- func GetUpdates(botToken string, offset string) (*[]Update, error)
- func SetWebhook(botToken string, webhookURL string) error
- type Chat
- type File
- type GetFileResponseBody
- type GetUpdatesResponseBody
- type Location
- type Message
- type SendMessageRequestBody
- type SendMessageResponseBody
- type Update
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 ¶
GetUpdates retrieves incoming updates using long polling
func SetWebhook ¶
SetWebhook sets the webhook URL for the telegram bot. Setting an empty url will remove the webhook integration
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
type SendMessageRequestBody ¶
SendMessageRequestBody defines the request body structure required for the `sendMessage` method for the telegram API
type SendMessageResponseBody ¶
SendMessageResponseBody defines the structure of the body returned by the `sendMessage` method of the telegram API