message

package
v0.65.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2022 License: MIT Imports: 20 Imported by: 76

Documentation

Overview

Package message contains some useful utilities for creating Telegram messages.

Code generated by mktyping, DO NOT EDIT.

Index

Examples

Constants

View Source
const (
	// DiceEmoticon is an emoticon to send dice sticker.
	DiceEmoticon = "🎲"
	// DartsEmoticon is an emoticon to send darts sticker.
	DartsEmoticon = "🎯"
	// BasketballEmoticon is an emoticon to send basketball sticker.
	BasketballEmoticon = "🏀"
	// FootballEmoticon is an emoticon to send football sticker.
	FootballEmoticon = "⚽"
	// CasinoEmoticon is an emoticon to send casino sticker.
	CasinoEmoticon = "🎰"
	// BowlingEmoticon is an emoticon to send bowling sticker.
	BowlingEmoticon = "🎳"
)
View Source
const (
	// DefaultVoiceMIME is a default voice MIME to use.
	DefaultVoiceMIME = "audio/ogg"

	// DefaultAudioMIME is a default audio MIME to use.
	DefaultAudioMIME = "audio/mp3"

	// DefaultVideoMIME is a default video MIME to use.
	DefaultVideoMIME = "video/mp4"

	// DefaultGifMIME is a default gif MIME to use.
	DefaultGifMIME = "image/gif"

	// DefaultStickerMIME is a default sticker MIME to use.
	DefaultStickerMIME = "image/webp"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AnswerableMessageUpdate added in v0.26.0

type AnswerableMessageUpdate interface {
	GetMessage() tg.MessageClass
}

AnswerableMessageUpdate represents update which can be used to answer.

type AudioDocumentBuilder added in v0.26.0

type AudioDocumentBuilder struct {
	// contains filtered or unexported fields
}

AudioDocumentBuilder is an Audio media option.

func Audio added in v0.26.0

func Audio(file tg.InputFileClass, caption ...StyledTextOption) *AudioDocumentBuilder

Audio adds audio attachment.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	"github.com/go-faster/errors"

	"github.com/gotd/td/telegram"
	"github.com/gotd/td/telegram/message"
	"github.com/gotd/td/telegram/uploader"
	"github.com/gotd/td/tg"
)

func sendAudio(ctx context.Context) error {
	client, err := telegram.ClientFromEnvironment(telegram.Options{})
	if err != nil {
		return err
	}

	return client.Run(ctx, func(ctx context.Context) error {
		raw := tg.NewClient(client)
		// Upload file.
		f, err := uploader.NewUploader(raw).FromPath(ctx, "vsyo idyot po planu.mp3")
		if err != nil {
			return errors.Wrap(err, "upload")
		}

		sender := message.NewSender(raw)
		r := sender.Resolve("@durovschat")

		// Sends audio to the @durovschat.
		if _, err := r.Audio(ctx, f); err != nil {
			return err
		}

		// Sends audio with title to the @durovschat.
		if _, err := r.Media(ctx, message.Audio(f).
			Performer("Yegor Letov").
			Title("Everything is going according to plan")); err != nil {
			return err
		}

		// Sends voice message to the @durovschat.
		if _, err := r.Voice(ctx, f); err != nil {
			return err
		}

		return nil
	})
}

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	if err := sendAudio(ctx); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(2)
	}
}
Output:

func Voice added in v0.26.0

Voice adds voice attachment.

func (*AudioDocumentBuilder) Duration added in v0.26.0

func (u *AudioDocumentBuilder) Duration(duration time.Duration) *AudioDocumentBuilder

Duration sets duration of audio file.

func (*AudioDocumentBuilder) DurationSeconds added in v0.26.0

func (u *AudioDocumentBuilder) DurationSeconds(duration int) *AudioDocumentBuilder

DurationSeconds sets duration in seconds.

func (*AudioDocumentBuilder) Performer added in v0.26.0

func (u *AudioDocumentBuilder) Performer(performer string) *AudioDocumentBuilder

Performer sets performer.

func (*AudioDocumentBuilder) Title added in v0.26.0

Title sets name of song.

func (*AudioDocumentBuilder) Voice added in v0.26.0

Voice sets flag to mark this audio as voice message.

func (*AudioDocumentBuilder) Waveform added in v0.26.0

func (u *AudioDocumentBuilder) Waveform(waveform []byte) *AudioDocumentBuilder

Waveform sets waveform representation of the voice message.

type Builder added in v0.26.0

type Builder struct {
	// contains filtered or unexported fields
}

Builder is a message builder.

func (*Builder) Album added in v0.26.0

func (b *Builder) Album(ctx context.Context, media MultiMediaOption, album ...MultiMediaOption) (tg.UpdatesClass, error)

Album sends message with multiple media attachments.

func (*Builder) Audio added in v0.26.0

func (b *Builder) Audio(
	ctx context.Context,
	file tg.InputFileClass, caption ...StyledTextOption,
) (tg.UpdatesClass, error)

Audio sends audio file.

func (*Builder) Background added in v0.26.0

func (b *Builder) Background() *Builder

Background sets flag to send this message as background message.

func (*Builder) Basketball added in v0.26.0

func (b *Builder) Basketball(ctx context.Context) (tg.UpdatesClass, error)

Basketball sends a basketball animated sticker.

func (*Builder) Bowling added in v0.33.0

func (b *Builder) Bowling(ctx context.Context) (tg.UpdatesClass, error)

Bowling sends a bowling animated sticker.

func (*Builder) Casino added in v0.33.0

func (b *Builder) Casino(ctx context.Context) (tg.UpdatesClass, error)

Casino sends a casino animated sticker.

func (*Builder) Clear added in v0.26.0

func (b *Builder) Clear() *Builder

Clear sets flag to clear the draft field.

func (*Builder) ClearDraft added in v0.26.0

func (b *Builder) ClearDraft(ctx context.Context) error

ClearDraft clears draft. Also, you can use Clear() builder option with any other message send method.

See https://core.telegram.org/api/drafts#clearing-drafts.

func (*Builder) Darts added in v0.26.0

func (b *Builder) Darts(ctx context.Context) (tg.UpdatesClass, error)

Darts sends a darts animated sticker.

func (*Builder) Dice added in v0.26.0

func (b *Builder) Dice(ctx context.Context) (tg.UpdatesClass, error)

Dice sends a dice animated sticker.

func (*Builder) Document added in v0.26.0

func (b *Builder) Document(
	ctx context.Context, file FileLocation, caption ...StyledTextOption,
) (tg.UpdatesClass, error)

Document sends document.

func (*Builder) DocumentByHash added in v0.26.0

func (b *Builder) DocumentByHash(
	ctx context.Context, hash []byte, size int64, mime string,
	caption ...StyledTextOption,
) (tg.UpdatesClass, error)

DocumentByHash finds document by hash and sends as attachment.

func (*Builder) DocumentExternal added in v0.26.0

func (b *Builder) DocumentExternal(ctx context.Context, url string, caption ...StyledTextOption) (tg.UpdatesClass, error)

DocumentExternal sends document attachment that will be downloaded by the Telegram servers.

func (*Builder) Edit added in v0.26.0

func (b *Builder) Edit(id int) *EditMessageBuilder

Edit edits message by ID.

func (*Builder) File added in v0.26.0

func (b *Builder) File(ctx context.Context, file tg.InputFileClass, caption ...StyledTextOption) (tg.UpdatesClass, error)

File sends uploaded file as document and forces it to be used as plain file, not media.

func (*Builder) Football added in v0.33.0

func (b *Builder) Football(ctx context.Context) (tg.UpdatesClass, error)

Football sends a football animated sticker.

