first commit

main
Administrator 2024-05-18 01:46:35 +08:00
commit b58b94d6f5
25 changed files with 1111 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

9
.idea/aiweek.iml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/aiweek.iml" filepath="$PROJECT_DIR$/.idea/aiweek.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

70
aichat/chat.go Normal file
View File

@ -0,0 +1,70 @@
package aichat
import (
"bytes"
"fmt"
"github.com/tidwall/gjson"
"io"
"log"
"net/http"
"strings"
)
type KimiResp string
type AiReq struct {
token string
body string
url string
}
const token = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ1c2VyLWNlbnRlciIsImV4cCI6MTcyMzYyMTk1NywiaWF0IjoxNzE1ODQ1OTU3LCJqdGkiOiJjcDJybWg5a3FxNGxraGNqbTVtZyIsInR5cCI6InJlZnJlc2giLCJzdWIiOiJjbzU4anJxbG5sOTZlanF1czVnMCIsInNwYWNlX2lkIjoiY281OGpycWxubDk2ZWpxdXM1ZmciLCJhYnN0cmFjdF91c2VyX2lkIjoiY281OGpycWxubDk2ZWpxdXM1ZjAifQ.WNq2OH5egQKlnnuM4ygY2MjjmgsjhEOwHJdV7oQA66_mrHgGKluilcBuMZ5dMpClpAOnVY6wJ021dYHajzuInQ"
func NewAiReq(url string) *AiReq {
body := fmt.Sprintf("{\"model\":\"kimi\",\"messages\":[{\"role\":\"user\",\"content\":[{\"type\":\"file\",\"file_url\":{\"url\":\"%s\"}},{\"type\":\"text\",\"text\":\"帮忙分析下工单中的错别字告诉我对应的工单id\"}]}],\"use_search\":false}", url)
return &AiReq{
token: token,
body: body,
url: url,
}
}
func (a *AiReq) StartChat(kimiUrl string) KimiResp {
// 定义 POST 请求的 URL
url := kimiUrl
// 创建一个带有 JSON 负载的 POST 请求
payload := []byte(a.body) // 根据需要修改 JSON 负载
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if err != nil {
log.Fatal(err)
}
// 添加 Token 到请求头
req.Header.Add("Authorization", "Bearer "+a.token)
// 设置 Content-Type 为 application/json
req.Header.Set("Content-Type", "application/json")
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
return KimiResp(body)
}
func (k KimiResp) PrepareWechatBody() string {
wechatBody := gjson.Get(string(k), "choices.#.message.content").String()
r := strings.NewReplacer("\\n", "\n", "\\", "", "[", "", "]", "", "\"", "")
wechatBody = r.Replace(wechatBody)
fmt.Println("body", wechatBody)
//wechatBody = strings.Replace(wechatBody, "\\n", "\n", -1)
//wechatBody = strings.Replace(wechatBody, "\\", "", -1)
//wechatBody = strings.Replace(wechatBody, "[", "", -1)
//wechatBody = strings.Replace(wechatBody, "]", "", -1)
//wechatBody = strings.Replace(wechatBody, "\"", "", 1)
return wechatBody
}

8
file/conf.go Normal file
View File

@ -0,0 +1,8 @@
package file
import "aiweek/tools"
func GetFileServerUrl() string {
server := tools.NewServerConfObj().ReadYaml("./fileserver.yaml").Server[0]
return server
}

2
fileserver.yaml Normal file
View File

@ -0,0 +1,2 @@
server:
- "http://43.143.245.135:233/"

26
go.mod Normal file
View File

