Categorygithub.com/bots-house/telegram-bot-api
modulepackage
2.2.1+incompatible
Repository: https://github.com/bots-house/telegram-bot-api.git
Documentation: pkg.go.dev

# README

Golang bindings for the Telegram Bot API

GoDoc Travis

All methods have been added, and all features should be available. If you want a feature that hasn't been added yet or something is broken, open an issue and I'll see what I can do.

All methods are fairly self explanatory, and reading the godoc page should explain everything. If something isn't clear, open an issue or submit a pull request.

The scope of this project is just to provide a wrapper around the API without any additional features. There are other projects for creating something with plugins and command handlers without having to design all that yourself.

Use github.com/go-telegram-bot-api/telegram-bot-api for the latest version, or use gopkg.in/telegram-bot-api.v2 for the stable build.

Example

This is a very simple bot that just displays any gotten updates, then replies it to that chat.

package main

import (
	"log"
	"gopkg.in/telegram-bot-api.v2"
)

func main() {
	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
	if err != nil {
		log.Panic(err)
	}

	bot.Debug = true

	log.Printf("Authorized on account %s", bot.Self.UserName)

	u := tgbotapi.NewUpdate(0)
	u.Timeout = 60

	updates, err := bot.GetUpdatesChan(u)

	for update := range updates {
		log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)

		msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
		msg.ReplyToMessageID = update.Message.MessageID

		bot.Send(msg)
	}
}

If you need to use webhooks (if you wish to run on Google App Engine), you may use a slightly different method.

package main

import (
	"gopkg.in/telegram-bot-api.v2"
	"log"
	"net/http"
)

func main() {
	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
	if err != nil {
		log.Fatal(err)
	}

	bot.Debug = true

	log.Printf("Authorized on account %s", bot.Self.UserName)

	_, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
	if err != nil {
		log.Fatal(err)
	}

	updates := bot.ListenForWebhook("/" + bot.Token)
	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)

	for update := range updates {
		log.Printf("%+v\n", update)
	}
}

If you need, you may generate a self signed certficate, as this requires HTTPS / TLS. The above example tells Telegram that this is your certificate and that it should be trusted, even though it is not properly signed.

openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3560 -subj "//O=Org\CN=Test" -nodes

Now that Let's Encrypt has entered public beta, you may wish to generate your free TLS certificate there.

# Functions

NewAudioShare shares an existing audio file.
NewAudioUpload creates a new audio uploader.
NewBotAPI creates a new BotAPI instance.
NewBotAPIWithClient creates a new BotAPI instance and allows you to pass a http.Client.
NewChatAction sets a chat action.
NewDocumentShare shares an existing document.
NewDocumentUpload creates a new document uploader.
NewForward creates a new forward.
NewInlineQueryResultArticle creates a new inline query article.
NewInlineQueryResultGIF creates a new inline query GIF.
NewInlineQueryResultMPEG4GIF creates a new inline query MPEG4 GIF.
NewInlineQueryResultPhoto creates a new inline query photo.
NewInlineQueryResultVideo creates a new inline query video.
NewLocation shares your location.
NewMessage creates a new Message.
NewPhotoShare shares an existing photo.
NewPhotoUpload creates a new photo uploader.
NewStickerShare shares an existing sticker.
NewStickerUpload creates a new sticker uploader.
NewUpdate gets updates since the last Offset.
NewUserProfilePhotos gets user profile photos.
NewVideoShare shares an existing video.
NewVideoUpload creates a new video uploader.
NewVoiceShare shares an existing voice.
NewVoiceUpload creates a new voice uploader.
NewWebhook creates a new webhook.
NewWebhookWithCert creates a new webhook with a certificate.

# Constants

APIEndpoint is the endpoint for all API methods, with formatting for Sprintf.
Constant values for ChatActions.
Constant values for ChatActions.
Constant values for ChatActions.
Constant values for ChatActions.
Constant values for ChatActions.
Constant values for ChatActions.
Constant values for ChatActions.
Constant values for ChatActions.
ErrAPIForbidden happens when a token is bad.
ErrBadFileType happens when you pass an unknown type.
FileEndpoint is the endpoint for downloading a file from Telegram.
Constant values for ParseMode in MessageConfig.
Constant values for ParseMode in MessageConfig.

# Structs

APIResponse is a response from the Telegram API with the result stored raw.
Audio contains information about audio.
AudioConfig contains information about a SendAudio request.
BaseChat is base type for all chat config types.
BaseFile is a base type for all file config types.
BotAPI allows you to interact with the Telegram Bot API.
Chat contains information about the place a message was sent.
ChatActionConfig contains information about a SendChatAction request.
ChosenInlineResult is an inline query result chosen by a User.
Contact contains information about a contact.
Document contains information about a document.
DocumentConfig contains information about a SendDocument request.
File contains information about a file to download from Telegram.
FileBytes contains information about a set of bytes to upload as a File.
FileConfig has information about a file hosted on Telegram.
FileReader contains information about a reader to upload as a File.
ForceReply allows the Bot to have users directly reply to it without additional interaction.
ForwardConfig contains information about a ForwardMessage request.
GroupChat is a group chat.
InlineConfig contains information on making an InlineQuery response.
InlineQuery is a Query from Telegram for an inline request.
InlineQueryResultArticle is an inline query response article.
InlineQueryResultGIF is an inline query response GIF.
InlineQueryResultMPEG4GIF is an inline query response MPEG4 GIF.
InlineQueryResultPhoto is an inline query response photo.
InlineQueryResultVideo is an inline query response video.
Location contains information about a place.
LocationConfig contains information about a SendLocation request.
Message is returned by almost every request, and contains data about almost anything.
MessageConfig contains information about a SendMessage request.
PhotoConfig contains information about a SendPhoto request.
PhotoSize contains information about photos.
ReplyKeyboardHide allows the Bot to hide a custom keyboard.
ReplyKeyboardMarkup allows the Bot to set a custom keyboard.
Sticker contains information about a sticker.
StickerConfig contains information about a SendSticker request.
Update is an update response, from GetUpdates.
UpdateConfig contains information about a GetUpdates request.
User is a user on Telegram.
UserProfilePhotos contains a set of user profile photos.
UserProfilePhotosConfig contains information about a GetUserProfilePhotos request.
Video contains information about a video.
VideoConfig contains information about a SendVideo request.
Voice contains information about a voice.
VoiceConfig contains information about a SendVoice request.
WebhookConfig contains information about a SetWebhook request.

# Interfaces

Chattable is any config type that can be sent.
Fileable is any config type that can be sent that includes a file.