func (*Builder) ForwardIDs added in v0.26.0

func (b *Builder) ForwardIDs(from tg.InputPeerClass, id int, ids ...int) *ForwardBuilder

ForwardIDs creates builder to forward messages by ID.

func (*Builder) ForwardMessages added in v0.26.0

func (b *Builder) ForwardMessages(from tg.InputPeerClass, msg tg.MessageClass, m ...tg.MessageClass) *ForwardBuilder

ForwardMessages creates builder to forward messages.

func (*Builder) GIF added in v0.26.0

func (b *Builder) GIF(
	ctx context.Context,
	file tg.InputFileClass, caption ...StyledTextOption,
) (tg.UpdatesClass, error)

GIF sends gif.

func (*Builder) InlineResult added in v0.26.0

func (b *Builder) InlineResult(ctx context.Context, id string, queryID int64, hideVia bool) (tg.UpdatesClass, error)

InlineResult is a user method to send bot inline query result message.

func (*Builder) Markup added in v0.26.0

func (b *Builder) Markup(m tg.ReplyMarkupClass) *Builder

Markup sets reply markup for sending bot buttons.

NB: markup will not be used, if you send multiple media attachments.

func (*Builder) Media added in v0.26.0

func (b *Builder) Media(ctx context.Context, media MediaOption) (tg.UpdatesClass, error)

Media sends message with media attachment.

func (*Builder) NoForwards added in v0.54.0

func (b *Builder) NoForwards() *Builder

NoForwards whether that sent message cannot be forwarded.

See https://telegram.org/blog/protected-content-delete-by-date-and-more#protected-content-in-groups-and-channels.

func (*Builder) NoWebpage added in v0.26.0

func (b *Builder) NoWebpage() *Builder

NoWebpage sets flag to disable generation of the webpage preview.

func (*Builder) Photo added in v0.26.0

func (b *Builder) Photo(
	ctx context.Context,
	photo FileLocation, caption ...StyledTextOption,
) (tg.UpdatesClass, error)

Photo sends photo.

func (*Builder) PhotoExternal added in v0.26.0

func (b *Builder) PhotoExternal(
	ctx context.Context,
	url string, caption ...StyledTextOption,
) (tg.UpdatesClass, error)

PhotoExternal sends photo attachment which will be downloaded by the Telegram servers.

func (*Builder) Reply added in v0.26.0

func (b *Builder) Reply(id int) *Builder

Reply sets message ID to reply.

func (*Builder) ReplyMsg added in v0.26.0

func (b *Builder) ReplyMsg(msg tg.MessageClass) *Builder

ReplyMsg sets message to reply.

func (*Builder) RoundVideo added in v0.26.0

func (b *Builder) RoundVideo(
	ctx context.Context,
	file tg.InputFileClass, caption ...StyledTextOption,
) (tg.UpdatesClass, error)

RoundVideo sends round video.

func (*Builder) Row added in v0.26.0

func (b *Builder) Row(buttons ...tg.KeyboardButtonClass) *Builder

Row sets single row keyboard markup for sending bot buttons.

NB: markup will not be used, if you send multiple media attachments.

func (*Builder) SaveDraft added in v0.26.0

func (b *Builder) SaveDraft(ctx context.Context, msg string) error

SaveDraft saves given message as draft.

See https://core.telegram.org/api/drafts#saving-drafts.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	"github.com/go-faster/errors"

	"github.com/gotd/td/telegram"
	"github.com/gotd/td/telegram/message"
	"github.com/gotd/td/telegram/message/styling"
	"github.com/gotd/td/tg"
)

func saveDraft(ctx context.Context) error {
	client, err := telegram.ClientFromEnvironment(telegram.Options{})
	if err != nil {
		return err
	}

	return client.Run(ctx, func(ctx context.Context) error {
		sender := message.NewSender(tg.NewClient(client))
		r := sender.Resolve("@durov")

		// Save draft message.
		if err := r.SaveDraft(ctx, "Hi!"); err != nil {
			return errors.Wrap(err, "draft")
		}

		// Save styled draft message.
		if err := r.SaveStyledDraft(ctx, styling.Bold("Hi!")); err != nil {
			return errors.Wrap(err, "draft")
		}

		// Clear draft for resolved @durov peer.
		if err := r.ClearDraft(ctx); err != nil {
			return errors.Wrap(err, "draft")
		}

		return nil
	})
}

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	if err := saveDraft(ctx); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(2)
	}
}
Output:

func (*Builder) SaveStyledDraft added in v0.26.0

func (b *Builder) SaveStyledDraft(ctx context.Context, texts ...StyledTextOption) error

SaveStyledDraft saves given styled message as draft.

See https://core.telegram.org/api/drafts#saving-drafts.

func (*Builder) Schedule added in v0.26.0

func (b *Builder) Schedule(date time.Time) *Builder

Schedule sets scheduled message date for scheduled messages.

func (*Builder) ScheduleTS added in v0.26.0

func (b *Builder) ScheduleTS(date int) *Builder

ScheduleTS sets scheduled message timestamp for scheduled messages.

func (*Builder) SendAs added in v0.54.0

func (b *Builder) SendAs(p tg.InputPeerClass) *Builder

SendAs sets peer to send as.

See https://telegram.org/blog/protected-content-delete-by-date-and-more#anonymous-posting-in-public-groups.

func (*Builder) Silent added in v0.26.0

func (b *Builder) Silent() *Builder

Silent sets flag to send this message silently (no notifications for the receivers).

func (*Builder) StyledText added in v0.26.0

func (b *Builder) StyledText(ctx context.Context, texts ...StyledTextOption) (tg.UpdatesClass, error)

StyledText sends styled text message.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	"github.com/gotd/td/telegram"
	"github.com/gotd/td/telegram/message"
	"github.com/gotd/td/telegram/message/styling"
	"github.com/gotd/td/tg"
)

func sendStyledText(ctx context.Context) error {
	client, err := telegram.ClientFromEnvironment(telegram.Options{})
	if err != nil {
		return err
	}

	// This example creates a big message with different style lines
	// and sends to your Saved Messages folder.
	return client.Run(ctx, func(ctx context.Context) error {
		formats := []message.StyledTextOption{
			styling.Plain("plaintext"), styling.Plain("\n\n"),
			styling.Mention("@durov"), styling.Plain("\n\n"),
			styling.Hashtag("#hashtag"), styling.Plain("\n\n"),
			styling.BotCommand("/command"), styling.Plain("\n\n"),
			styling.URL("https://google.org"), styling.Plain("\n\n"),
			styling.Email("example@example.org"), styling.Plain("\n\n"),
			styling.Bold("bold"), styling.Plain("\n\n"),
			styling.Italic("italic"), styling.Plain("\n\n"),
			styling.Underline("underline"), styling.Plain("\n\n"),
			styling.Strike("strike"), styling.Plain("\n\n"),
			styling.Code("fmt.Println(`Hello, World!`)"), styling.Plain("\n\n"),
			styling.Pre("fmt.Println(`Hello, World!`)", "Go"), styling.Plain("\n\n"),
			styling.TextURL("clickme", "https://google.com"), styling.Plain("\n\n"),
			styling.Phone("+71234567891"), styling.Plain("\n\n"),
			styling.Cashtag("$CASHTAG"), styling.Plain("\n\n"),
			styling.Blockquote("blockquote"), styling.Plain("\n\n"),
			styling.BankCard("5550111111111111"), styling.Plain("\n\n"),
		}

		_, err := message.NewSender(tg.NewClient(client)).
			Self().StyledText(ctx, formats...)
		return err
	})
}

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	if err := sendStyledText(ctx); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(2)
	}
}
Output:

func (*Builder) Text added in v0.26.0