@ -0,0 +1,26 @@
module aiweek
go 1.22.2
require (
github.com/PuerkitoBio/goquery v1.9.2
github.com/google/uuid v1.6.0
github.com/jasonlvhit/gocron v0.0.1
github.com/tidwall/gjson v1.17.1
github.com/xuri/excelize/v2 v2.8.1
gopkg.in/yaml.v2 v2.4.0
)
require (
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/richardlehane/mscfb v1.0.4 // indirect
github.com/richardlehane/msoleps v1.0.3 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/xuri/efp v0.0.0-20231025114914-d1ff6096ae53 // indirect
github.com/xuri/nfp v0.0.0-20230919160717-d98342af3f05 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/text v0.14.0 // indirect
)

101
go.sum Normal file
View File

@ -0,0 +1,101 @@
github.com/PuerkitoBio/goquery v1.9.2 h1:4/wZksC3KgkQw7SQgkKotmKljk0M6V8TUvA8Wb4yPeE=
github.com/PuerkitoBio/goquery v1.9.2/go.mod h1:GHPCaP0ODyyxqcNoFGYlAprUFH81NuRPd0GX3Zu2Mvk=
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-redis/redis v6.15.5+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jasonlvhit/gocron v0.0.1 h1:qTt5qF3b3srDjeOIR4Le1LfeyvoYzJlYpqvG7tJX5YU=
github.com/jasonlvhit/gocron v0.0.1/go.mod h1:k9a3TV8VcU73XZxfVHCHWMWF9SOqgoku0/QlY2yvlA4=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/richardlehane/mscfb v1.0.4 h1:WULscsljNPConisD5hR0+OyZjwK46Pfyr6mPu5ZawpM=
github.com/richardlehane/mscfb v1.0.4/go.mod h1:YzVpcZg9czvAuhk9T+a3avCpcFPMUWm7gK3DypaEsUk=
github.com/richardlehane/msoleps v1.0.1/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
github.com/richardlehane/msoleps v1.0.3 h1:aznSZzrwYRl3rLKRT3gUk9am7T/mLNSnJINvN0AQoVM=
github.com/richardlehane/msoleps v1.0.3/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/xuri/efp v0.0.0-20231025114914-d1ff6096ae53 h1:Chd9DkqERQQuHpXjR/HSV1jLZA6uaoiwwH3vSuF3IW0=
github.com/xuri/efp v0.0.0-20231025114914-d1ff6096ae53/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
github.com/xuri/excelize/v2 v2.8.1 h1:pZLMEwK8ep+CLIUWpWmvW8IWE/yxqG0I1xcN6cVMGuQ=
github.com/xuri/excelize/v2 v2.8.1/go.mod h1:oli1E4C3Pa5RXg1TBXn4ENCXDV5JUMlBluUhG7c+CEE=
github.com/xuri/nfp v0.0.0-20230919160717-d98342af3f05 h1:qhbILQo1K3mphbwKh1vNm4oGezE1eF9fQWmNiIpSfI4=
github.com/xuri/nfp v0.0.0-20230919160717-d98342af3f05/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=
golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

99
main.go Normal file
View File

