Categorygithub.com/ligmar/go-telegram-bot-api
modulepackage
1.0.5
Repository: https://github.com/ligmar/go-telegram-bot-api.git
Documentation: pkg.go.dev

# README

Golang bindings for the Telegram Bot API

This repository is fork of https://github.com/go-telegram-bot-api/telegram-bot-api


New fetches

  1. Channel with updates is public.

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.

Join the development group if you want to ask questions or discuss development.

Example

First, ensure the library is installed and up to date by running go get -u github.com/ligmar/go-telegram-bot-api.

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

package main

import (
	"log"

	"github.com/ligmar/go-telegram-bot-api"
)

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 {
		if update.Message == nil { // ignore any non-Message Updates
			continue
		}

		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 (
	"log"
	"net/http"

	"github.com/ligmar/go-telegram-bot-api"
)

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)
	}
	info, err := bot.GetWebhookInfo()
	if err != nil {
		log.Fatal(err)
	}
	if info.LastErrorDate != 0 {
		log.Printf("Telegram callback failed: %s", info.LastErrorMessage)
	}
	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 is available, you may wish to generate your free TLS certificate there.

# Functions

NewAnimationShare shares an existing animation.
NewAnimationUpload creates a new animation uploader.
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.
NewCallback creates a new callback message.
NewCallbackWithAlert creates a new callback message that alerts the user.
NewChatAction sets a chat action.
NewContact allows you to send a shared contact.
NewDeleteMessage creates a request to delete a message.
NewDocumentShare shares an existing document.
NewDocumentUpload creates a new document uploader.
NewEditMessageCaption allows you to edit the caption of a message.
NewEditMessageReplyMarkup allows you to edit the inline keyboard markup.
NewEditMessageText allows you to edit the text of a message.
NewForward creates a new forward.
NewHideKeyboard hides the keyboard, with the option for being selective or hiding for everyone.
NewInlineKeyboardButtonData creates an inline keyboard button with text and data for a callback.
NewInlineKeyboardButtonLoginURL creates an inline keyboard button with text which goes to a Login URL.
NewInlineKeyboardButtonSwitch creates an inline keyboard button with text which allows the user to switch to a chat or return to a chat.
NewInlineKeyboardButtonURL creates an inline keyboard button with text which goes to a URL.
NewInlineKeyboardMarkup creates a new inline keyboard.
NewInlineKeyboardRow creates an inline keyboard row with buttons.
NewInlineQueryResultArticle creates a new inline query article.
NewInlineQueryResultArticleHTML creates a new inline query article with HTML parsing.
NewInlineQueryResultArticleMarkdown creates a new inline query article with Markdown parsing.
NewInlineQueryResultAudio creates a new inline query audio.
NewInlineQueryResultDocument creates a new inline query document.
NewInlineQueryResultGIF creates a new inline query GIF.
NewInlineQueryResultLocation creates a new inline query location.
NewInlineQueryResultMPEG4GIF creates a new inline query MPEG4 GIF.
NewInlineQueryResultPhoto creates a new inline query photo.
NewInlineQueryResultPhotoWithThumb creates a new inline query photo.
NewInlineQueryResultVideo creates a new inline query video.
NewInlineQueryResultVoice creates a new inline query voice.
NewInputMediaPhoto creates a new InputMediaPhoto.
NewInputMediaVideo creates a new InputMediaVideo.
NewInvoice creates a new Invoice request to the user.
NewKeyboardButton creates a regular keyboard button.
NewKeyboardButtonContact creates a keyboard button that requests user contact information upon click.
NewKeyboardButtonLocation creates a keyboard button that requests user location information upon click.
NewKeyboardButtonRow creates a row of keyboard buttons.
NewLocation shares your location.
NewMediaGroup creates a new media group.
NewMessage creates a new Message.
NewMessageToChannel creates a new Message that is sent to a channel by username.
NewPhotoShare shares an existing photo.
NewPhotoUpload creates a new photo uploader.
NewRemoveKeyboard hides the keyboard, with the option for being selective or hiding for everyone.
NewReplyKeyboard creates a new regular keyboard with sane defaults.
NewSetChatPhotoShare shares an existing photo.
NewSetChatPhotoUpload creates a new chat 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.
NewVenue allows you to send a venue and its location.
NewVideoNoteShare shares an existing video.
NewVideoNoteUpload creates a new video note uploader.
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.
SetLogger specifies the logger that the package should use.

# 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.
Library errors.
FileEndpoint is the endpoint for downloading a file from Telegram.
Constant values for ParseMode in MessageConfig.
Constant values for ParseMode in MessageConfig.

# Structs