func (b *Builder) Text(ctx context.Context, msg string) (tg.UpdatesClass, error)

Text sends text message.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	"github.com/gotd/td/telegram"
	"github.com/gotd/td/telegram/message"
	"github.com/gotd/td/telegram/message/styling"
)

func sendText(ctx context.Context) error {
	client, err := telegram.ClientFromEnvironment(telegram.Options{})
	if err != nil {
		return err
	}

	return client.Run(ctx, func(ctx context.Context) error {
		s := message.NewSender(client.API())

		// Sends message to your Saved Message folder.
		if _, err := s.Self().Text(ctx, "Hi!"); err != nil {
			return err
		}

		// Resolves @my_channel and gets tg.InputPeerClass.
		p, err := s.Resolve("my_channel").AsInputPeer(ctx)
		if err != nil {
			return err
		}

		// Replies to message ID = 1 in @gotd_en as @my_channel with spoiler message "spoiler".
		if _, err := s.Resolve("gotd_en").
			SendAs(p).Reply(1).
			StyledText(ctx, styling.Spoiler("spoiler")); err != nil {
			return err
		}

		// Sends message to channel
		return err
	})
}

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	if err := sendText(ctx); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(2)
	}
}
Output:

func (*Builder) Textf added in v0.30.0

func (b *Builder) Textf(ctx context.Context, format string, args ...interface{}) (tg.UpdatesClass, error)

Textf formats and sends text message.

func (*Builder) Upload added in v0.27.0

func (b *Builder) Upload(upd UploadOption) *UploadBuilder

Upload creates new UploadBuilder to upload and send attachments. Given option will be called only once, even if you call upload functions.

func (*Builder) UploadMedia added in v0.26.0

func (b *Builder) UploadMedia(ctx context.Context, media MediaOption) (tg.MessageMediaClass, error)

UploadMedia uses messages.uploadMedia to upload a file and associate it to a chat (without actually sending it to the chat).

See https://core.telegram.org/method/messages.uploadMedia.

func (*Builder) UploadedPhoto added in v0.26.0

func (b *Builder) UploadedPhoto(
	ctx context.Context,
	file tg.InputFileClass, caption ...StyledTextOption,
) (tg.UpdatesClass, error)

UploadedPhoto sends uploaded file as photo.

func (*Builder) UploadedSticker added in v0.26.0

func (b *Builder) UploadedSticker(
	ctx context.Context,
	file tg.InputFileClass, caption ...StyledTextOption,
) (tg.UpdatesClass, error)

UploadedSticker sends uploaded file as sticker.

func (*Builder) Video added in v0.26.0

func (b *Builder) Video(
	ctx context.Context,
	file tg.InputFileClass, caption ...StyledTextOption,
) (tg.UpdatesClass, error)

Video sends video.

func (*Builder) Voice added in v0.26.0

func (b *Builder) Voice(ctx context.Context, file tg.InputFileClass) (tg.UpdatesClass, error)

Voice sends voice message.

type DeleteBuilder added in v0.33.0

type DeleteBuilder struct {
	// contains filtered or unexported fields
}

DeleteBuilder is an intermediate builder to delete messages. Unlike RevokeBuilder will keep messages for other users.

func (*DeleteBuilder) Messages added in v0.33.0

func (b *DeleteBuilder) Messages(ctx context.Context, ids ...int) (*tg.MessagesAffectedMessages, error)

Messages deletes messages by given IDs, but keeps it for other users.

NB: Telegram counts message IDs globally for private chats (but not for channels). This method does not check that all given message IDs from one chat.

type DocumentBuilder added in v0.26.0

type DocumentBuilder struct {
	// contains filtered or unexported fields
}

DocumentBuilder is a Document media option.

func Document added in v0.26.0

func Document(doc FileLocation, caption ...StyledTextOption) *DocumentBuilder

Document adds document attachment.

func (*DocumentBuilder) Query added in v0.26.0

func (u *DocumentBuilder) Query(query string) *DocumentBuilder

Query sets query field of InputMediaDocument.

func (*DocumentBuilder) TTL added in v0.26.0

TTL sets time to live of self-destructing document.

func (*DocumentBuilder) TTLSeconds added in v0.26.0

func (u *DocumentBuilder) TTLSeconds(ttl int) *DocumentBuilder

TTLSeconds sets time to live in seconds of self-destructing document.

type DocumentExternalBuilder added in v0.26.0

type DocumentExternalBuilder struct {
	// contains filtered or unexported fields
}

DocumentExternalBuilder is a DocumentExternal media option.

func DocumentExternal added in v0.26.0

func DocumentExternal(url string, caption ...StyledTextOption) *DocumentExternalBuilder

DocumentExternal adds document attachment that will be downloaded by the Telegram servers.

func (*DocumentExternalBuilder) TTL added in v0.26.0

TTL sets time to live of self-destructing document.

func (*DocumentExternalBuilder) TTLSeconds added in v0.26.0

TTLSeconds sets time to live in seconds of self-destructing document.

type EditMessageBuilder added in v0.26.0

type EditMessageBuilder struct {
	// contains filtered or unexported fields
}

EditMessageBuilder creates edit message builder.

func (*EditMessageBuilder) Media added in v0.26.0

Media edits message using given media and text.

func (*EditMessageBuilder) StyledText added in v0.26.0

func (b *EditMessageBuilder) StyledText(ctx context.Context, texts ...StyledTextOption) (tg.UpdatesClass, error)

StyledText edits message using given message.

func (*EditMessageBuilder) Text added in v0.26.0

Text edits message.

func (*EditMessageBuilder) Textf added in v0.30.0

func (b *EditMessageBuilder) Textf(ctx context.Context, format string, args ...interface{}) (tg.UpdatesClass, error)

Textf formats and edits message .

type FileLocation added in v0.26.0

type FileLocation interface {
	GetID() (value int64)
	GetAccessHash() (value int64)
	GetFileReference() (value []byte)
}

FileLocation is an abstraction of Telegram file location.

type FilePromise added in v0.38.0

type FilePromise = func(ctx context.Context, b Uploader) (tg.InputFileClass, error)

FilePromise is a upload file promise.

type ForwardBuilder added in v0.26.0

type ForwardBuilder struct {
	// contains filtered or unexported fields
}

ForwardBuilder is a forward request builder.

func (*ForwardBuilder) Send added in v0.26.0

Send sends forwarded messages.

func (*ForwardBuilder) WithMyScore added in v0.26.0

func (b *ForwardBuilder) WithMyScore() *ForwardBuilder

WithMyScore sets flag to include your score in the forwarded game.

type InlineUpdate added in v0.31.0

type InlineUpdate interface {
	GetQueryID() int64
}

InlineUpdate is an abstraction for

type MediaOption added in v0.26.0

type MediaOption interface {
	// contains filtered or unexported methods
}

MediaOption is an option for sending media attachments.

func Basketball added in v0.26.0

func Basketball() MediaOption

Basketball adds a basketball animated sticker.

func Bowling added in v0.33.0

func Bowling() MediaOption

Bowling adds a bowling animated sticker.

func Casino added in v0.33.0

func Casino() MediaOption

Casino adds a casino animated sticker.

func Contact added in v0.26.0

func Contact(contact tg.InputMediaContact, caption ...StyledTextOption) MediaOption

Contact adds contact attachment.

func Darts added in v0.26.0

func Darts() MediaOption

Darts adds a darts animated sticker.

func Dice added in v0.26.0

func Dice() MediaOption

Dice adds a dice animated sticker.

func Football added in v0.33.0

func Football() MediaOption

Football adds a football animated sticker.

func Game added in v0.26.0

func Game(id tg.InputGameClass, caption ...StyledTextOption) MediaOption

