-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
204 lines (165 loc) · 5.24 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"os/signal"
"strings"
"time"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
"github.com/mkevac/markocaloriesbot/stats"
)
var (
adminUsername string
mh *MediaHandler
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
adminUsername = os.Getenv("ADMIN_USERNAME")
log.Printf("Admin username: %s", adminUsername)
mh = NewMediaHandler()
opts := []bot.Option{
bot.WithDefaultHandler(handler),
bot.WithCheckInitTimeout(time.Minute),
//bot.WithServerURL("http://telegram-bot-api:8081"),
}
var b *bot.Bot
var err error
for range time.Tick(time.Second * 5) {
b, err = bot.New(os.Getenv("TELEGRAM_BOT_API_TOKEN"), opts...)
if err != nil {
log.Printf("Error creating bot: %s", err)
time.Sleep(time.Second * 5)
} else {
break
}
}
b.RegisterHandler(bot.HandlerTypeMessageText, "/stats", bot.MatchTypeExact, statsHandler)
go answerMachine(ctx, b)
b.Start(ctx)
}
func FormatChatGPTResponse(response *OpenAIResponse) string {
// make a pretty response from OpenAIResponse struct
// first list all the foods with their calories, protein, fat, and carbs
var foods strings.Builder
for _, food := range response.Foods {
foods.WriteString(fmt.Sprintf("%s (%s):\n", food.Description, food.Portion))
foods.WriteString(fmt.Sprintf("Calories: %.2f\n", food.Calories))
foods.WriteString(fmt.Sprintf("Protein: %.2f\n", food.Protein))
foods.WriteString(fmt.Sprintf("Fat: %.2f\n", food.Fat))
foods.WriteString(fmt.Sprintf("Carbs: %.2f\n", food.Carbs))
foods.WriteString("\n")
}
// then list the total calories, protein, fat, and carbs
total := response.Total
var totalString strings.Builder
totalString.WriteString("Total:\n")
totalString.WriteString(fmt.Sprintf("Calories: %.2f\n", total.Calories))
totalString.WriteString(fmt.Sprintf("Protein: %.2f\n", total.Protein))
totalString.WriteString(fmt.Sprintf("Fat: %.2f\n", total.Fat))
totalString.WriteString(fmt.Sprintf("Carbs: %.2f\n", total.Carbs))
// combine the two
var result strings.Builder
result.WriteString(foods.String())
result.WriteString(totalString.String())
return result.String()
}
func answerMachine(ctx context.Context, b *bot.Bot) {
for mg := range mh.OutputChannel {
log.Printf("Sending ChatGPT response to chat %d", mg.ChatID)
var text string
if mg.ChatGPTError != nil {
text = fmt.Sprintf("Error processing image: %s", mg.ChatGPTError)
} else {
text = FormatChatGPTResponse(mg.ChatGPTResponse)
}
_, err := b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: mg.ChatID,
Text: text,
ReplyParameters: &models.ReplyParameters{
MessageID: mg.ReplyToMessageID,
},
})
if err != nil {
log.Printf("Error sending message: %s", err)
}
}
}
func statsHandler(ctx context.Context, b *bot.Bot, update *models.Update) {
if update.Message.From.Username != adminUsername {
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Text: "You are not authorized to use this command",
})
return
}
stats := stats.GetStats()
totalRequests := 0
for _, count := range stats.Requests {
totalRequests += count
}
// prepare stats message in Markdown format
var statsMessage strings.Builder
statsMessage.WriteString("*Stats*\n")
statsMessage.WriteString("```\n")
statsMessage.WriteString(fmt.Sprintf("Total requests: %d\n", totalRequests))
for username, count := range stats.Requests {
statsMessage.WriteString(fmt.Sprintf("%s: %d\n", username, count))
}
statsMessage.WriteString(fmt.Sprintf("Download errors: %d\n", stats.DownloadErrors))
statsMessage.WriteString(fmt.Sprintf("Unrecognized commands: %d\n", stats.UnrecognizedCommands))
statsMessage.WriteString("```")
b.SendMessage(ctx, &bot.SendMessageParams{
ChatID: update.Message.Chat.ID,
Text: statsMessage.String(),
ParseMode: models.ParseModeMarkdown,
})
}
func messageToMediaItem(ctx context.Context, b *bot.Bot, message *models.Message) (*MediaItem, error) {
if len(message.Photo) == 0 {
return nil, fmt.Errorf("no photo in message")
}
// find biggest photo
photo := message.Photo[0]
for _, p := range message.Photo {
if p.FileSize > photo.FileSize {
photo = p
}
}
file, err := b.GetFile(ctx, &bot.GetFileParams{
FileID: photo.FileID,
})
if err != nil {
return nil, fmt.Errorf("error getting file: %w", err)
}
link := b.FileDownloadLink(file)
return &MediaItem{
GroupID: message.MediaGroupID,
ChatID: message.Chat.ID,
Caption: message.Caption,
URL: link,
ReplyToMessageID: message.ID,
}, nil
}
func handler(ctx context.Context, b *bot.Bot, update *models.Update) {
if update.Message == nil {
log.Printf("Received update without message")
return
}
log.Printf("[%s]: received message: '%s'", update.Message.From.Username, update.Message.Text)
// convert update.Message to json and print it
data, _ := json.MarshalIndent(update.Message, "", " ")
log.Printf("Message: %s", data)
mi, err := messageToMediaItem(ctx, b, update.Message)
if err != nil {
log.Printf("Error converting message to media item: %s", err)
return
}
log.Printf("Message: %v", mi)
mh.InputChannel <- mi
stats.AddRequest(update.Message.From.Username)
}