kefu/wechathook/service/service.go

150 lines
3.9 KiB
Go

package service
import (
"errors"
"fmt"
"github.com/tidwall/gjson"
"log"
"time"
"wechathook/models"
)
func GetSelfInfo() (string, error) {
port := models.FindSettingDefault("wechatPort", "8055")
url := fmt.Sprintf("http://127.0.0.1:%s/DaenWxHook/client/", port)
data := map[string]string{
"type": "Q0003",
}
respBytes, err := PostJSON(url, data)
respString := string(respBytes)
log.Println("当前登录信息:", respString)
if err != nil {
return "", err
}
return respString, nil
}
// 同意好友请求
func AgreeFirend(v3, v4 string) (string, error) {
port := models.FindSettingDefault("wechatPort", "8055")
url := fmt.Sprintf("http://127.0.0.1:%s/DaenWxHook/client/", port)
data := map[string]interface{}{
"type": "Q0017",
"data": map[string]interface{}{
"scene": "6",
"v3": v3,
"v4": v4,
},
}
respBytes, err := PostJSON(url, data)
respString := string(respBytes)
log.Println("同意好友:", respString)
if err != nil {
return "", err
}
return respString, nil
}
// 调用客服接口
func GetReplyByAPI(visitorId, visitorName, avatar, msg string) (string, error) {
url := models.FindSetting("apiBase").Value
openaiBase := models.FindSetting("openaiBase").Value
openaiKey := models.FindSetting("openaiKey").Value
openaiModel := models.FindSetting("openaiModel").Value
if url == "" && openaiBase != "" && openaiKey != "" {
//扣子智能体
if openaiModel == "coze" {
coze := &Coze{
BOT_ID: openaiBase,
API_KEY: openaiKey,
}
history := make([]CozeCompletionMessage, 0)
messageContent, _ := coze.ChatCoze(visitorId, visitorId, msg, history)
return messageContent, nil
}
gpt := NewChatGptTool(openaiBase, openaiKey)
message := []Gpt3Dot5Message{
{
Role: "user",
Content: msg,
},
}
res, err := gpt.ChatGPT3Dot5Turbo(message, openaiModel)
return res, err
}
data := map[string]interface{}{
"visitor_id": "wechathook|" + visitorId,
"visitor_name": visitorName,
"avatar": avatar,
"content": msg,
}
log.Println("客服:", data)
respBytes, err := PostJSON(url, data)
respString := string(respBytes)
log.Println("客服:", respString)
if err != nil {
return "", err
}
resStr := TrimHtml(gjson.Get(respString, "result.content").String())
return resStr, nil
}
// 从本地获取回复
func GetReplyFromLocal(message string) string {
result := SearchKeywordReplys(models.FindSetting("keywords").Value, message)
finalReply := models.FindSetting("finalReply").Value
replyTemplate := models.FindSetting("replyTemplate").Value
if result == "" {
result = finalReply
}
if result != "" && replyTemplate != "" && !IsWindowsPath(result) {
result = fmt.Sprintf(replyTemplate, result)
}
return result
}
// 获取好友信息
func GetFirendInfo(wxid string) (string, error) {
port := models.FindSettingDefault("wechatPort", "8055")
url := fmt.Sprintf("http://127.0.0.1:%s/DaenWxHook/client/", port)
data := map[string]interface{}{
"type": "Q0004",
"data": map[string]interface{}{
"wxid": wxid,
},
}
respBytes, err := PostJSON(url, data)
respString := string(respBytes)
log.Println("好友信息:", respString)
if err != nil {
return "", err
}
return respString, nil
}
// 发送微信消息
func SendWechatMsg(wxid, msg string) (string, error) {
if msg == "" {
return "", errors.New("回复内容为空!")
}
sleepTime, _ := GetRandomValue(models.FindSettingDefault("delayTime", "0"))
time.Sleep(time.Duration(sleepTime) * time.Second)
port := models.FindSettingDefault("wechatPort", "8055")
url := fmt.Sprintf("http://127.0.0.1:%s/DaenWxHook/client/", port)
data := map[string]interface{}{
"type": "Q0001",
"data": map[string]interface{}{
"wxid": wxid,
"msg": msg,
},
}
respBytes, err := PostJSON(url, data)
respString := string(respBytes)
log.Println("发送信息:", respString)
if err != nil {
return "", err
}
return respString, nil
}