Game adds a game attachment.

func GeoPoint added in v0.26.0

func GeoPoint(lat, long float64, accuracy int, caption ...StyledTextOption) MediaOption

GeoPoint adds geo point attachment. NB: parameter accuracy may be zero and will not be used.

func Media added in v0.26.0

func Media(media tg.InputMediaClass, caption ...StyledTextOption) MediaOption

Media adds given media attachment to message.

func MediaDice added in v0.26.0

func MediaDice(emoticon string) MediaOption

MediaDice adds a dice-based animated sticker.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	"github.com/gotd/td/telegram"
	"github.com/gotd/td/telegram/message"
	"github.com/gotd/td/tg"
)

func sendDice(ctx context.Context) error {
	client, err := telegram.ClientFromEnvironment(telegram.Options{})
	if err != nil {
		return err
	}

	return client.Run(ctx, func(ctx context.Context) error {
		sender := message.NewSender(tg.NewClient(client))

		// Sends dice "🎲" to the @durovschat.
		if _, err := sender.Resolve("@durovschat").Dice(ctx); err != nil {
			return err
		}

		// Sends darts "🎯" to the @durovschat.
		if _, err := sender.Resolve("https://t.me/durovschat").Darts(ctx); err != nil {
			return err
		}

		return nil
	})
}

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	if err := sendDice(ctx); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(2)
	}
}
Output:

type MultiMediaOption added in v0.26.0

type MultiMediaOption interface {
	MediaOption
	// contains filtered or unexported methods
}

MultiMediaOption is an option for sending albums.

func ForceMulti added in v0.26.0

func ForceMulti(opt MediaOption) MultiMediaOption

ForceMulti converts MediaOption to MultiMediaOption. It can produce unexpected RPC errors. Use carefully.

type PeerUpdate added in v0.31.0

type PeerUpdate interface {
	GetPeer() tg.PeerClass
}

PeerUpdate represents update which can be used to answer.

type PhotoBuilder added in v0.26.0

type PhotoBuilder struct {
	// contains filtered or unexported fields
}

PhotoBuilder is a Photo media option.

func Photo added in v0.26.0

func Photo(photo FileLocation, caption ...StyledTextOption) *PhotoBuilder

Photo adds photo attachment.

func (*PhotoBuilder) TTL added in v0.26.0

func (u *PhotoBuilder) TTL(ttl time.Duration) *PhotoBuilder

TTL sets time to live of self-destructing photo.

func (*PhotoBuilder) TTLSeconds added in v0.26.0

func (u *PhotoBuilder) TTLSeconds(ttl int) *PhotoBuilder

TTLSeconds sets time to live in seconds of self-destructing photo.

type PhotoExternalBuilder added in v0.26.0

type PhotoExternalBuilder struct {
	// contains filtered or unexported fields
}

PhotoExternalBuilder is a PhotoExternal media option.

func PhotoExternal added in v0.26.0

func PhotoExternal(url string, caption ...StyledTextOption) *PhotoExternalBuilder

PhotoExternal adds photo attachment which will be downloaded by the Telegram servers.

func (*PhotoExternalBuilder) TTL added in v0.26.0

TTL sets time to live of self-destructing document.

func (*PhotoExternalBuilder) TTLSeconds added in v0.26.0

func (u *PhotoExternalBuilder) TTLSeconds(ttl int) *PhotoExternalBuilder

TTLSeconds sets time to live in seconds of self-destructing document.

type PollAnswerOption added in v0.26.0

type PollAnswerOption func(p *pollAnswerBuilder)

PollAnswerOption is an option to create poll answer.

func CorrectPollAnswer added in v0.26.0

func CorrectPollAnswer(text string) PollAnswerOption

CorrectPollAnswer creates new correct poll answer option.

func PollAnswer added in v0.26.0

func PollAnswer(text string) PollAnswerOption

PollAnswer creates new plain poll answer option.

func RawPollAnswer added in v0.26.0

func RawPollAnswer(poll tg.PollAnswer) PollAnswerOption

RawPollAnswer creates new raw poll answer option.

type PollBuilder added in v0.26.0

type PollBuilder struct {
	// contains filtered or unexported fields
}

PollBuilder is a Poll media option.

func Poll added in v0.26.0

func Poll(question string, a, b PollAnswerOption, answers ...PollAnswerOption) *PollBuilder

Poll adds poll attachment.

func (*PollBuilder) Close added in v0.26.0

func (p *PollBuilder) Close() *PollBuilder

Close sets flag that the poll is closed and doesn't accept any more answers.

func (*PollBuilder) CloseDate added in v0.26.0

func (p *PollBuilder) CloseDate(d time.Time) *PollBuilder

CloseDate sets point in time (Unix timestamp) when the poll will be automatically closed. Must be at least 5 and no more than 600 seconds in the future.

func (*PollBuilder) CloseDateTS added in v0.26.0

func (p *PollBuilder) CloseDateTS(ts int) *PollBuilder

CloseDateTS sets point in time (Unix timestamp) when the poll will be automatically closed. Must be at least 5 and no more than 600 seconds in the future.

func (*PollBuilder) ClosePeriod added in v0.26.0

func (p *PollBuilder) ClosePeriod(d time.Duration) *PollBuilder

ClosePeriod sets amount of time in seconds the poll will be active after creation, 5-600 seconds.

func (*PollBuilder) ClosePeriodSeconds added in v0.26.0

func (p *PollBuilder) ClosePeriodSeconds(s int) *PollBuilder

ClosePeriodSeconds sets amount of time in seconds the poll will be active after creation, 5-600.

func (*PollBuilder) Explanation added in v0.26.0

func (p *PollBuilder) Explanation(msg string) *PollBuilder

Explanation sets explanation message.

func (*PollBuilder) MultipleChoice added in v0.26.0

func (p *PollBuilder) MultipleChoice(multipleChoice bool) *PollBuilder

MultipleChoice sets flag that multiple options can be chosen as answer.

func (*PollBuilder) PollID added in v0.26.0

func (p *PollBuilder) PollID() int64

PollID return poll ID. If poll was not sent, will be zero. It useful to close polls.

func (*PollBuilder) PublicVoters added in v0.26.0

func (p *PollBuilder) PublicVoters(publicVoters bool) *PollBuilder

PublicVoters sets flag that votes are publicly visible to all users (non-anonymous poll).

func (*PollBuilder) StyledExplanation added in v0.26.0

func (p *PollBuilder) StyledExplanation(texts ...StyledTextOption) *PollBuilder

StyledExplanation sets styled explanation message.

type ReportBuilder added in v0.26.0

type ReportBuilder struct {
	// contains filtered or unexported fields
}

ReportBuilder is a reporting messages helper.

func (*ReportBuilder) ChildAbuse added in v0.26.0

func (b *ReportBuilder) ChildAbuse(ctx context.Context) (bool, error)

ChildAbuse sends report for child abuse.

func (*ReportBuilder) Copyright added in v0.26.0

func (b *ReportBuilder) Copyright(ctx context.Context) (bool, error)

Copyright sends report for copyrighted content.

func (*ReportBuilder) Fake added in v0.26.0

func (b *ReportBuilder) Fake(ctx context.Context) (bool, error)

Fake sends report for fake.

func (*ReportBuilder) GeoIrrelevant added in v0.26.0

func (b *ReportBuilder) GeoIrrelevant(ctx context.Context) (bool, error)

GeoIrrelevant sends report for irrelevant geogroup.

func (*ReportBuilder) Message added in v0.26.0

func (b *ReportBuilder) Message(msg string) *ReportBuilder

Message sets additional comment for report.

func (*ReportBuilder) Other added in v0.26.0

