v0.5.0

Telegram Messages

Last updated


This page covers sending text, deleting messages, and uploading files, photos, and voice with core/telegram.

Text


  1. msg, err := bot.Send(ctx, chatID, replyTo, "*bold*", telegram.WithSendType(telegram.TypeMarkdown))

ParameterMeaning
replyTo int> 0 sends the message as a reply to that message ID; 0 sends a standalone message
opts ...MessageOptionWithSendType(TypeMarkdown) → MarkdownV2, WithSendType(TypeHTML) → HTML; omit for plain text

With MarkdownV2 you must escape Telegram's reserved characters yourself; the library passes the text through unchanged.
Delete(ctx, chatID, msgID) deletes one message and returns the API error unchanged.

Documents, video, audio


  1. msg, err := bot.SendFile(ctx, chatID, telegram.TypeDocument, "./report.pdf", "Q3 report")

FileTypeAPI
TypeDocumentsendDocument
TypeVideosendVideo
TypeAudiosendAudio

The file is opened with os.Open and streamed; the uploaded name is filepath.Base(path). Only the first caption argument is used. An empty path returns path is required; any other FileType value returns unknown file type.

Photos and albums


  1. msgs, err := bot.SendPhoto(ctx, chatID, []string{"a.jpg", "b.jpg"}, "Trip")

Empty entries are dropped first. One remaining path uses sendPhoto; 2 to 10 paths use sendMediaGroup and Telegram shows them as one album with the caption on the first photo. Zero or more than 10 paths return an error. The result is one *models.Message per photo.

Voice


  1. msg, err := bot.SendVoice(ctx, chatID, "./reply.ogg", "caption")

SendVoice uploads an existing file. Telegram renders it as a voice bubble when the file is OGG encoded with OPUS; produce that file yourself (for example with a TTS service and ffmpeg). There is no replyTo parameter.
Any trailing strings are captions. Code written for v0.4.x, SendVoice(ctx, chatID, text, apiKey), still compiles against v0.5.0 but uploads text as a file path and posts apiKey as the caption. See Removed APIs.

中文