-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (66 loc) · 2.38 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
package main
import (
"fmt"
"latex-to-image-bot/latexToImage"
"log"
"os"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func main() {
bot, err := tgbotapi.NewBotAPI(os.Getenv("GO_TELEGRAM_APITOKEN"))
if err != nil {
log.Panic(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message == nil { // ignore any non-Message updates
continue
}
if update.Message.IsCommand() {
cmdReplayMsg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
// respond to the commands
switch update.Message.Command() {
case "start":
cmdReplayMsg.Text = "Hi there! Thanks for starting LaTeX to image bot. With my help, you can quickly and easily convert your LaTeX code into images that you can share with others. Just send me your LaTeX code and I'll take care of the rest."
case "help":
cmdReplayMsg.Text = `Just send me your LaTeX code and I'll convert it into an image for you.
I you want a code snippet to paste into your LaTeX document, use the following equation:
$$\text{Entropy } H = \sum\limits_{i=1}^{n} -p(m_{i})\log_{2}(p(m_{i}))$$`
case "contact":
cmdReplayMsg.Text = "To see the source code of this bot, please visit https://github.com/taham8875/latex-to-image-bot"
default:
cmdReplayMsg.Text = "I don't know that command, supported commands are: /start, /help, /contact"
}
if _, err := bot.Send(cmdReplayMsg); err != nil {
log.Panic(err)
}
continue
}
// respond to the user with a message
waitMsg := tgbotapi.NewMessage(update.Message.Chat.ID, "converting your latex code to image...")
waitMsg.ReplyToMessageID = update.Message.MessageID
if _, err := bot.Send(waitMsg); err != nil {
log.Panic(err)
}
// convert the user input to pdf
outputFilePath, err := latexToImage.ConvertLatexToImage(update.Message.Text)
if err != nil {
fmt.Printf("Error: %v", err)
return
}
// when the user send a message, reply with photo
msg := tgbotapi.NewPhoto(update.Message.Chat.ID, tgbotapi.FilePath(outputFilePath))
if _, err := bot.Send(msg); err != nil {
log.Panic(err)
}
// delete the image file
if err = os.Remove(outputFilePath); err != nil {
fmt.Printf("Error deleting the image file: %v", err)
return
}
}
}