func (b *ReportBuilder) Other(ctx context.Context) (bool, error)

Other sends report for other.

func (*ReportBuilder) Pornography added in v0.26.0

func (b *ReportBuilder) Pornography(ctx context.Context) (bool, error)

Pornography sends report for pornography.

func (*ReportBuilder) Spam added in v0.26.0

func (b *ReportBuilder) Spam(ctx context.Context) (bool, error)

Spam sends report for spam.

func (*ReportBuilder) Violence added in v0.26.0

func (b *ReportBuilder) Violence(ctx context.Context) (bool, error)

Violence sends report for violence.

type RequestBuilder added in v0.26.0

type RequestBuilder struct {
	Builder
}

RequestBuilder is an intermediate builder to make different RPC calls using Sender.

func (*RequestBuilder) AsInputChannel added in v0.35.0

func (b *RequestBuilder) AsInputChannel(ctx context.Context) (*tg.InputChannel, error)

AsInputChannel returns resolve result as InputChannel.

func (*RequestBuilder) AsInputChannelClass added in v0.35.0

func (b *RequestBuilder) AsInputChannelClass(ctx context.Context) (tg.InputChannelClass, error)

AsInputChannelClass returns resolve result as tg.NotEmptyInputChannel.

func (*RequestBuilder) AsInputPeer added in v0.26.0

func (b *RequestBuilder) AsInputPeer(ctx context.Context) (tg.InputPeerClass, error)

AsInputPeer returns resolve result as InputPeerClass.

func (*RequestBuilder) AsInputUser added in v0.35.0

func (b *RequestBuilder) AsInputUser(ctx context.Context) (*tg.InputUser, error)

AsInputUser returns resolve result as InputUser.

func (*RequestBuilder) AsInputUserClass added in v0.35.0

func (b *RequestBuilder) AsInputUserClass(ctx context.Context) (tg.InputUserClass, error)

AsInputUserClass returns resolve result as InputUserClass.

func (*RequestBuilder) CloneBuilder added in v0.50.0

func (b *RequestBuilder) CloneBuilder() *Builder

CloneBuilder returns copy of message Builder inside RequestBuilder.

func (*RequestBuilder) Join added in v0.30.0

Join joins resolved channel. NB: if resolved peer is not a channel, error will be returned.

func (*RequestBuilder) Leave added in v0.30.0

Leave leaves resolved peer.

NB: if resolved peer is not a channel or chat, or chat is deactivated, empty *tg.Updates will be returned.

func (*RequestBuilder) PeerSettings added in v0.26.0

func (b *RequestBuilder) PeerSettings(ctx context.Context) (*tg.PeerSettings, error)

PeerSettings returns peer settings.

func (*RequestBuilder) PollVote added in v0.26.0

func (b *RequestBuilder) PollVote(
	ctx context.Context, msgID int,
	answer []byte, answers ...[]byte,
) (tg.UpdatesClass, error)

PollVote votes in a poll.

func (*RequestBuilder) Reaction added in v0.54.0

func (b *RequestBuilder) Reaction(ctx context.Context, msgID int, reaction string) (tg.UpdatesClass, error)

Reaction sends reaction for given message.

func (*RequestBuilder) Report added in v0.26.0

func (b *RequestBuilder) Report(id int, ids ...int) *ReportBuilder

Report reports messages in a chat for violation of Telegram's Terms of Service.

func (*RequestBuilder) ReportSpam added in v0.26.0

func (b *RequestBuilder) ReportSpam(ctx context.Context) (bool, error)

ReportSpam reports peer for spam. NB: You should check that the peer settings of the chat allow us to do that.

func (*RequestBuilder) Revoke added in v0.33.0

func (b *RequestBuilder) Revoke() *RevokeBuilder

Revoke creates new RevokeBuilder.

func (*RequestBuilder) Scheduled added in v0.26.0

func (b *RequestBuilder) Scheduled() *ScheduledManager

Scheduled creates new ScheduledManager using resolved peer.

func (*RequestBuilder) ScreenshotNotify added in v0.26.0

func (b *RequestBuilder) ScreenshotNotify(ctx context.Context, msgID int) (tg.UpdatesClass, error)

ScreenshotNotify sends notification about screenshot. Parameter msgID is an ID of message that was screenshotted, can be 0.

func (*RequestBuilder) TypingAction added in v0.26.0

func (b *RequestBuilder) TypingAction() *TypingActionBuilder

TypingAction creates TypingActionBuilder.

type RevokeBuilder added in v0.33.0

type RevokeBuilder struct {
	// contains filtered or unexported fields
}

RevokeBuilder is an intermediate builder to delete messages. Unlike DeleteBuilder will not keep messages for other users.

func (*RevokeBuilder) Messages added in v0.33.0

func (b *RevokeBuilder) Messages(ctx context.Context, ids ...int) (*tg.MessagesAffectedMessages, error)

Messages deletes messages by given IDs.

NB: Telegram counts message IDs globally for private chats (but not for channels). This method does not check that all given message IDs from one chat.

type ScheduledManager added in v0.26.0

type ScheduledManager struct {
	// contains filtered or unexported fields
}

ScheduledManager is a scheduled messages manager.

func (*ScheduledManager) Delete added in v0.26.0

func (m *ScheduledManager) Delete(ctx context.Context, id int, ids ...int) (tg.UpdatesClass, error)

Delete deletes scheduled messages.

func (*ScheduledManager) Get added in v0.26.0

Get gets scheduled messages.

func (*ScheduledManager) History added in v0.26.0

History gets scheduled messages history.

func (*ScheduledManager) HistoryWithHash added in v0.26.0

func (m *ScheduledManager) HistoryWithHash(ctx context.Context, hash int64) (tg.ModifiedMessagesMessages, error)

HistoryWithHash gets scheduled messages history.

func (*ScheduledManager) Send added in v0.26.0

func (m *ScheduledManager) Send(ctx context.Context, id int, ids ...int) (tg.UpdatesClass, error)

Send sends scheduled messages.

type SearchDocumentBuilder added in v0.26.0

type SearchDocumentBuilder struct {
	// contains filtered or unexported fields
}

SearchDocumentBuilder is a Document media option which uses messages.getDocumentByHash to find document.

See https://core.telegram.org/method/messages.getDocumentByHash.

See https://core.telegram.org/api/files#re-using-pre-uploaded-files.

func DocumentByHash added in v0.26.0

func DocumentByHash(
	hash []byte, size int64, mime string,
	caption ...StyledTextOption,
) *SearchDocumentBuilder

DocumentByHash finds document by hash and adds as attachment.

See https://core.telegram.org/method/messages.getDocumentByHash.

See https://core.telegram.org/api/files#re-using-pre-uploaded-files.

func (*SearchDocumentBuilder) Query added in v0.26.0

Query sets query field of InputMediaDocument.

func (*SearchDocumentBuilder) TTL added in v0.26.0

TTL sets time to live of self-destructing document.

func (*SearchDocumentBuilder) TTLSeconds added in v0.26.0

func (u *SearchDocumentBuilder) TTLSeconds(ttl int) *SearchDocumentBuilder

TTLSeconds sets time to live in seconds of self-destructing document.

type Sender added in v0.26.0

type Sender struct {
	// contains filtered or unexported fields
}

Sender is a message sending helper.

func NewSender added in v0.26.0

func NewSender(raw *tg.Client) *Sender

NewSender creates a new Sender.

func (*Sender) Answer added in v0.26.0

func (s *Sender) Answer(
	uctx tg.Entities,
	upd AnswerableMessageUpdate,
	decorators ...peer.PromiseDecorator,
) *RequestBuilder

Answer uses given message update to create message for same chat.

