-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
224 lines (199 loc) · 6.06 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"time"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
"golang.org/x/exp/slices"
)
var telegramBot *bot.Bot
var cmdHandler cmdHandlerType
var sdAPI sdAPIType
var reqQueue ReqQueue
func sendReplyToMessage(ctx context.Context, replyToMsg *models.Message, s string) (msg *models.Message) {
var err error
msg, err = telegramBot.SendMessage(ctx, &bot.SendMessageParams{
ReplyToMessageID: replyToMsg.ID,
ChatID: replyToMsg.Chat.ID,
Text: s,
})
if err != nil {
fmt.Println(" reply send error:", err)
}
return
}
func sendTextToAdmins(ctx context.Context, s string) {
for _, chatID := range params.AdminUserIDs {
_, _ = telegramBot.SendMessage(ctx, &bot.SendMessageParams{
ChatID: chatID,
Text: s,
})
}
}
type ImageFileData struct {
data []byte
filename string
}
func handleImage(ctx context.Context, update *models.Update, fileID, filename string) {
// Are we expecting image data from this user?
if reqQueue.currentEntry.gotImageChan == nil || update.Message.From.ID != reqQueue.currentEntry.entry.Message.From.ID {
return
}
var g GetFile
d, err := g.GetFile(ctx, fileID)
if err != nil {
reqQueue.currentEntry.entry.sendReply(ctx, errorStr+": can't get file: "+err.Error())
return
}
reqQueue.currentEntry.entry.sendReply(ctx, doneStr+" downloading\n"+reqQueue.currentEntry.entry.Params.String())
// Updating the message to reply to this document.
reqQueue.currentEntry.entry.Message = update.Message
reqQueue.currentEntry.entry.ReplyMessage = nil
// Notifying the request queue that we now got the image data.
reqQueue.currentEntry.gotImageChan <- ImageFileData{
data: d,
filename: filename,
}
}
func handleMessage(ctx context.Context, update *models.Update) {
if update.Message.Text == "" {
return
}
fmt.Print("msg from ", update.Message.From.Username, "#", update.Message.From.ID, ": ", update.Message.Text, "\n")
if update.Message.Chat.ID >= 0 { // From user?
if !slices.Contains(params.AllowedUserIDs, update.Message.From.ID) {
fmt.Println(" user not allowed, ignoring")
return
}
} else { // From group ?
fmt.Print(" msg from group #", update.Message.Chat.ID)
if !slices.Contains(params.AllowedGroupIDs, update.Message.Chat.ID) {
fmt.Println(", group not allowed, ignoring")
return
}
fmt.Println()
}
// Check if message is a command.
if update.Message.Text[0] == '/' || update.Message.Text[0] == '!' {
cmd := strings.Split(update.Message.Text, " ")[0]
if strings.Contains(cmd, "@") {
cmd = strings.Split(cmd, "@")[0]
}
update.Message.Text = strings.TrimPrefix(update.Message.Text, cmd+" ")
cmdChar := string(cmd[0])
cmd = cmd[1:] // Cutting the command character.
switch cmd {
case "sd":
fmt.Println(" interpreting as cmd sd")
cmdHandler.SD(ctx, update.Message)
return
case "sdupscale":
fmt.Println(" interpreting as cmd sdupscale")
cmdHandler.SDUpscale(ctx, update.Message)
return
case "sdcancel":
fmt.Println(" interpreting as cmd sdcancel")
cmdHandler.SDCancel(ctx, update.Message)
return
case "sdmodels":
fmt.Println(" interpreting as cmd sdmodels")
cmdHandler.Models(ctx, update.Message)
return
case "sdsamplers":
fmt.Println(" interpreting as cmd sdsamplers")
cmdHandler.Samplers(ctx, update.Message)
return
case "sdembeddings":
fmt.Println(" interpreting as cmd sdembeddings")
cmdHandler.Embeddings(ctx, update.Message)
return
case "sdloras":
fmt.Println(" interpreting as cmd sdloras")
cmdHandler.LoRAs(ctx, update.Message)
return
case "sdupscalers":
fmt.Println(" interpreting as cmd sdupscalers")
cmdHandler.Upscalers(ctx, update.Message)
return
case "sdvaes":
fmt.Println(" interpreting as cmd sdvaes")
cmdHandler.VAEs(ctx, update.Message)
return
case "sdsmi":
fmt.Println(" interpreting as cmd sdsmi")
cmdHandler.SMI(ctx, update.Message)
return
case "sdhelp":
fmt.Println(" interpreting as cmd sdhelp")
cmdHandler.Help(ctx, update.Message, cmdChar)
return
case "start":
fmt.Println(" interpreting as cmd start")
if update.Message.Chat.ID >= 0 { // From user?
sendReplyToMessage(ctx, update.Message, "🤖 Welcome! This is a Telegram Bot frontend "+
"for rendering images with Stable Diffusion.\n\nMore info: https://github.com/nonoo/stable-diffusion-telegram-bot")
}
return
default:
fmt.Println(" invalid cmd")
if update.Message.Chat.ID >= 0 {
sendReplyToMessage(ctx, update.Message, errorStr+": invalid command")
}
return
}
}
if update.Message.Chat.ID >= 0 { // From user?
cmdHandler.SD(ctx, update.Message)
}
}
func telegramBotUpdateHandler(ctx context.Context, b *bot.Bot, update *models.Update) {
if update.Message == nil {
return
}
if update.Message.Document != nil {
handleImage(ctx, update, update.Message.Document.FileID, update.Message.Document.FileName)
} else if update.Message.Photo != nil && len(update.Message.Photo) > 0 {
handleImage(ctx, update, update.Message.Photo[len(update.Message.Photo)-1].FileID, "image.jpg")
} else if update.Message.Text != "" {
handleMessage(ctx, update)
}
}
func main() {
fmt.Println("stable-diffusion-telegram-bot starting...")
if err := params.Init(); err != nil {
fmt.Println("error:", err)
os.Exit(1)
}
var cancel context.CancelFunc
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
if params.SDStart && !params.DelayedSDStart {
if err := startStableDiffusionIfNeeded(ctx); err != nil {
panic(err.Error())
}
}
reqQueue.Init(ctx)
opts := []bot.Option{
bot.WithDefaultHandler(telegramBotUpdateHandler),
}
var err error
telegramBot, err = bot.New(params.BotToken, opts...)
if nil != err {
panic(fmt.Sprint("can't init telegram bot: ", err))
}
verStr, _ := versionCheckGetStr(ctx)
sendTextToAdmins(ctx, "🤖 Bot started, "+verStr)
go func() {
for {
time.Sleep(24 * time.Hour)
if s, updateNeededOrError := versionCheckGetStr(ctx); updateNeededOrError {
sendTextToAdmins(ctx, s)
}
}
}()
telegramBot.Start(ctx)
}