Animation is a GIF animation demonstrating the game.
AnimationConfig contains information about a SendAnimation request.
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.
BaseEdit is base type of all chat edits.
BaseFile is a base type for all file config types.
BotAPI allows you to interact with the Telegram Bot API.
CallbackConfig contains information on making a CallbackQuery response.
CallbackGame is for starting a game in an inline keyboard button.
CallbackQuery is data sent when a keyboard button with callback data is clicked.
Chat contains information about the place a message was sent.
ChatActionConfig contains information about a SendChatAction request.
ChatAnimation contains information about an animation.
ChatConfig contains information about getting information on a chat.
ChatConfigWithUser contains information about getting information on a specific user within a chat.
ChatMember is information about a member in a chat.
ChatMemberConfig contains information about a user in a chat for use with administrative functions such as kicking or unbanning a user.
ChatPhoto represents a chat photo.
ChosenInlineResult is an inline query result chosen by a User.
Contact contains information about a contact.
ContactConfig allows you to send a contact.
No description provided by the author
No description provided by the author
DeleteChatPhotoConfig contains information for delete chat photo.
DeleteMessageConfig contains information of a message in a chat to delete.
Document contains information about a document.
DocumentConfig contains information about a SendDocument request.
EditMessageCaptionConfig allows you to modify the caption of a message.
EditMessageReplyMarkupConfig allows you to modify the reply markup of a message.
EditMessageTextConfig allows you to modify the text in a message.
No description provided by the author
No description provided by the author
Error is an error containing extra information returned by the Telegram API.
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.
No description provided by the author
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.
Game is a game within Telegram.
GameConfig allows you to send a game.
GameHighScore is a user's score and position on the leaderboard.
GetGameHighScoresConfig allows you to fetch the high scores for a game.
GroupChat is a group chat.
No description provided by the author
InlineConfig contains information on making an InlineQuery response.
InlineKeyboardButton is a button within a custom keyboard for inline query responses.
InlineKeyboardMarkup is a custom keyboard presented for an inline bot.
InlineQuery is a Query from Telegram for an inline request.
InlineQueryResultArticle is an inline query response article.
InlineQueryResultAudio is an inline query response audio.
InlineQueryResultDocument is an inline query response document.
InlineQueryResultGame is an inline query response game.
InlineQueryResultGIF is an inline query response GIF.
InlineQueryResultLocation is an inline query response location.
InlineQueryResultMPEG4GIF is an inline query response MPEG4 GIF.
InlineQueryResultPhoto is an inline query response photo.
InlineQueryResultVideo is an inline query response video.
InlineQueryResultVoice is an inline query response voice.
InputContactMessageContent contains a contact for displaying as an inline query result.
InputLocationMessageContent contains a location for displaying as an inline query result.
InputMediaPhoto contains a photo for displaying as part of a media group.
InputMediaVideo contains a video for displaying as part of a media group.
InputTextMessageContent contains text for displaying as an inline query result.
InputVenueMessageContent contains a venue for displaying as an inline query result.
Invoice contains basic information about an invoice.
InvoiceConfig contains information for sendInvoice request.
KeyboardButton is a button within a custom keyboard.
KickChatMemberConfig contains extra fields to kick user.
LabeledPrice represents a portion of the price for goods or services.
Location contains information about a place.
LocationConfig contains information about a SendLocation request.
LoginURL is a struct for LoginURL https://core.telegram.org/bots/api#loginurl.
MediaGroupConfig contains information about a sendMediaGroup request.
Message is returned by almost every request, and contains data about almost anything.
MessageConfig contains information about a SendMessage request.
MessageEntity contains information about data in a Message.
OrderInfo represents information about an order.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
PassportRequestInfoConfig allows you to request passport info.
PassportScope is the requested scopes of data.
PassportScopeElementOne requires the specified element be provided.
PassportScopeElementOneOfSeveral allows you to request any one of the requested documents.
No description provided by the author
PhotoConfig contains information about a SendPhoto request.
PhotoSize contains information about photos.
PinChatMessageConfig contains information of a message in a chat to pin.
PreCheckoutConfig conatins information for answerPreCheckoutQuery request.
PreCheckoutQuery contains information about an incoming pre-checkout query.
PromoteChatMemberConfig contains fields to promote members of chat.
ReplyKeyboardHide allows the Bot to hide a custom keyboard.
ReplyKeyboardMarkup allows the Bot to set a custom keyboard.
ReplyKeyboardRemove allows the Bot to hide a custom keyboard.
ResponseParameters are various errors that can be returned in APIResponse.
RestrictChatMemberConfig contains fields to restrict members of chat.
No description provided by the author
SetChatDescriptionConfig contains information for change chat description.
SetChatPhotoConfig contains information for change chat photo.
SetChatTitleConfig contains information for change chat title.
SetGameScoreConfig allows you to update the game score in a chat.
ShippingAddress represents a shipping address.
ShippingConfig contains information for answerShippingQuery request.
ShippingOption represents one shipping option.
ShippingQuery contains information about an incoming shipping query.
Sticker contains information about a sticker.
StickerConfig contains information about a SendSticker request.
SuccessfulPayment contains basic information about a successful payment.
UnpinChatMessageConfig contains information of chat to unpin.
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.
Venue contains information about a venue, including its Location.
VenueConfig contains information about a SendVenue request.
Video contains information about a video.
VideoConfig contains information about a SendVideo request.
VideoNote contains information about a video.
VideoNoteConfig contains information about a SendVideoNote request.
Voice contains information about a voice.
VoiceConfig contains information about a SendVoice request.
WebhookConfig contains information about a SetWebhook request.
WebhookInfo is information about a currently set webhook.

# Interfaces

BotLogger is an interface that represents the required methods to log data.
Chattable is any config type that can be sent.
Fileable is any config type that can be sent that includes a file.
No description provided by the author
PassportScopeElement supports using one or one of several elements.

# Type aliases

No description provided by the author