func (*Sender) ClearAllDrafts added in v0.26.0

func (s *Sender) ClearAllDrafts(ctx context.Context) error

ClearAllDrafts clears all drafts in all peers.

func (*Sender) Delete added in v0.33.0

func (s *Sender) Delete() *DeleteBuilder

Delete creates new DeleteBuilder.

func (*Sender) Inline added in v0.31.0

func (s *Sender) Inline(upd InlineUpdate) *inline.ResultBuilder

Inline creates new inline.ResultBuilder using given update.

func (*Sender) JoinHash added in v0.30.0

func (s *Sender) JoinHash(ctx context.Context, hash string) (tg.UpdatesClass, error)

JoinHash joins to private chat using given hash.

func (s *Sender) JoinLink(ctx context.Context, link string) (tg.UpdatesClass, error)

JoinLink joins to private chat using given link or hash. Input examples:

t.me/+AAAAAAAAAAAAAAAA
https://t.me/+AAAAAAAAAAAAAAAA
t.me/joinchat/AAAAAAAAAAAAAAAA
https://t.me/joinchat/AAAAAAAAAAAAAAAA
tg:join?invite=AAAAAAAAAAAAAAAA
tg://join?invite=AAAAAAAAAAAAAAAA

func (*Sender) Peer added in v0.26.0

func (s *Sender) Peer(uctx tg.Entities, upd PeerUpdate, decorators ...peer.PromiseDecorator) *RequestBuilder

Peer uses given peer update to create message for same chat.

func (*Sender) PeerPromise added in v0.27.0

func (s *Sender) PeerPromise(p peer.Promise, decorators ...peer.PromiseDecorator) *RequestBuilder

PeerPromise uses given peer promise to create new message builder.

func (*Sender) Reply added in v0.26.0

func (s *Sender) Reply(uctx tg.Entities, upd AnswerableMessageUpdate, decorators ...peer.PromiseDecorator) *Builder

Reply uses given message update to create message for same chat and create a reply. Shorthand for

sender.Answer(uctx, upd).ReplyMsg(upd.GetMessage())

func (*Sender) Resolve added in v0.26.0

func (s *Sender) Resolve(from string, decorators ...peer.PromiseDecorator) *RequestBuilder

Resolve uses given text to create new message builder. It resolves peer of message using Sender's PeerResolver. Input examples:

@telegram
telegram
t.me/telegram
https://t.me/telegram
tg:resolve?domain=telegram
tg://resolve?domain=telegram
+13115552368
+1 (311) 555-0123
+1 311 555-6162
Example
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	"github.com/gotd/td/telegram"
	"github.com/gotd/td/telegram/message"
	"github.com/gotd/td/telegram/message/peer"
	"github.com/gotd/td/tg"
)

func resolve(ctx context.Context) error {
	client, err := telegram.ClientFromEnvironment(telegram.Options{})
	if err != nil {
		return err
	}

	return client.Run(ctx, func(ctx context.Context) error {
		sender := message.NewSender(tg.NewClient(client))

		// Resolve and return input peer for @misato.
		_, err := sender.Resolve("misato").AsInputPeer(ctx)
		if err != nil {
			return err
		}

		// Resolve and join channel @seele.
		// If @seele is a user, not channel, error would be returned.
		_, err = sender.Resolve("seele", peer.OnlyChannel).Join(ctx)
		if err != nil {
			return err
		}

		return nil
	})
}

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	if err := resolve(ctx); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(2)
	}
}
Output:

func (s *Sender) ResolveDeeplink(link string, decorators ...peer.PromiseDecorator) *RequestBuilder

ResolveDeeplink uses given deeplink to create new message builder. Deeplink is a URL like https://t.me/telegram. It resolves peer of message using Sender's PeerResolver. Input examples:

t.me/telegram
https://t.me/telegram
tg:resolve?domain=telegram
tg://resolve?domain=telegram

func (*Sender) ResolveDomain added in v0.26.0

func (s *Sender) ResolveDomain(domain string, decorators ...peer.PromiseDecorator) *RequestBuilder

ResolveDomain uses given domain to create new message builder. It resolves peer of message using Sender's PeerResolver. Can has prefix with @ or not. Input examples:

@telegram
telegram

func (*Sender) ResolvePhone added in v0.30.0

func (s *Sender) ResolvePhone(phone string, decorators ...peer.PromiseDecorator) *RequestBuilder

ResolvePhone uses given phone to create new peer promise. It resolves peer of message using given Resolver. Input example:

+13115552368
+1 (311) 555-0123
+1 311 555-6162

NB: ResolvePhone just deletes any non-digit symbols from phone argument. For now, Telegram sends contact number as string like "13115552368".

func (*Sender) Self added in v0.26.0

func (s *Sender) Self() *RequestBuilder

Self creates a new message builder to send it to yourself. It means that message will be sent to your Saved Messages folder.

func (*Sender) To added in v0.31.0

To uses given peer to create new message builder.

func (*Sender) WithRand added in v0.26.0

func (s *Sender) WithRand(r io.Reader) *Sender

WithRand sets random ID source.

func (*Sender) WithResolver added in v0.26.0

func (s *Sender) WithResolver(resolver peer.Resolver) *Sender

WithResolver sets peer resolver to use.

func (*Sender) WithUploader added in v0.26.0

func (s *Sender) WithUploader(u Uploader) *Sender

WithUploader sets file uploader to use.

type StyledTextOption added in v0.26.0

type StyledTextOption = styling.StyledTextOption

StyledTextOption is an option for styling text.

type TypingActionBuilder added in v0.26.0

type TypingActionBuilder struct {
	// contains filtered or unexported fields
}

TypingActionBuilder is a helper to create and send typing actions.

See https://core.telegram.org/method/messages.setTyping.

See https://core.telegram.org/type/SendMessageAction.

func (*TypingActionBuilder) Cancel added in v0.26.0

func (b *TypingActionBuilder) Cancel(ctx context.Context) error

Cancel sends SendMessageCancelAction.

func (*TypingActionBuilder) ChooseContact added in v0.26.0

func (b *TypingActionBuilder) ChooseContact(ctx context.Context) error

ChooseContact sends SendMessageChooseContactAction.

func (*TypingActionBuilder) ChooseSticker added in v0.54.0

func (b *TypingActionBuilder) ChooseSticker(ctx context.Context) error

ChooseSticker sends SendMessageChooseStickerAction.

func (*TypingActionBuilder) Custom added in v0.54.0

Custom sends given action.

func (*TypingActionBuilder) EmojiInteraction added in v0.54.0

func (b *TypingActionBuilder) EmojiInteraction(ctx context.Context, emoticon string, msgID int, interaction tg.DataJSON) error

EmojiInteraction sends SendMessageEmojiInteraction.

func (*TypingActionBuilder) EmojiInteractionSeen added in v0.54.0

func (b *TypingActionBuilder) EmojiInteractionSeen(ctx context.Context, emoticon string) error

EmojiInteractionSeen sends SendMessageEmojiInteractionSeen.

func (*TypingActionBuilder) GamePlay added in v0.26.0

func (b *TypingActionBuilder) GamePlay(ctx context.Context) error

GamePlay sends SendMessageGamePlayAction.

func (*TypingActionBuilder) GeoLocation added in v0.26.0

func (b *TypingActionBuilder) GeoLocation(ctx context.Context) error

GeoLocation sends SendMessageGeoLocationAction.

func (*TypingActionBuilder) HistoryImport added in v0.26.0

func (b *TypingActionBuilder) HistoryImport(ctx context.Context, progress int) error

HistoryImport sends SendMessageHistoryImportAction.

func (*TypingActionBuilder) RecordAudio added in v0.26.0

