-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to create packages dispatcher, botconfig (#2)
- Loading branch information
Showing
4 changed files
with
206 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/mrchi/lark-dalle3-bot/pkg/dispatcher" | ||
larkee "github.com/mrchi/lark-dalle3-bot/pkg/larkee" | ||
) | ||
|
||
var commandBalance = dispatcher.Command{ | ||
Prefix: "/balance", | ||
HelpMsg: "**/balance** Get tokens balance of Bing cookie", | ||
Execute: func(prompt string, larkeeClient *larkee.LarkClient, messageId string, tanantKey string) { | ||
balance, err := bingClient.GetTokenBalance() | ||
var replyMsg string | ||
if err != nil { | ||
replyMsg = fmt.Sprintf("[Error]%s", err.Error()) | ||
} else if balance == 0 { | ||
replyMsg = "Tokens are exhausted, generation will take longer and may fail" | ||
} else { | ||
replyMsg = fmt.Sprintf("There are %d tokens left", balance) | ||
} | ||
larkeeClient.ReplyTextMessage(replyMsg, messageId, tanantKey) | ||
}, | ||
} | ||
|
||
var commandPrompt = dispatcher.Command{ | ||
Prefix: "/prompt", | ||
HelpMsg: "**/prompt <Your prompt>** Create image with prompt", | ||
Execute: func(prompt string, larkeeClient *larkee.LarkClient, messageId string, tanantKey string) { | ||
// 判断 prompt 不为空 | ||
if prompt == "" { | ||
larkeeClient.ReplyTextMessage("[Error]Prompt is empty", messageId, tanantKey) | ||
return | ||
} | ||
|
||
// 提交创建请求 | ||
writingId, err := bingClient.CreateImage(prompt) | ||
if err != nil { | ||
larkeeClient.ReplyTextMessage(fmt.Sprintf("[Error]%s", err.Error()), messageId, tanantKey) | ||
return | ||
} | ||
|
||
// 返回一些提示信息 | ||
messages := []string{"Request submitted\nWriting ID is " + writingId} | ||
balance, err := bingClient.GetTokenBalance() | ||
var balanceMsg string | ||
if err != nil { | ||
balanceMsg = fmt.Sprintf("[Error]Failed get token balance, %s", err.Error()) | ||
} else if balance == 0 { | ||
balanceMsg = "Tokens are exhausted, generation will take longer and may fail" | ||
} else { | ||
balanceMsg = fmt.Sprintf("There are %d tokens left", balance) | ||
} | ||
messages = append(messages, balanceMsg) | ||
larkeeClient.ReplyTextMessage(strings.Join(messages, "\n"), messageId, tanantKey) | ||
|
||
// 获取生成结果 | ||
imageUrls, err := bingClient.QueryResult(writingId, prompt) | ||
if err != nil { | ||
larkeeClient.ReplyTextMessage(fmt.Sprintf("[Error]%s", err.Error()), messageId, tanantKey) | ||
return | ||
} | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(len(imageUrls)) | ||
imageKeys := make([]string, len(imageUrls)) | ||
for idx, imageUrl := range imageUrls { | ||
go func(idx int, imageUrl string) { | ||
defer wg.Done() | ||
reader, err := bingClient.DownloadImage(imageUrl) | ||
if err != nil { | ||
log.Printf("Download image failed, %s", err.Error()) | ||
return | ||
} | ||
imageKey, err := larkeeClient.UploadImage(reader) | ||
if err != nil { | ||
log.Printf("Upload image failed, %s", err.Error()) | ||
return | ||
} | ||
imageKeys[idx] = imageKey | ||
}(idx, imageUrl) | ||
} | ||
wg.Wait() | ||
larkeeClient.ReplyImagesInteractiveMessage(prompt, imageKeys, messageId, tanantKey) | ||
}, | ||
} | ||
|
||
func commandHelpExecute(helpMsgs []string, larkeeClient *larkee.LarkClient, messageId string, tanantKey string) { | ||
msg := "Welcome to use DALL·E 3 bot. We now support the following commands:\n\n" + strings.Join(helpMsgs, "\n") | ||
larkeeClient.ReplyMarkdownMessage(msg, messageId, tanantKey) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package botconfig | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"os" | ||
) | ||
|
||
type BotConfig struct { | ||
BingCookie string `json:"bing_cookie"` | ||
LarkVerificationToken string `json:"lark_verification_token"` | ||
LarkEventEncryptKey string `json:"lark_event_encrypt_key"` | ||
LarkAppID string `json:"lark_app_id"` | ||
LarkAppSecret string `json:"lark_app_secret"` | ||
LarkLogLevel int `json:"lark_log_level"` | ||
LarkEventServerAddr string `json:"lark_event_server_addr"` | ||
IsFeishu bool `json:"is_feishu"` | ||
} | ||
|
||
func ReadConfigFromFile(filePath string) (*BotConfig, error) { | ||
var config BotConfig | ||
file, err := os.Open(filePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
|
||
content, err := io.ReadAll(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := json.Unmarshal(content, &config); err != nil { | ||
return nil, err | ||
} | ||
return &config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.