@ -0,0 +1,99 @@
package main
import (
"aiweek/aichat"
"aiweek/notifer"
"aiweek/tools"
"aiweek/udesk/reply"
"flag"
"fmt"
"github.com/jasonlvhit/gocron"
"log"
"os"
"time"
)
const ToNginxFilepath = "C:\\Users\\wangaocean\\Desktop\\巡检\\"
const kimiUrl = "http://43.143.245.135:8000/v1/chat/completions"
var liveMode bool
var operateTime = 1
func main() {
log.Println(`
_____
_/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____
\ __\\_ __ \/ _ \ / \ \__ \ / _ \ \ \/ \/ /\__ \ / \ / ___\
| | | | \( <_> ) Y Y \ / __ \( <_> ) \ / / __ \| | \/ /_/ >
|__| |__| \____/|__|_| / (____ /\____/ \/\_/ (____ /___| /\___ /
\/ \/ \/ \//_____/ `)
flag.BoolVar(&liveMode, "livemode", false, "即时模式默认关闭")
flag.Parse()
if !liveMode {
log.Println("目前是定时任务模式")
log.Printf("等待任务的第%v次执行...", operateTime)
// 定义任务,每周五的五点执行
gocron.Every(1).Friday().At("16:00").Do(retryJob)
// 开始定时任务
<-gocron.Start()
} else {
log.Println("目前是即时任务模式")
job()
}
}
func job() error {
e := reply.NewExcelObj()
replyObj, _ := e.SetReplyContent()
e.CreateNewExcel(replyObj)
e.SetAddress()
//fmt.Println("struct:", e.ExcelName, e.ExcelPath, e.ExcelAddress)
err := reply.CopyFile(e.ExcelPath, ToNginxFilepath+reply.ExcelName)
if err != nil {
return err
}
err = os.Remove(e.ExcelPath)
if err != nil {
return err
}
//校验文件数量超过4个删除最老的文件
fileCount, err := tools.CountFilesInDir(ToNginxFilepath)
if err != nil {
return err
}
if fileCount > 4 {
cleanFile := tools.ReadFilename(ToNginxFilepath)
err := os.Remove(ToNginxFilepath + cleanFile)
if err != nil {
return err
}
}
result := aichat.NewAiReq(e.ExcelAddress).StartChat(kimiUrl).PrepareWechatBody()
notifer.SendWechat(result)
operateTime++
return nil
}
func retryJob() {
// 设置最大重试次数
maxRetries := 3
// 设置重试间隔
retryInterval := 20 * time.Second
// 重试逻辑
for attempt := 0; attempt < maxRetries; attempt++ {
err := job()
if err == nil {
log.Println("执行成功")
return
}
fmt.Printf("尝试 %d 次失败,错误是: %s\n", attempt+1, err)
if attempt < maxRetries-1 {
// 如果不是最后一次重试,等待一段时间后重试
time.Sleep(retryInterval)
}
}
// 如果所有重试都失败了,这里可以处理失败的情况
log.Fatal("所有任务均失败!")
}

38
notifer/wechat.go Normal file
View File

@ -0,0 +1,38 @@
package notifer
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
)
const webhookURL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=30155eca-29d2-4e65-84bf-f31e2dac2074"
func SendWechat(repContent string) {
// 替换为你的企业微信机器人Webhook URL
jsonData, err := json.Marshal(repContent)
if err != nil {
fmt.Println("转换为JSON时出错:", err)
return
}
repContent = string(jsonData)
// 创建消息体
repContent = fmt.Sprintf(`{"msgtype": "text", "text": {"content": %s}}`, repContent)
fmt.Println(repContent)
// 发送HTTP POST请求到企业微信机器人Webhook
resp, err := http.Post(webhookURL, "application/json", strings.NewReader(repContent))
if err != nil {
log.Printf("发送企业微信机器人失败,错误是: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("发送企业微信机器人失败,错误是: %v", err)
}
log.Printf("发送企业微信机器人完成,状态码为:%v返回体为%v", resp.StatusCode, string(body))
}

Binary file not shown.

Binary file not shown.

93
tools/algorithm.go Normal file
View File

@ -0,0 +1,93 @@
package tools
import (
"log"
"os"
"sort"
)
//去除两个切片中的重复值
func ExcludeSlice(slice1, slice2 []string) []string {
// 创建一个map用于存储slice1中的所有元素
elementsInSlice1 := make(map[string]bool)
for _, v := range slice1 {
elementsInSlice1[v] = true
}
// 创建一个新的切片,用于存放结果
var result []string
for _, v := range slice2 {
// 如果元素不在slice1中则添加到结果切片中
if _, exists := elementsInSlice1[v]; !exists {
result = append(result, v)
}
}
return result
}
//反转切片
func ReverseSlice(s []string) []string {
length := len(s)
reversed := make([]string, length)
for i, v := range s {
reversed[length-i-1] = v
}
return reversed
}
//去掉切片中的空字符串
func RemoveEmptyStrings(s []string) []string {
var nonEmptyStrings []string
for _, str := range s {
if str != "" {
nonEmptyStrings = append(nonEmptyStrings, str)
}
}
return nonEmptyStrings
}
//slice每个元素中的换行符去掉
func RemoveNewlineElements(slice []string) []string {
var cleanSlice []string
for _, s := range slice {
if s != "\n" && s != "" {
cleanSlice = append(cleanSlice, s)
}
}
return cleanSlice
}
//slice每个元素中添加一个换行符
func AddNewlineToEachElement(slice []string) []string {
var newSlice []string
for _, s := range slice {
newSlice = append(newSlice, s+"\n")
}
return newSlice
}
//文件名排序
func ReadFilename(directory string) string {
entries, err := os.ReadDir(directory)
if err != nil {
log.Printf("读取目录失败,错误是:%v", err)
return ""
}
var s []string
// 遍历目录条目并打印文件名
for _, entry := range entries {
// 使用entry.Name()获取文件名
s = append(s, entry.Name())
}
sort.Strings(s)
readyToDelete := s[0]
return readyToDelete
}

24
tools/file.go Normal file
View File

@ -0,0 +1,24 @@
package tools
import (
"os"
"path/filepath"
)
//计算文件夹中的文件数目
func CountFilesInDir(dirPath string) (int, error) {
fileCount := 0
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
fileCount++
}
return nil
})
return fileCount, err
}