func (b *TypingActionBuilder) RecordAudio(ctx context.Context) error

RecordAudio sends SendMessageRecordAudioAction.

func (*TypingActionBuilder) RecordRound added in v0.26.0

func (b *TypingActionBuilder) RecordRound(ctx context.Context) error

RecordRound sends SendMessageRecordRoundAction.

func (*TypingActionBuilder) RecordVideo added in v0.26.0

func (b *TypingActionBuilder) RecordVideo(ctx context.Context) error

RecordVideo sends SendMessageRecordVideoAction.

func (*TypingActionBuilder) SpeakingInGroupCall added in v0.26.0

func (b *TypingActionBuilder) SpeakingInGroupCall(ctx context.Context) error

SpeakingInGroupCall sends SpeakingInGroupCallAction.

func (*TypingActionBuilder) ThreadID added in v0.26.0

func (b *TypingActionBuilder) ThreadID(id int) *TypingActionBuilder

ThreadID sets thread ID to send.

func (*TypingActionBuilder) ThreadMsg added in v0.26.0

ThreadMsg sets message's ID as thread ID to send.

func (*TypingActionBuilder) Typing added in v0.26.0

func (b *TypingActionBuilder) Typing(ctx context.Context) error

Typing sends SendMessageTypingAction.

func (*TypingActionBuilder) UploadAudio added in v0.26.0

func (b *TypingActionBuilder) UploadAudio(ctx context.Context, progress int) error

UploadAudio sends SendMessageUploadAudioAction.

func (*TypingActionBuilder) UploadDocument added in v0.26.0

func (b *TypingActionBuilder) UploadDocument(ctx context.Context, progress int) error

UploadDocument sends SendMessageUploadDocumentAction.

func (*TypingActionBuilder) UploadPhoto added in v0.26.0

func (b *TypingActionBuilder) UploadPhoto(ctx context.Context, progress int) error

UploadPhoto sends SendMessageUploadPhotoAction.

func (*TypingActionBuilder) UploadRound added in v0.26.0

func (b *TypingActionBuilder) UploadRound(ctx context.Context, progress int) error

UploadRound sends SendMessageUploadRoundAction.

func (*TypingActionBuilder) UploadVideo added in v0.26.0

func (b *TypingActionBuilder) UploadVideo(ctx context.Context, progress int) error

UploadVideo sends SendMessageUploadVideoAction.

type UploadBuilder added in v0.26.0

type UploadBuilder struct {
	// contains filtered or unexported fields
}

UploadBuilder is an attachment uploading helper.

func (*UploadBuilder) AsInputFile added in v0.26.0

func (u *UploadBuilder) AsInputFile(ctx context.Context) (tg.InputFileClass, error)

AsInputFile uploads and returns uploaded file entity.

func (*UploadBuilder) Audio added in v0.26.0

func (u *UploadBuilder) Audio(ctx context.Context, caption ...StyledTextOption) (tg.UpdatesClass, error)

Audio uploads and sends audio file.

func (*UploadBuilder) File added in v0.26.0

func (u *UploadBuilder) File(ctx context.Context, caption ...StyledTextOption) (tg.UpdatesClass, error)

File uploads and sends plain file.

func (*UploadBuilder) GIF added in v0.26.0

func (u *UploadBuilder) GIF(ctx context.Context, caption ...StyledTextOption) (tg.UpdatesClass, error)

GIF uploads and sends gif file.

func (*UploadBuilder) Photo added in v0.26.0

func (u *UploadBuilder) Photo(ctx context.Context, caption ...StyledTextOption) (tg.UpdatesClass, error)

Photo uploads and sends photo.

func (*UploadBuilder) RoundVideo added in v0.26.0

func (u *UploadBuilder) RoundVideo(ctx context.Context, caption ...StyledTextOption) (tg.UpdatesClass, error)

RoundVideo uploads and sends round video.

func (*UploadBuilder) Sticker added in v0.26.0

func (u *UploadBuilder) Sticker(ctx context.Context, caption ...StyledTextOption) (tg.UpdatesClass, error)

Sticker uploads and sends sticker.

func (*UploadBuilder) Video added in v0.26.0

func (u *UploadBuilder) Video(ctx context.Context, caption ...StyledTextOption) (tg.UpdatesClass, error)

Video uploads and sends video.

func (*UploadBuilder) Voice added in v0.26.0

func (u *UploadBuilder) Voice(ctx context.Context) (tg.UpdatesClass, error)

Voice uploads and sends voice message.

type UploadOption added in v0.26.0

type UploadOption interface {
	// contains filtered or unexported methods
}

UploadOption is a UploadBuilder creation option.

func FromBytes added in v0.26.0

func FromBytes(name string, data []byte) UploadOption

FromBytes uploads file from given byte slice.

func FromFS added in v0.26.0

func FromFS(filesystem fs.FS, path string) UploadOption

FromFS uploads file from given path using given fs.FS.

func FromFile added in v0.26.0

func FromFile(f uploader.File) UploadOption

FromFile uploads given File. NB: FromFile does not close given file.

func FromPath added in v0.26.0

func FromPath(path string) UploadOption

FromPath uploads file from given path.

func FromReader added in v0.26.0

func FromReader(name string, r io.Reader) UploadOption

FromReader uploads file from given io.Reader. NB: totally stream should not exceed the limit for small files (10 MB as docs says, may be a bit bigger).

func FromSource added in v0.38.0

func FromSource(src source.Source, rawURL string) UploadOption

FromSource uploads file from given URL using given Source.

func FromURL added in v0.38.0

func FromURL(rawURL string) UploadOption

FromURL uploads file from given URL.

func Upload added in v0.38.0

func Upload(promise FilePromise) UploadOption

Upload creates new upload options using given promise.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	"github.com/go-faster/errors"

	"github.com/gotd/td/telegram"
	"github.com/gotd/td/telegram/message"
	"github.com/gotd/td/tg"
)

func filePromiseResult(ctx context.Context) error {
	client, err := telegram.ClientFromEnvironment(telegram.Options{})
	if err != nil {
		return err
	}

	return client.Run(ctx, func(ctx context.Context) error {
		sender := message.NewSender(tg.NewClient(client))
		r := sender.Resolve("@durov")

		var result tg.InputFileClass
		_, err := r.Upload(message.Upload(func(ctx context.Context, b message.Uploader) (tg.InputFileClass, error) {
			r, err := b.FromPath(ctx, "file.jpg")
			if err != nil {
				return nil, err
			}

			result = r
			return r, nil
		})).Photo(ctx)
		if err != nil {
			return errors.Wrap(err, "upload photo")
		}

		_, err = r.Media(ctx, message.UploadedDocument(result))
		if err != nil {
			return errors.Wrap(err, "upload document")
		}

		return nil
	})
}

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	if err := filePromiseResult(ctx); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(2)
	}
}
Output:

type UploadedDocumentBuilder added in v0.26.0

type UploadedDocumentBuilder struct {
	// contains filtered or unexported fields
}

UploadedDocumentBuilder is a UploadedDocument media option.

func File added in v0.26.0

File adds document attachment and forces it to be used as plain file, not media.

func GIF added in v0.26.0

GIF adds gif attachment.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	"github.com/gotd/td/telegram"
	"github.com/gotd/td/telegram/message"
	"github.com/gotd/td/tg"
)

func sendGif(ctx context.Context) error {
	client, err := telegram.ClientFromEnvironment(telegram.Options{})
	if err != nil {
		return err
	}

	return client.Run(ctx, func(ctx context.Context) error {
		sender := message.NewSender(tg.NewClient(client))

		// Uploads and sends gif to the @durovschat.
		if _, err := sender.Resolve("https://t.me/durovschat").
			Upload(message.FromPath("./rickroll.gif")).
			GIF(ctx); err != nil {
			return err
		}

		return nil
	})
}

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	if err := sendGif(ctx); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(2)
	}
}
Output:

