-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
107 lines (102 loc) · 3.05 KB
/
upload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/StarkBotsIndustries/telegraph/v2"
)
func upload(b *gotgbot.Bot, ctx *ext.Context) error {
str, s := uploadToTelegraph(b, ctx.EffectiveMessage)
if str == "" {
return nil
}
b.SendChatAction(ctx.Message.From.Id, "typing")
if s {
_, err := ctx.EffectiveMessage.Reply(b, str, &gotgbot.SendMessageOpts{ParseMode: "html"})
if err != nil {
return fmt.Errorf("failed to send upload str message: %w", err)
}
return nil
}
userID := ctx.EffectiveMessage.From.Id
currentStates := states[userID]
states[userID] = append(currentStates, Img{Link: str, Caption: ctx.EffectiveMessage.Caption})
_, err := ctx.EffectiveMessage.Reply(
b,
fmt.Sprintf("File is ready to Upload. Where do you want me to upload? \n\nIf you don't understand this, just tap on the first option. \n\nTotal Files : %v", len(states[userID])),
&gotgbot.SendMessageOpts{
ReplyMarkup: gotgbot.InlineKeyboardMarkup{
InlineKeyboard: uploadButtons,
},
},
)
if err != nil {
return fmt.Errorf("failed to send upload str message: %w", err)
}
return nil
}
func downloadFile(fileId string, b *gotgbot.Bot) ([]byte, error) {
bs := make([]byte, 0)
file, err := b.GetFile(fileId)
if err != nil {
return bs, fmt.Errorf("failed to getFile using FileID: %w", err)
}
r, err := http.Get("https://api.telegram.org/file/bot" + b.Token + "/" + file.FilePath)
if err != nil {
return bs, fmt.Errorf("failed to get file from servers: %w", err)
}
bs, err = ioutil.ReadAll(r.Body)
if err != nil {
return bs, fmt.Errorf("failed to read response while getting file bytes: %w", err)
}
defer r.Body.Close()
return bs, nil
}
// Returns link to telegraph uploaded media or error as string.
// String because, users should know the error. Weird logic anyway
func uploadToTelegraph(b *gotgbot.Bot, m *gotgbot.Message) (string, bool) {
var f string
mt := "photo"
if m.Animation != nil {
f = m.Animation.FileId
mt = "video"
} else if m.Audio != nil {
return "", false
} else if m.Document != nil {
if m.Document.FileName[len(m.Document.FileName)-4:] == "html" {
return "", false
}
f = m.Document.FileId
} else if m.Video != nil {
f = m.Video.FileId
mt = "video"
} else if m.Sticker != nil {
return "", false
} else if m.Voice != nil {
return "", false
} else if m.Photo != nil {
// Usually 3 file ids are provided. (Maybe always)
// Quality increases in ascending order.
// Last one is usually the best one. (Maybe always)
f = m.Photo[len(m.Photo)-1].FileId
} else if m.Text != "" {
return "", false
} else if m.PinnedMessage != nil {
return "", false
} else {
return "Please send a valid media", true
}
bs, err := downloadFile(f, b)
if err != nil {
return "An error occurred while downloading. \n\n" + err.Error(), true
}
res, err := telegraph.Upload(bytes.NewReader(bs), mt)
if err != nil {
return "An error occurred while uploading. \n\n" + err.Error(), true
} else {
return "https://telegra.ph/" + res, false
}
}