|
1 | 1 | package main |
2 | 2 |
|
3 | | -// Youtube DL Bot Example; |
4 | | -// https://gist.github.com/AmarnathCJD/bfceefe9efd1a079ab151da54ef3bba2 |
5 | | - |
6 | 3 | import ( |
| 4 | + "bytes" |
7 | 5 | "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "strings" |
8 | 9 |
|
9 | 10 | "github.com/amarnathcjd/gogram/telegram" |
10 | 11 | ) |
11 | 12 |
|
12 | 13 | const ( |
13 | 14 | appID = 6 |
14 | | - apiKey = "" |
15 | | - botToken = "" |
| 15 | + appHash = "YOUR_APP_HASH" |
| 16 | + botToken = "YOUR_BOT_TOKEN" |
| 17 | + chatID = "YOUR_CHAT_ID" |
16 | 18 | ) |
17 | 19 |
|
18 | 20 | func main() { |
19 | | - // create a new client object |
20 | 21 | client, _ := telegram.NewClient(telegram.ClientConfig{ |
21 | 22 | AppID: appID, |
22 | | - AppHash: apiKey, |
| 23 | + AppHash: appHash, |
23 | 24 | }) |
24 | | - |
25 | 25 | client.LoginBot(botToken) |
26 | 26 |
|
27 | | - chat, _ := client.ResolvePeer("chatId") |
28 | | - m, _ := client.SendMessage(chat, "Starting File Upload...") |
29 | | - |
30 | | - client.SendMedia(chat, "<file-name>", &telegram.MediaOptions{ |
31 | | - Upload: &telegram.UploadOptions{ |
32 | | - ProgressCallback: func(pi *telegram.ProgressInfo) { |
33 | | - m.Edit(fmt.Sprintf("Uploading... %.2f%% complete (%.2f MB/s), ETA: %.2f seconds", |
34 | | - pi.Percentage, |
35 | | - pi.CurrentSpeed/1024/1024, |
36 | | - pi.ETA, |
37 | | - )) |
38 | | - }, |
39 | | - ProgressInterval: 5, // update every 5 seconds |
40 | | - }, |
41 | | - }) |
42 | | - |
43 | | - // to use custom progress manager |
44 | | - // pm := telegram.NewProgressManager(5) |
45 | | - // pm.EditFunc(MediaDownloadProgress("<file-name>", m, pm)) |
46 | | - // client.SendMedia(chat, "<file-name>", &telegram.MediaOptions{ |
47 | | - // ProgressManager: pm, |
48 | | - // }) |
49 | | - |
50 | | - // same goes for download |
51 | | - // &DownloadOptions{ProgressManager: NewProgressManager(5).SetMessage(m)} |
52 | | - // or |
53 | | - // &DownloadOptions{ProgressCallback: func(pi *ProgressInfo) { |
54 | | - // m.Edit(fmt.Sprintf("Downloading... %.2f%% complete (%.2f MB/s), ETA: %.2f seconds", |
55 | | - // pi.Percentage, |
56 | | - // pi.CurrentSpeed/1024/1024, |
57 | | - // pi.ETA, |
58 | | - // )) |
59 | | - // }} |
60 | | - // &SendOptions{ProgressManager: ...} |
61 | | - // &MediaOptions{ProgressManager: ...} |
| 27 | + chat, _ := client.ResolvePeer(chatID) |
| 28 | + |
| 29 | + // Upload from a file path (string) |
| 30 | + // Parallel upload — file is read at random offsets by multiple workers. |
| 31 | + client.SendMedia(chat, "video.mp4", &telegram.MediaOptions{ |
| 32 | + Upload: &telegram.UploadOptions{ |
| 33 | + ProgressCallback: logProgress, |
| 34 | + ProgressInterval: 3, |
| 35 | + }, |
| 36 | + }) |
| 37 | + |
| 38 | + // Upload from *os.File |
| 39 | + // Also parallel — *os.File satisfies io.ReaderAt. |
| 40 | + f, _ := os.Open("photo.jpg") |
| 41 | + defer f.Close() |
| 42 | + client.SendMedia(chat, f, &telegram.MediaOptions{ |
| 43 | + Upload: &telegram.UploadOptions{ |
| 44 | + ProgressCallback: logProgress, |
| 45 | + }, |
| 46 | + }) |
| 47 | + |
| 48 | + // Upload from []byte |
| 49 | + // Parallel — bytes.NewReader wraps the slice, supports ReaderAt. |
| 50 | + data, _ := os.ReadFile("document.pdf") |
| 51 | + client.SendMedia(chat, data, &telegram.MediaOptions{ |
| 52 | + Upload: &telegram.UploadOptions{ |
| 53 | + FileName: "document.pdf", // required: no filename is inferred from []byte |
| 54 | + }, |
| 55 | + }) |
| 56 | + |
| 57 | + // Upload from *bytes.Reader (parallel) |
| 58 | + // bytes.Reader implements io.ReaderAt so uploads are parallelised. |
| 59 | + br := bytes.NewReader(data) |
| 60 | + client.SendMedia(chat, br, &telegram.MediaOptions{ |
| 61 | + Upload: &telegram.UploadOptions{ |
| 62 | + FileName: "document.pdf", |
| 63 | + ProgressCallback: logProgress, |
| 64 | + }, |
| 65 | + }) |
| 66 | + |
| 67 | + // Upload from io.ReadSeeker (sequential, size known) |
| 68 | + // ReadSeeker is NOT safe for concurrent reads, so gogram uses a single worker. |
| 69 | + // The seek is used upfront to measure the file size for accurate progress. |
| 70 | + var rs io.ReadSeeker = strings.NewReader("hello world") |
| 71 | + client.SendMedia(chat, rs, &telegram.MediaOptions{ |
| 72 | + Upload: &telegram.UploadOptions{ |
| 73 | + FileName: "hello.txt", |
| 74 | + }, |
| 75 | + }) |
| 76 | + |
| 77 | + // Upload from io.Reader (sequential, size unknown) |
| 78 | + // No parallelism, no progress percentage (size is unknown). |
| 79 | + // Use for streaming sources like network responses. |
| 80 | + var r io.Reader = strings.NewReader("streamed content") |
| 81 | + client.SendMedia(chat, r, &telegram.MediaOptions{ |
| 82 | + Upload: &telegram.UploadOptions{ |
| 83 | + FileName: "stream.txt", |
| 84 | + }, |
| 85 | + }) |
| 86 | + |
| 87 | + // Upload from ReaderAtSource (parallel, custom io.ReaderAt) |
| 88 | + // Use this when you have a custom source that supports random-access reads, |
| 89 | + // e.g. a memory-mapped file, a cloud storage reader, etc. |
| 90 | + // You must supply the size explicitly since there is no way to infer it. |
| 91 | + rawData := []byte("binary payload") |
| 92 | + src := &telegram.ReaderAtSource{ |
| 93 | + Reader: bytes.NewReader(rawData), |
| 94 | + Size: int64(len(rawData)), |
| 95 | + Name: "payload.bin", |
| 96 | + } |
| 97 | + client.SendMedia(chat, src, &telegram.MediaOptions{ |
| 98 | + Upload: &telegram.UploadOptions{ |
| 99 | + ProgressCallback: logProgress, |
| 100 | + }, |
| 101 | + }) |
| 102 | + |
| 103 | + // Upload with ProgressManager (edits a live message) |
| 104 | + status, _ := client.SendMessage(chat, "Uploading...") |
| 105 | + pm := telegram.NewProgressManager(5). |
| 106 | + WithCallback(func(pi *telegram.ProgressInfo) { |
| 107 | + status.Edit(fmt.Sprintf("Uploading... %.1f%% (%.2f MB/s) ETA %.0fs", |
| 108 | + pi.Percentage, pi.CurrentSpeed/1024/1024, pi.ETA)) |
| 109 | + }) |
| 110 | + client.SendMedia(chat, "bigfile.zip", &telegram.MediaOptions{ |
| 111 | + Upload: &telegram.UploadOptions{ |
| 112 | + ProgressManager: pm, |
| 113 | + }, |
| 114 | + }) |
| 115 | + status.Edit("Upload complete!") |
| 116 | +} |
| 117 | + |
| 118 | +func logProgress(p *telegram.ProgressInfo) { |
| 119 | + fmt.Printf("\r%s %.1f%% %.2f MB/s ETA %.0fs", |
| 120 | + p.FileName, p.Percentage, p.CurrentSpeed/1024/1024, p.ETA) |
| 121 | + if p.Percentage >= 100 { |
| 122 | + fmt.Println() |
| 123 | + } |
62 | 124 | } |
0 commit comments