func UploadedDocument added in v0.26.0

func UploadedDocument(file tg.InputFileClass, caption ...StyledTextOption) *UploadedDocumentBuilder

UploadedDocument adds document attachment.

func (*UploadedDocumentBuilder) Attributes added in v0.26.0

Attributes adds given attributes to the document. Attribute specify the type of the document (video, audio, voice, sticker, etc.).

func (*UploadedDocumentBuilder) Audio added in v0.26.0

Audio creates new AudioDocumentBuilder to create audio attachment.

func (*UploadedDocumentBuilder) Filename added in v0.26.0

Filename sets name of uploaded file.

func (*UploadedDocumentBuilder) ForceFile added in v0.26.0

ForceFile sets flag to force the media file to be uploaded as document.

func (*UploadedDocumentBuilder) GIF added in v0.36.0

GIF add attributes to create GIF attachment.

func (*UploadedDocumentBuilder) HasStickers added in v0.26.0

HasStickers sets flag that document attachment has stickers.

func (*UploadedDocumentBuilder) MIME added in v0.26.0

MIME sets MIME type of document.

func (*UploadedDocumentBuilder) NosoundVideo added in v0.26.0

NosoundVideo sets flag that the specified document is a video file with no audio tracks (a GIF animation (even as MPEG4), for example).

func (*UploadedDocumentBuilder) RoundVideo added in v0.26.0

RoundVideo creates new VideoDocumentBuilder to create round video attachment.

func (*UploadedDocumentBuilder) Stickers added in v0.26.0

Stickers adds attached mask stickers.

func (*UploadedDocumentBuilder) TTL added in v0.26.0

TTL sets time to live of self-destructing document.

func (*UploadedDocumentBuilder) TTLSeconds added in v0.26.0

TTLSeconds sets time to live in seconds of self-destructing document.

func (*UploadedDocumentBuilder) Thumb added in v0.26.0

Thumb sets thumbnail of the document, uploaded as for the file.

func (*UploadedDocumentBuilder) UploadedSticker added in v0.26.0

func (u *UploadedDocumentBuilder) UploadedSticker() *UploadedStickerBuilder

UploadedSticker creates new UploadedStickerBuilder to create sticker attachment.

func (*UploadedDocumentBuilder) Video added in v0.26.0

Video creates new VideoDocumentBuilder to create video attachment.

func (*UploadedDocumentBuilder) Voice added in v0.26.0

Voice creates new AudioDocumentBuilder to create voice attachment.

type UploadedPhotoBuilder added in v0.26.0

type UploadedPhotoBuilder struct {
	// contains filtered or unexported fields
}

UploadedPhotoBuilder is a UploadedPhoto media option.

func UploadedPhoto added in v0.26.0

func UploadedPhoto(file tg.InputFileClass, caption ...StyledTextOption) *UploadedPhotoBuilder

UploadedPhoto adds photo attachment.

func (*UploadedPhotoBuilder) Stickers added in v0.26.0

func (u *UploadedPhotoBuilder) Stickers(stickers ...FileLocation) *UploadedPhotoBuilder

Stickers adds attached mask stickers.

func (*UploadedPhotoBuilder) TTL added in v0.26.0

TTL sets time to live of self-destructing photo.

func (*UploadedPhotoBuilder) TTLSeconds added in v0.26.0

func (u *UploadedPhotoBuilder) TTLSeconds(ttl int) *UploadedPhotoBuilder

TTLSeconds sets time to live in seconds of self-destructing photo.

type UploadedStickerBuilder added in v0.26.0

type UploadedStickerBuilder struct {
	// contains filtered or unexported fields
}

UploadedStickerBuilder is a uploaded sticker media option.

func UploadedSticker added in v0.26.0

func UploadedSticker(file tg.InputFileClass, caption ...StyledTextOption) *UploadedStickerBuilder

UploadedSticker adds uploaded sticker attachment.

func (*UploadedStickerBuilder) Alt added in v0.26.0

Alt sets alternative emoji representation of sticker.

func (*UploadedStickerBuilder) Mask added in v0.26.0

Mask sets flag that is a mask sticker.

func (*UploadedStickerBuilder) MaskCoords added in v0.26.0

func (u *UploadedStickerBuilder) MaskCoords(maskCoords tg.MaskCoords) *UploadedStickerBuilder

MaskCoords sets mask coordinates (if this is a mask sticker, attached to a photo).

func (*UploadedStickerBuilder) StickerSet added in v0.26.0

StickerSet sets associated sticker set.

type Uploader added in v0.26.0

type Uploader interface {
	FromFile(ctx context.Context, f uploader.File) (tg.InputFileClass, error)
	FromPath(ctx context.Context, path string) (tg.InputFileClass, error)
	FromFS(ctx context.Context, filesystem fs.FS, path string) (tg.InputFileClass, error)
	FromReader(ctx context.Context, name string, f io.Reader) (tg.InputFileClass, error)
	FromBytes(ctx context.Context, name string, b []byte) (tg.InputFileClass, error)
	FromURL(ctx context.Context, rawURL string) (tg.InputFileClass, error)
	FromSource(ctx context.Context, src source.Source, rawURL string) (tg.InputFileClass, error)
}

Uploader is an abstraction for Telegram file uploader.

type VideoDocumentBuilder added in v0.26.0

type VideoDocumentBuilder struct {
	// contains filtered or unexported fields
}

VideoDocumentBuilder is a Video media option.

func RoundVideo added in v0.26.0

func RoundVideo(file tg.InputFileClass, caption ...StyledTextOption) *VideoDocumentBuilder

RoundVideo adds round video attachment.

func Video added in v0.26.0

func Video(file tg.InputFileClass, caption ...StyledTextOption) *VideoDocumentBuilder

Video adds video attachment.

func (*VideoDocumentBuilder) Duration added in v0.26.0

func (u *VideoDocumentBuilder) Duration(duration time.Duration) *VideoDocumentBuilder

Duration sets duration of video file.

func (*VideoDocumentBuilder) DurationSeconds added in v0.26.0

func (u *VideoDocumentBuilder) DurationSeconds(duration int) *VideoDocumentBuilder

DurationSeconds sets duration in seconds.

func (*VideoDocumentBuilder) Resolution added in v0.26.0

func (u *VideoDocumentBuilder) Resolution(w, h int) *VideoDocumentBuilder

Resolution sets resolution of this video.

func (*VideoDocumentBuilder) Round added in v0.26.0

Round sets flag to mark this video as round.

func (*VideoDocumentBuilder) SupportsStreaming added in v0.26.0

func (u *VideoDocumentBuilder) SupportsStreaming() *VideoDocumentBuilder

SupportsStreaming sets flag to mark this video as which supports streaming.

Directories

Path Synopsis
Package entity contains message formatting and styling helpers.
Package entity contains message formatting and styling helpers.
Package html contains HTML styling options.
Package html contains HTML styling options.
Package inline contains inline query results builder.
Package inline contains inline query results builder.
internal
mkrun
Package mkrun contains some helpers for generation scripts.
Package mkrun contains some helpers for generation scripts.
Package markup contain bots inline markup builder.
Package markup contain bots inline markup builder.
Package peer conatains some peer resolving and extracting helpers.
Package peer conatains some peer resolving and extracting helpers.
Package styling contains styling options for Telegram messages.
Package styling contains styling options for Telegram messages.
Package unpack contains some updates result unpacking helpers.
Package unpack contains some updates result unpacking helpers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL