-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
134 lines (109 loc) · 3.58 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/joho/godotenv"
tele "gopkg.in/telebot.v3"
"k8s.io/klog/v2"
)
func deleteFile(fileName string) {
if err := os.Remove(fileName); err != nil {
klog.Fatal("Error when deleting converted file", err)
}
}
// exists returns whether the given file or directory exists
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func main() {
klog.InitFlags(nil)
if ex, err := exists(".env"); ex && err == nil {
klog.Info("LOADING ENV")
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
}
TOKEN := os.Getenv("TOKEN")
WEBHOOK_URL := os.Getenv("WEBHOOK_URL")
PORT := os.Getenv("PORT")
klog.Infof("TOKEN %s", TOKEN)
klog.Infof("WEBHOOK_URL %s", WEBHOOK_URL)
klog.Infof("PORT %s", PORT)
var config tele.Settings
if WEBHOOK_URL == "" {
// delete bot webhook
_, err := http.Get(fmt.Sprintf("https://api.telegram.org/bot%s/deleteWebhook", TOKEN))
if err != nil {
log.Fatal(err)
}
config = tele.Settings{
Token: TOKEN,
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
}
} else {
config = tele.Settings{
Token: TOKEN,
Poller: &tele.Webhook{
Endpoint: &tele.WebhookEndpoint{PublicURL: WEBHOOK_URL},
AllowedUpdates: []string{"callback_query", "message"},
Listen: fmt.Sprintf(":%s", PORT),
},
}
}
b, err := tele.NewBot(config)
if err != nil {
klog.Fatal(err)
return
}
klog.Info("Bot started")
now := time.Now().Format(time.RFC3339)
nowFmt := now[:len(now)-6]
b.Handle(tele.OnVoice, func(c tele.Context) error {
klog.Infof("Handling voice message from %s", c.Chat().Username)
if file, err := b.FileByID(c.Message().Voice.File.FileID); err != nil {
klog.Fatal("Error when getting file by id", err)
} else {
if len(file.FilePath) > 0 {
fileDownloadUrl := fmt.Sprintf("https://api.telegram.org/file/bot%s/%s", TOKEN, file.FilePath)
convertedFileName := fmt.Sprintf("tg_%s.mp3", nowFmt)
if err := DownloadFile(convertedFileName, fileDownloadUrl); err != nil {
klog.Fatal("Error when downloading file", err)
}
defer deleteFile(convertedFileName)
retFile := tele.Audio{File: tele.FromDisk(convertedFileName), Title: convertedFileName, FileName: convertedFileName}
klog.Infof("Responding to %s", c.Chat().Username)
return c.Send(&retFile)
} else {
klog.Fatalf("File Path not defined for %s", file.FileID)
}
}
return c.Send("Sorry! There has been an issue with this voice message 😔")
})
b.Handle(tele.OnText, func(ctx tele.Context) error {
return ctx.Send("Hi! I'm here, send me a voice message and I'll hosw you magic!")
})
b.Handle("/help", func(ctx tele.Context) error {
klog.Info("RECEIVED HELP COMMAND")
return ctx.Send("Hello👋\nI'm bot that can turn any voice message into file to be shared outside of telegram.\n\nIf you like me, please make a little donation to my creator, thank you!")
})
b.Handle("/privacy", func(ctx tele.Context) error {
klog.Info("RECEIVED PRIVACY COMMAND")
return ctx.Send("Hello👋\nI take privacy very seriously:\n- I do not store any of the messages and files we exchange\n- I do not store any information about you.\nYou simply write, I respond, that's it!")
})
b.Handle("/donate", func(ctx tele.Context) error {
klog.Info("RECEIVED DONATE COMMAND")
return ctx.Send("Hello👋\nIf you would like to thank my creator for my services, please donate using paypal to this email [email protected]!")
})
b.Start()
}