-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendFile.go
More file actions
63 lines (55 loc) · 1.21 KB
/
Copy pathsendFile.go
File metadata and controls
63 lines (55 loc) · 1.21 KB
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
package telegram
import (
"context"
"fmt"
"os"
"path/filepath"
tgBot "github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
)
type FileType int
const (
TypeDocument FileType = iota
TypeVideo
TypeAudio
)
func (b *Bot) SendFile(ctx context.Context, chatID int64, fileType FileType, path string, caption ...string) (*models.Message, error) {
if path == "" {
return nil, fmt.Errorf("path is required")
}
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("os.Open: %w", err)
}
defer file.Close()
upload := &models.InputFileUpload{
Filename: filepath.Base(path),
Data: file,
}
var captionStr string
if len(caption) > 0 {
captionStr = caption[0]
}
switch fileType {
case TypeDocument:
return b.api.SendDocument(ctx, &tgBot.SendDocumentParams{
ChatID: chatID,
Document: upload,
Caption: captionStr,
})
case TypeVideo:
return b.api.SendVideo(ctx, &tgBot.SendVideoParams{
ChatID: chatID,
Video: upload,
Caption: captionStr,
})
case TypeAudio:
return b.api.SendAudio(ctx, &tgBot.SendAudioParams{
ChatID: chatID,
Audio: upload,
Caption: captionStr,
})
default:
return nil, fmt.Errorf("unknown file type: %d", fileType)
}
}