82
tools/option.go Normal file
View File

@ -0,0 +1,82 @@
package tools
import (
"gopkg.in/yaml.v2"
"io"
"log"
"os"
)
//自定义选项
type Option struct {
Filters []struct {
Filter7D string `yaml:"filter7d"`
Filter14D string `yaml:"filter14d"`
} `yaml:"filters"`
Macros []string `yaml:"macros"`
}
//自定义文件服务器地址
type ServerConf struct {
Server []string `yaml:"server"`
}
func NewOptionObj() *Option {
return &Option{}
}
func NewServerConfObj() *ServerConf {
return &ServerConf{}
}
func (ServerConf) ReadYaml(yamlFile string) *ServerConf {
file, err := os.Open(yamlFile)
if err != nil {
log.Printf("打开yaml文件错误错误是%v", err)
return nil
}
defer file.Close()
data, err := io.ReadAll(file)
if err != nil {
log.Printf("读取yaml文件错误错误是%v", err)
return nil
}
var serverconf ServerConf
err = yaml.Unmarshal(data, &serverconf)
if err != nil {
log.Printf("yaml文件内容反序列化失败错误是%v", err)
return nil
}
return &serverconf
}
func (Option) ReadYaml(yamlFile string) *Option {
file, err := os.Open(yamlFile)
if err != nil {
log.Printf("打开yaml文件错误错误是%v", err)
return nil
}
defer file.Close()
data, err := io.ReadAll(file)
if err != nil {
log.Printf("读取yaml文件错误错误是%v", err)
return nil
}
var option Option
err = yaml.Unmarshal(data, &option)
if err != nil {
log.Printf("yaml文件内容反序列化失败错误是%v", err)
return nil
}
return &option
}

18
udesk.yaml Normal file
View File

@ -0,0 +1,18 @@
filters:
- filter7d: "16523464"
- filter14d: "16523814"
macros:
- "您好,我们的客服已尝试就此问题联系您,但尚未收到您的回复,如需进一步帮助,请您及时联系我们,谢谢。"
- "您好,您反馈的问题正在处理当中,有任何进展这边第一时间跟您同步,请您耐心等待,十分感谢。"
- "您好,您反馈的问题已分配任务给工程师,请您耐心等待,稍后工程师将尽快进行处理,感谢您的等待与理解。"
- "您好,您提出的这个问题,我们已向项目经理反馈,具体细节和进展,项目经理会与您沟通,麻烦您耐心等待,感谢您的咨询,祝您生活愉快,再见。"
- "您好,您反馈的问题这边确认下,稍后答复您,请您稍等。"
- "您好,如果确认问题解决,请您关闭工单,有其他问题,请再联系我们,感谢您的咨询与反馈,祝您生活愉快,再见。"
- "您好,您的问题,这边已经做升级处理,后端有回复这边会第一时间给您反馈,辛苦您耐心等待下,感谢您的理解。"
- "您好,您反馈的问题已经收到,这边先看下,请您稍等。"
- "您好,该问题的解决方案已反馈,烦请您验证下是否解决您的问题,如已解决,请您关闭工单,有任何问题可以随时再联系我们,感谢您的支持,祝您生活愉快,再见。"
- "您好,经过您的确认问题后已解决,工单可以关闭,如有其他问题,请您随时再联系我们,感谢您的支持,祝您生活愉快,再见!"
- "您好,经过项目经理确认,工单可以暂时关闭,有任何问题请您再联系我们,感谢您的支持,祝您生活愉快,再见!"
- "您好,您反馈的漏洞问题已收到,这边会与研发进行沟通确认,尽快给您答复,请您耐心等待,感谢您的理解!"
- "您好,您的工单我们已做升级处理,我是本次为您服务的高级工程师,您的问题我这边正在处理当中,稍后为您同步进展,请您稍等。"
- "您好非常抱歉当前工单处于排队处理状态已与您沟通XX点处理该问题感谢您的理解请您耐心等待~"

35
udesk/auth/base.go Normal file
View File

@ -0,0 +1,35 @@
package auth
const (
Email = "hbqi@alauda.io"
Sign_version = "v2"
Password = "Ye_qiu@123"
Auth_token_url = "https://servicecenter-alauda.udesk.cn/open_api_v1/log_in"
)
//获取鉴权token的账号密码请求体对象
type RequestUdeskBody struct {
Email string `json:"email"`
Password string `json:"password"`
}
//udesk鉴权接口返回token对象
type UdeskToken struct {
Code string `json:"code"`
Open_api_auth_token string `json:"open_api_auth_token"`
}
// 定义认证对象
type Authobj struct {
Email,
Timestamp,
Sign,
Nonce,
Sign_version string
}
//全局声明返回体对象
var u = newRespUdeskBody()

90
udesk/auth/function.go Normal file
View File

