-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
146 lines (123 loc) · 3.56 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"bytes"
"io"
"context"
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func main() {
// Get environment variables
botToken := os.Getenv("TELEGRAM_BOT_TOKEN")
owner, err := strconv.ParseInt(os.Getenv("OWNER_ID"), 10, 64)
if err != nil {
log.Fatal("Invalid OWNER_ID")
}
customDomain := os.Getenv("CF_CUSTOM_DOMAIN")
// Cloudflare R2 credentials
accountId := os.Getenv("CF_ACCOUNT_ID")
accessKeyId := os.Getenv("CF_ACCESS_KEY_ID")
accessKeySecret := os.Getenv("CF_ACCESS_KEY_SECRET")
bucketName := os.Getenv("CF_BUCKET_NAME")
// Initialize Telegram bot
bot, err := tgbotapi.NewBotAPI(botToken)
if err != nil {
log.Fatal(err)
}
// Initialize S3 client for Cloudflare R2
r2Resolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
URL: fmt.Sprintf("https://%s.r2.cloudflarestorage.com", accountId),
}, nil
})
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithEndpointResolverWithOptions(r2Resolver),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKeyId, accessKeySecret, "")),
config.WithRegion("auto"),
)
if err != nil {
log.Fatal(err)
}
s3Client := s3.NewFromConfig(cfg)
// Set up updates channel
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
log.Printf("Bot started")
// Handle updates
for update := range updates {
if update.Message == nil {
continue
}
// Check if message is from owner
if update.Message.From.ID != owner {
continue
}
// Handle photo messages
if update.Message.Photo != nil || update.Message.Document != nil {
var fileID string
var fileName string
if update.Message.Photo != nil {
// Get the largest photo size
photos := update.Message.Photo
photo := photos[len(photos)-1]
fileID = photo.FileID
fileName = fmt.Sprintf("%d.jpg", time.Now().Unix())
} else if update.Message.Document != nil {
fileID = update.Message.Document.FileID
fileName = update.Message.Document.FileName
}
// Get file URL from Telegram
file, err := bot.GetFile(tgbotapi.FileConfig{FileID: fileID})
if err != nil {
log.Printf("Error getting file: %v", err)
continue
}
// Download file
resp, err := http.Get(file.Link(botToken))
if err != nil {
log.Printf("Error downloading file: %v", err)
continue
}
// Read the entire response body into a buffer
fileData, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Printf("Error reading file: %v", err)
continue
}
// Detect content type
contentType := http.DetectContentType(fileData)
// Upload to R2 using the buffer
_, err = s3Client.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: &bucketName,
Key: &fileName,
Body: bytes.NewReader(fileData),
ContentType: &contentType,
})
if err != nil {
log.Printf("Error uploading to R2: %v", err)
continue
}
// Generate public URL
var publicURL string
if customDomain != "" {
publicURL = fmt.Sprintf("https://%s/%s", customDomain, fileName)
} else {
publicURL = fmt.Sprintf("https://%s.r2.cloudflarestorage.com/%s", accountId, fileName)
}
// Send response
msg := tgbotapi.NewMessage(update.Message.Chat.ID, publicURL)
bot.Send(msg)
}
}
}