Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Jenkins 中文社区
Wechat Backend
提交
9a0144c2
W
Wechat Backend
项目概览
Jenkins 中文社区
/
Wechat Backend
通知
2
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
Wechat Backend
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
9a0144c2
编写于
12月 02, 2018
作者:
JenkinsInChina
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Test pass: news type
上级
eaaea0c9
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
141 addition
and
51 deletion
+141
-51
articles.go
articles.go
+27
-19
articles_test.go
articles_test.go
+61
-3
main.go
main.go
+32
-16
types.go
types.go
+21
-13
未找到文件。
articles.go
浏览文件 @
9a0144c2
...
...
@@ -68,6 +68,32 @@ func initCheck() {
var
respMap
=
make
(
map
[
string
]
interface
{})
func
responseHandler
(
yamlContent
[]
byte
)
{
reps
:=
ResponseBody
{}
err
:=
yaml
.
Unmarshal
(
yamlContent
,
&
reps
)
if
err
==
nil
{
log
.
Println
(
reps
.
Kind
,
reps
.
Keyword
,
reps
)
reps
.
MsgType
=
reps
.
Kind
switch
reps
.
Kind
{
case
"text"
:
text
:=
TextResponseBody
{}
yaml
.
Unmarshal
(
yamlContent
,
&
text
)
respMap
[
reps
.
Keyword
]
=
text
case
"image"
:
image
:=
ImageResponseBody
{}
yaml
.
Unmarshal
(
yamlContent
,
&
image
)
respMap
[
reps
.
Keyword
]
=
image
case
"news"
:
news
:=
NewsResponseBody
{}
yaml
.
Unmarshal
(
yamlContent
,
&
news
)
respMap
[
reps
.
Keyword
]
=
news
}
}
else
{
fmt
.
Println
(
err
)
}
}
func
update
()
{
root
:=
CONFIG
+
"/management/auto-reply"
files
,
err
:=
ioutil
.
ReadDir
(
root
)
...
...
@@ -82,25 +108,7 @@ func update() {
content
,
err
:=
ioutil
.
ReadFile
(
root
+
"/"
+
file
.
Name
())
if
err
==
nil
{
reps
:=
ResponseBody
{}
err
=
yaml
.
Unmarshal
([]
byte
(
content
),
&
reps
)
if
err
==
nil
{
log
.
Println
(
reps
.
Kind
,
reps
.
Keyword
,
reps
)
reps
.
MsgType
=
reps
.
Kind
switch
reps
.
Kind
{
case
"text"
:
text
:=
TextResponseBody
{}
yaml
.
Unmarshal
([]
byte
(
content
),
&
text
)
log
.
Println
(
text
)
respMap
[
reps
.
Keyword
]
=
text
case
"image"
:
case
"link"
:
}
}
else
{
fmt
.
Println
(
err
)
}
responseHandler
(
content
)
}
else
{
log
.
Println
(
"Can't read file "
,
file
.
Name
())
}
...
...
articles_test.go
浏览文件 @
9a0144c2
package
main
import
"testing"
import
(
"testing"
func
TestInitCheck
(
t
*
testing
.
T
)
{
initCheck
()
"github.com/stretchr/testify/assert"
)
// func TestInitCheck(t *testing.T) {
// initCheck()
// }
func
TestImageResponseBody
(
t
*
testing
.
T
)
{
yml
:=
`
msgType: image
kind: image
keyword: hi
content: say hello from jenkins.
image:
mediaID: mediaId
`
responseHandler
([]
byte
(
yml
))
resp
:=
respMap
[
"hi"
]
if
resp
==
nil
{
t
.
Error
(
"Can't find response by keyword: hi."
)
}
imageResp
,
ok
:=
resp
.
(
ImageResponseBody
)
if
!
ok
{
t
.
Error
(
"Get the wrong type, should be ImageResponseBody."
)
}
assert
.
Equal
(
t
,
imageResp
.
Image
.
MediaID
,
"mediaId"
,
"ImageResponseBody parse error, can't find the correct mediaId: "
,
imageResp
.
Image
.
MediaID
)
}
func
TestNewsResponseBody
(
t
*
testing
.
T
)
{
yml
:=
`
kind: news
keyword: about
msgType: news
articleCount: 1
articles:
- title: "title"
description: "desc"
picUrl: "http://pic.com"
url: "http://blog.com"
`
responseHandler
([]
byte
(
yml
))
resp
:=
respMap
[
"about"
]
if
resp
==
nil
{
t
.
Error
(
"Can't find response by keyword: about."
)
return
}
newsResp
,
ok
:=
resp
.
(
NewsResponseBody
)
if
!
ok
{
t
.
Error
(
"Get the wrong type, should be NewsResponseBody."
)
}
assert
.
Equal
(
t
,
newsResp
.
Articles
.
Articles
[
0
]
.
Title
,
"title"
,
"title parse error."
)
}
main.go
浏览文件 @
9a0144c2
...
...
@@ -74,6 +74,24 @@ func procRequest(w http.ResponseWriter, r *http.Request) {
}
fmt
.
Fprintf
(
w
,
string
(
textResp
))
return
}
else
if
image
,
ok
:=
resp
.
(
ImageResponseBody
);
ok
{
imageResp
,
err
:=
makeImageResponseBody
(
textRequestBody
.
ToUserName
,
textRequestBody
.
FromUserName
,
image
.
Image
.
MediaID
)
if
err
!=
nil
{
log
.
Println
(
"Wechat Service: makeTextResponseBody error: "
,
err
)
return
}
log
.
Println
(
"response"
,
string
(
imageResp
))
fmt
.
Fprintf
(
w
,
string
(
imageResp
))
return
}
else
if
news
,
ok
:=
resp
.
(
NewsResponseBody
);
ok
{
newsResp
,
err
:=
makeNewsResponseBody
(
textRequestBody
.
ToUserName
,
textRequestBody
.
FromUserName
,
news
)
if
err
!=
nil
{
log
.
Println
(
"Wechat Service: makeNewsResponseBody error: "
,
err
)
return
}
log
.
Println
(
"response"
,
string
(
newsResp
))
fmt
.
Fprintf
(
w
,
string
(
newsResp
))
return
}
else
{
log
.
Println
(
"type error"
,
ok
)
}
...
...
@@ -81,15 +99,6 @@ func procRequest(w http.ResponseWriter, r *http.Request) {
log
.
Printf
(
"can't find keyword %s
\n
"
,
keyword
)
}
}
responseTextBody
,
err
:=
makeNewsResponseBody
(
textRequestBody
.
ToUserName
,
textRequestBody
.
FromUserName
,
"Hello, "
+
textRequestBody
.
FromUserName
)
if
err
!=
nil
{
log
.
Println
(
"Wechat Service: makeTextResponseBody error: "
,
err
)
return
}
fmt
.
Fprintf
(
w
,
string
(
responseTextBody
))
}
}
}
...
...
@@ -105,23 +114,30 @@ func makeTextResponseBody(fromUserName, toUserName, content string) ([]byte, err
return
xml
.
MarshalIndent
(
textResponseBody
,
" "
,
" "
)
}
func
makeImageResponseBody
(
fromUserName
,
toUserName
,
mediaID
string
)
([]
byte
,
error
)
{
imageResponseBody
:=
&
ImageResponseBody
{}
imageResponseBody
.
FromUserName
=
fromUserName
imageResponseBody
.
ToUserName
=
toUserName
imageResponseBody
.
MsgType
=
"image"
imageResponseBody
.
CreateTime
=
time
.
Duration
(
time
.
Now
()
.
Unix
())
imageResponseBody
.
Image
=
Image
{
MediaID
:
mediaID
,
}
return
xml
.
MarshalIndent
(
imageResponseBody
,
" "
,
" "
)
}
func
makeWelcomeResponseBody
(
fromUserName
string
,
toUserName
string
)
([]
byte
,
error
)
{
return
makeTextResponseBody
(
fromUserName
,
toUserName
,
"welcome"
)
}
func
makeNewsResponseBody
(
fromUserName
,
toUserName
,
content
string
)
([]
byte
,
error
)
{
func
makeNewsResponseBody
(
fromUserName
,
toUserName
string
,
news
NewsResponseBody
)
([]
byte
,
error
)
{
newsResponseBody
:=
&
NewsResponseBody
{}
newsResponseBody
.
FromUserName
=
fromUserName
newsResponseBody
.
ToUserName
=
toUserName
newsResponseBody
.
MsgType
=
"news"
newsResponseBody
.
ArticleCount
=
1
newsResponseBody
.
Articles
=
Articles
{
Articles
:
[]
Article
{{
Title
:
"来自 Jenkins 官方的消息"
,
Description
:
"来自 Jenkins 官方的消息"
,
PicUrl
:
"https://raw.githubusercontent.com/jenkins-infra/wechat/master/images/vscode-pipeline-linter/example1.gif"
,
Url
:
"https://mp.weixin.qq.com/s/4pktvfQ3tJZgqY--VgNgZQ"
,
}},
Articles
:
news
.
Articles
.
Articles
,
}
newsResponseBody
.
CreateTime
=
time
.
Duration
(
time
.
Now
()
.
Unix
())
return
xml
.
MarshalIndent
(
newsResponseBody
,
" "
,
" "
)
...
...
types.go
浏览文件 @
9a0144c2
...
...
@@ -18,37 +18,45 @@ type TextRequestBody struct {
type
ResponseBody
struct
{
Keyword
string
`json:"keyword"`
MsgType
string
`json:"msgType"`
Kind
string
`json:"kind"`
}
type
TextResponseBody
struct
{
ResponseBody
XMLName
xml
.
Name
`xml:"xml"`
MsgType
string
`json:"msgType" yaml:"msgType" xml:"MsgType"`
ToUserName
string
FromUserName
string
CreateTime
time
.
Duration
}
type
TextResponseBody
struct
{
ResponseBody
`yaml:",inline"`
XMLName
xml
.
Name
`xml:"xml"`
Content
string
}
type
NewsResponseBody
struct
{
ResponseBody
ResponseBody
`yaml:",inline"`
XMLName
xml
.
Name
`xml:"xml"`
ToUserName
string
FromUserName
string
CreateTime
time
.
Duration
ArticleCount
int
Articles
Articles
ArticleCount
int
`json:"articleCount" yaml:"articleCount" xml:"ArticleCount"`
Articles
Articles
`yaml:",inline"`
}
type
ImageResponseBody
struct
{
ResponseBody
`yaml:",inline"`
XMLName
xml
.
Name
`xml:"xml"`
Image
Image
}
type
Articles
struct
{
XMLName
xml
.
Name
`xml:"Articles"`
//
XMLName xml.Name `xml:"Articles"`
Articles
[]
Article
`xml:"item"`
}
type
Image
struct
{
MediaID
string
`json:"mediaId" yaml:"mediaID" xml:"MediaId"`
}
type
Article
struct
{
Title
string
Description
string
PicUrl
string
PicUrl
string
`json:"picUrl" yaml:"picUrl" xml:"PicUrl"`
Url
string
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录