@ -0,0 +1,90 @@
package auth
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/google/uuid"
"io"
"net/http"
"strconv"
"time"
)
//新建请求体对象
func newReqUdeskToken() RequestUdeskBody {
return RequestUdeskBody{
Email: Email,
Password: Password,
}
}
// 新建返回体对象
func newRespUdeskBody() UdeskToken {
return UdeskToken{
Code: "",
Open_api_auth_token: "",
}
}
//获取Unix时间戳
func GetTimeStamp() string {
return strconv.FormatInt(time.Now().Unix(), 10)
}
//获取nonce
func GetNonce() string {
randomUUID := uuid.New()
nonce := randomUUID.String()
return nonce
}
// sha256转换函数
func calculateSHA256(input string) string {
// 将字符串转换为字节数组
inputBytes := []byte(input)
// 创建SHA-256哈希对象
hasher := sha256.New()
// 将字节数组写入哈希对象
hasher.Write(inputBytes)
// 计算哈希值并返回
hashInBytes := hasher.Sum(nil)
hashString := hex.EncodeToString(hashInBytes)
return hashString
}
// 获取鉴权token对象
func GetUdeskAuthToken() UdeskToken {
reqBody := newReqUdeskToken()
jsonData, err := json.Marshal(reqBody)
if err != nil {
fmt.Printf("获取udesk管理员token过程中转换请求体json失败错误是%v", err)
}
payload := bytes.NewBufferString(string(jsonData))
resp, err := http.Post(Auth_token_url, "application/json", payload)
if err != nil {
fmt.Printf("获取udesk管理员token过程中请求获取token接口失败错误是%v", err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("获取udesk管理员token过程中获取token接口响应失败错误是%v", err)
}
json.Unmarshal(body, &u)
return u
}

17
udesk/auth/geturl.go Normal file
View File

@ -0,0 +1,17 @@
package auth
import "strings"
//最终请求的url后缀拼接
func Geturlstring(url string) string {
var authobj = u.getAuthobj() //必须要同步处理timestamp和sign的关系否则会鉴权失败所以从同一个结构体中取值
var builder strings.Builder
builder.WriteString(url)
builder.WriteString("email=" + authobj.Email)
builder.WriteString("&timestamp=" + authobj.Timestamp)
builder.WriteString("&sign=" + authobj.Sign)
builder.WriteString("&nonce=" + authobj.Nonce)
builder.WriteString("&sign_version=" + authobj.Sign_version)
return builder.String()
}

37
udesk/auth/method.go Normal file
View File

@ -0,0 +1,37 @@
package auth
import (
"strings"
)
//获取token字符串
func (UdeskToken) getTokenString() string {
token := GetUdeskAuthToken().Open_api_auth_token
return token
}
//计算sign并返回authobj
func (UdeskToken) getAuthobj() Authobj {
token := u.getTokenString()
timestamp := GetTimeStamp()
nonce := GetNonce()
var builder strings.Builder
builder.WriteString(Email + "&")
builder.WriteString(token + "&")
builder.WriteString(timestamp + "&")
builder.WriteString(nonce + "&")
builder.WriteString(Sign_version)
str2sha256 := builder.String()
hashResult := calculateSHA256(str2sha256)
//fmt.Println(str2sha256)
return Authobj{
Email: Email,
Timestamp: timestamp,
Sign: hashResult,
Nonce: nonce,
Sign_version: Sign_version,
}
}

130
udesk/filter/function.go Normal file
View File

@ -0,0 +1,130 @@
package filter
import (
"aiweek/tools"
"aiweek/udesk/auth"
"fmt"
"github.com/tidwall/gjson"
"io"
"log"
"net/http"
"strconv"
"strings"
)
//* 获取工单过滤器下的符合条件的工单id *
// 获取工单总共的页数
func GetUdeskTicketId(filterId string) (*int64, error) {
filterUrl := fmt.Sprintf("https://servicecenter-alauda.udesk.cn/open_api_v1/tickets/tickets_in_filter?filter_id=%v&", filterId)
filterUrl = auth.Geturlstring(filterUrl)
resp, err := http.Get(filterUrl)
if err != nil {
// 处理错误
log.Printf("获取工单过滤器下的工单信息失败,发送请求过程,错误是:%v", err)
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
// 处理错误
log.Printf("获取工单过滤器下的工单信息失败,读取返回体过程,错误是:%v", err)
return nil, err
}
jsonData := string(body)
totalPages := gjson.Get(jsonData, "meta.total_pages").Int()
return &totalPages, nil
}
// 根据上面获取的页数,分别请求接口取到工单id切片
func LoopGetTicketId(totalPages *int64, filterId string) ([]string, error) {
var idSlice = make([]string, 0)
for i := int64(1); i <= *totalPages; i++ {
filterUrl := fmt.Sprintf("https://servicecenter-alauda.udesk.cn/open_api_v1/tickets/tickets_in_filter?filter_id=%v", filterId)
getIdUrl := auth.Geturlstring(filterUrl+"&") + "&page=" + strconv.FormatInt(i, 10)
resp, err := http.Get(getIdUrl)
if err != nil {
// 处理错误
log.Printf("获取工单过滤器下的工单id失败发送请求过程错误是%v", err)
return nil, err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Printf("获取工单过滤器下的工单id失败关闭返回体错误是%v", err)
}
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
// 处理错误
log.Printf("获取工单过滤器下的工单id失败读取返回体过程错误是%v", err)
return nil, err
}
jsonData := string(body)
ticketIdstr := gjson.Get(jsonData, "contents.#.ticket.id").String()
sliceStr := strings.Trim(ticketIdstr, "[]")
numbers := strings.Split(sliceStr, ",")
log.Printf("处理id切片中...第%v次处理...\n", i)
idSlice = append(idSlice, numbers...)
log.Printf("%T,%v", idSlice, idSlice)
}
log.Println("处理完成~owo~")
return idSlice, nil
}
//把udeskid转换为cloudid
func Id2CloudId(id string) string {
detailUrl := auth.Geturlstring("https://servicecenter-alauda.udesk.cn/open_api_v1/tickets/detail?") + "&id=" + id
resp, err := http.Get(detailUrl)
if err != nil {
// 处理错误
log.Printf("获取工单过滤器下的cloudid失败发送请求过程错误是%v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
// 处理错误
log.Printf("获取工单过滤器下的cloudid失败读取返回体过程错误是%v", err)
}
jsonData := string(body)
cloudId := gjson.Get(jsonData, "ticket.custom_fields.TextField_219254").String()
return cloudId
}
//获取上周到上上周的工单
func GetWeeklyTicket() []string {
optionobj := tools.NewOptionObj()
Opt := optionobj.ReadYaml("./udesk.yaml").Filters
sevenDayPages, err := GetUdeskTicketId(Opt[0].Filter7D)
if err != nil {
log.Printf("获取7天的工单过滤器中的page信息失败错误是%v", err)
}
fourteenDayPages, err := GetUdeskTicketId(Opt[1].Filter14D)
if err != nil {
log.Printf("获取14天的工单过滤器中的page信息失败错误是%v", err)
}
sevenDaytTickets, err := LoopGetTicketId(sevenDayPages, Opt[0].Filter7D)
if err != nil {
log.Printf("获取7天的工单过滤器中的ticket信息失败错误是%v", err)
}
fourteenDaytickets, err := LoopGetTicketId(fourteenDayPages, Opt[1].Filter14D)
if err != nil {
log.Printf("获取14天的工单过滤器中的ticket信息失败错误是%v", err)
}
fmt.Println("7天", sevenDaytTickets)
fmt.Println("14天", fourteenDaytickets)
weeklyTicket := tools.ExcludeSlice(sevenDaytTickets, fourteenDaytickets)
fmt.Println("上周到上上周的工单:", weeklyTicket)
return weeklyTicket
}

27
udesk/reply/base.go Normal file
View File

@ -0,0 +1,27 @@
package reply
import "time"
//定义回复结构体。
type Reply struct {
replyContent map[string][]string
}
type Excel struct {
ExcelName string
ExcelPath string
ExcelAddress string
Reply //继承回复
}
var ExcelName = "weeklyReport" + time.Now().Format("2006-01-02") + ".xlsx"
// 创建新的回复结构体对象。
func NewExcelObj() *Excel {
return &Excel{
ExcelName: ExcelName,
ExcelAddress: "",
}
}

40
udesk/reply/function.go Normal file
View File

@ -0,0 +1,40 @@
package reply
import (
"aiweek/tools"
"io"
"os"
)
// 加载宏
func excludeMacros() []string {
macros := tools.NewOptionObj().ReadYaml("./udesk.yaml").Macros
return macros
}
func CopyFile(srcPath, destPath string) error {
// 打开源文件
src, err := os.Open(srcPath)
if err != nil {
return err
}
defer src.Close()
// 创建目标文件(这将自动创建任何必需的目录)
dest, err := os.Create(destPath)
if err != nil {
return err
}
defer dest.Close()
// 复制文件内容
if _, err = io.Copy(dest, src); err != nil {
return err
}
// 确保目标文件被写入磁盘
if err := dest.Sync(); err != nil {
return err
}
return nil
}

143
udesk/reply/method.go Normal file
View File

@ -0,0 +1,143 @@
package reply
import (
"aiweek/file"
"aiweek/tools"
"aiweek/udesk/auth"
"aiweek/udesk/filter"
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/tidwall/gjson"
"github.com/xuri/excelize/v2"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
)
//设置真正的id-content键值对。
func (r *Reply) SetReplyContent() (*Reply, error) {
uDeskId := filter.GetWeeklyTicket()
contentMap := make(map[string][]string)
//计数器
j := 1
for _, v := range uDeskId {
idUrl := fmt.Sprintf("https://servicecenter-alauda.udesk.cn/open_api_v1/tickets/%v/replies?", v)
repliesUrl := auth.Geturlstring(idUrl)
resp, err := http.Get(repliesUrl)
if err != nil {
// 处理错误
log.Printf("获取工单过滤器下的回复信息失败,发送请求过程,错误是:%v\n", err)
return nil, err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
// 处理错误
log.Printf("获取工单过滤器下的回复信息失败,读取返回体过程,错误是:%v\n", err)
return nil, err
}
jsonData := string(body)
//*查找回复内容和配图
repliesContent := gjson.Get(jsonData, "replies").Array()
var s = make([]string, 0)
//排除关于客户的回复
for _, v := range repliesContent {
b := v.String()
replyType := gjson.Get(b, "author.user_type").String()
if replyType == "agent" {
finalContent := gjson.Get(b, "content").String()
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(finalContent))
doc.Find("p,img").Each(func(i int, selection *goquery.Selection) {
imgSrc, exists := selection.Attr("src")
if exists {
s = append(s, imgSrc)
s = append(s, "\n")
}
s = append(s, selection.Text())
//排除掉宏的内容
macros := excludeMacros()
s = tools.ExcludeSlice(macros, s)
s = tools.RemoveEmptyStrings(s)
s = append(s, "\n")
})
}
}
repliesContentSlice := tools.ReverseSlice(s)
repliesContentSlice = tools.RemoveNewlineElements(repliesContentSlice)
repliesContentSlice = tools.AddNewlineToEachElement(repliesContentSlice)
//跳过没有回复内容的工单。
if len(repliesContentSlice) == 0 {
continue
}
//工单id转换为cloudid
cloudId := filter.Id2CloudId(v)
log.Printf("正在处理map中的第%v个数据,键是%v\n", j, v)
if cloudId == "" {
contentMap[v] = repliesContentSlice
} else {
contentMap[cloudId] = repliesContentSlice
}
log.Printf("第%v个数据数据处理完成\n", j)
j++
}
r.replyContent = contentMap
return r, nil
}
func (e *Excel) CreateNewExcel(reply *Reply) { // 示例的 map
// 创建一个新的 Excel 文件
f := excelize.NewFile()
// 设置工作表的名称
index, err := f.NewSheet("Sheet1")
if err != nil {
return
}
// 在第一行,第一列写入标题
f.SetCellValue("Sheet1", "A1", "工单id")
f.SetCellValue("Sheet1", "B1", "工单回复")
//遍历 map将键和值写入 Excel 表格
row := 2
for key, value := range reply.replyContent {
log.Println(row, key, value)
f.SetCellValue("Sheet1", "A"+strconv.Itoa(row), key)
f.SetCellValue("Sheet1", "B"+strconv.Itoa(row), value)
row++
}
// 设置工作表为默认激活状态
f.SetActiveSheet(index)
// 保存 Excel 文件
if err := f.SaveAs(ExcelName); err != nil {
log.Fatal(err)
}
wd, _ := os.Getwd()
relPath := filepath.Join(wd, ExcelName)
absPath, _ := filepath.Abs(relPath)
e.ExcelPath = absPath
}
func (e *Excel) SetAddress() {
url := file.GetFileServerUrl()
address := url + e.ExcelName
e.ExcelAddress = address
}