Documentation
¶
Overview ¶
Package takeout provides a wrapper for Telegram takeout sessions.
Takeout sessions allow exporting user data from Telegram. See https://core.telegram.org/api/takeout for more information.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AsInitDelay ¶
AsInitDelay returns delay of TAKEOUT_INIT_DELAY_X error.
func Run ¶
func Run(ctx context.Context, invoker tg.Invoker, cfg Config, f func(ctx context.Context, client *Client) error) error
Run initializes a takeout session and calls the provided function.
The session is automatically finished when the function returns. If the function returns nil, the session is marked as successful. Otherwise, the session is marked as failed.
The session is finished even if ctx is canceled (with a short timeout).
Example ¶
package main
import (
"context"
"fmt"
"github.com/gotd/td/telegram/takeout"
"github.com/gotd/td/tg"
)
func main() {
// This example demonstrates how to use the takeout API wrapper.
// In a real application, you would use a proper tg.Invoker.
ctx := context.Background()
var invoker tg.Invoker // obtained from telegram.Client
// Configure what data to export
cfg := takeout.Config{
Contacts: true,
MessageUsers: true,
MessageChats: true,
MessageMegagroups: true,
MessageChannels: true,
Files: true,
FileMaxSize: 512 * 1024 * 1024, // 512 MB
}
err := takeout.Run(ctx, invoker, cfg, func(ctx context.Context, client *takeout.Client) error {
// All API calls made with client are wrapped with takeout session.
// Use tg.NewClient(client) to get a full API client.
api := tg.NewClient(client)
// For example, get dialogs:
dialogs, err := api.MessagesGetDialogs(ctx, &tg.MessagesGetDialogsRequest{
Limit: 100,
})
if err != nil {
return err
}
fmt.Printf("Got dialogs: %T\n", dialogs)
return nil
})
if err != nil {
// Handle error
_ = err
}
}
Output:
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps tg.Invoker to use a takeout session.
func Init ¶
Init initializes new takeout session.
See Config.OnInitDelay for TAKEOUT_INIT_DELAY handling.
func NewClient ¶
NewClient creates Client for existing takeout session with given ID, e.g. to resume a takeout session saved before restart.
Telegram keeps the session until it is finished, so long-running exports can persist Client.ID and resume with it instead of starting a new session (which may require confirmation by the user again).
func (*Client) Finish ¶
Finish finishes takeout session.
The request is sent within the session (invokeWithTakeout), so the server knows which session to finish.
type Config ¶
type Config struct {
// Contacts enables exporting contacts.
Contacts bool
// MessageUsers enables exporting messages from private chats.
MessageUsers bool
// MessageChats enables exporting messages from basic groups.
MessageChats bool
// MessageMegagroups enables exporting messages from supergroups.
MessageMegagroups bool
// MessageChannels enables exporting messages from channels.
MessageChannels bool
// Files enables exporting files.
Files bool
// FileMaxSize sets maximum file size to export.
// Only used if Files is true.
FileMaxSize int64
// OnInitDelay is called when init fails with TAKEOUT_INIT_DELAY_X: the
// user must confirm the export on another device, or wait d (X seconds)
// for the session to become available (optional).
//
// It is called once, on the first such error. Return nil to keep polling
// init every InitPollInterval (or d, if it is shorter) until it succeeds,
// or an error to abort. If not set, the error is returned.
//
// FLOOD_WAIT errors while polling are waited out.
OnInitDelay func(ctx context.Context, d time.Duration) error
// InitPollInterval is the interval of init attempts while waiting for
// the confirmation. Defaults to 30 seconds.
InitPollInterval time.Duration
// Clock is the time source. Defaults to clock.System.
Clock clock.Clock
}
Config configures takeout session.