|
| 1 | +// Binary bot-upload implements upload example for bot. |
| 2 | +package main |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "errors" |
| 7 | + "flag" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "go.uber.org/zap" |
| 11 | + |
| 12 | + "github.com/gotd/td/examples" |
| 13 | + "github.com/gotd/td/telegram" |
| 14 | + "github.com/gotd/td/telegram/message" |
| 15 | + "github.com/gotd/td/telegram/message/html" |
| 16 | + "github.com/gotd/td/telegram/uploader" |
| 17 | + "github.com/gotd/td/tg" |
| 18 | +) |
| 19 | + |
| 20 | +func main() { |
| 21 | + // Environment variables: |
| 22 | + // BOT_TOKEN: token from BotFather |
| 23 | + // APP_ID: app_id of Telegram app. |
| 24 | + // APP_HASH: app_hash of Telegram app. |
| 25 | + // SESSION_FILE: path to session file |
| 26 | + // SESSION_DIR: path to session directory, if SESSION_FILE is not set |
| 27 | + filePath := flag.String("file", "", "file to upload") |
| 28 | + targetDomain := flag.String("target", "", "target to upload, e.g. channel name") |
| 29 | + flag.Parse() |
| 30 | + |
| 31 | + examples.Run(func(ctx context.Context, log *zap.Logger) error { |
| 32 | + if *filePath == "" || *targetDomain == "" { |
| 33 | + return errors.New("no --file or --target provided") |
| 34 | + } |
| 35 | + |
| 36 | + // The performUpload will be called after client initialization. |
| 37 | + performUpload := func(ctx context.Context, client *telegram.Client) error { |
| 38 | + // Raw MTProto API client, allows making raw RPC calls. |
| 39 | + api := tg.NewClient(client) |
| 40 | + |
| 41 | + // Helper for uploading. Automatically uses big file upload when needed. |
| 42 | + u := uploader.NewUploader(api) |
| 43 | + |
| 44 | + // Helper for sending messages. |
| 45 | + sender := message.NewSender(api).WithUploader(u) |
| 46 | + |
| 47 | + // Uploading directly from path. Note that you can do it from |
| 48 | + // io.Reader or buffer, see From* methods of uploader. |
| 49 | + log.Info("Uploading file") |
| 50 | + upload, err := u.FromPath(ctx, *filePath) |
| 51 | + if err != nil { |
| 52 | + return fmt.Errorf("failed to upload: %w", err) |
| 53 | + } |
| 54 | + |
| 55 | + // Now we have uploaded file handle, sending it as styled message. |
| 56 | + // First, preparing message. |
| 57 | + document := message.UploadedDocument(upload, |
| 58 | + html.String(nil, `Upload: <b>From bot</b>`), |
| 59 | + ) |
| 60 | + |
| 61 | + // You can set MIME type, send file as video or audio by using |
| 62 | + // document builder: |
| 63 | + document. |
| 64 | + MIME("audio/mp3"). |
| 65 | + Filename("some-audio.mp3"). |
| 66 | + Audio() |
| 67 | + |
| 68 | + // Resolving target. Can be telephone number or @nickname of user, |
| 69 | + // group or channel. |
| 70 | + target := sender.Resolve(*targetDomain) |
| 71 | + |
| 72 | + // Sending message with media. |
| 73 | + log.Info("Sending file") |
| 74 | + if _, err := target.Media(ctx, document); err != nil { |
| 75 | + return fmt.Errorf("send: %w", err) |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | + } |
| 80 | + return telegram.BotFromEnvironment(ctx, telegram.Options{Logger: log}, nil, performUpload) |
| 81 | + }) |
| 82 | +} |
0 commit comments