Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Jenkins 中文社区
Wechat Backend
提交
7319042d
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 搜索 >>
提交
7319042d
编写于
12月 15, 2018
作者:
JenkinsInChina
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
put server port into config
上级
49365041
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
111 addition
and
83 deletion
+111
-83
articles.go
articles.go
+6
-9
config/config.go
config/config.go
+7
-4
config/config.yaml
config/config.yaml
+5
-0
config/config_test.go
config/config_test.go
+17
-0
github/webhook.go
github/webhook.go
+4
-3
main.go
main.go
+72
-67
未找到文件。
articles.go
浏览文件 @
7319042d
...
...
@@ -8,6 +8,7 @@ import (
"strings"
"github.com/linuxsuren/wechat-backend/config"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/yaml.v2"
)
...
...
@@ -16,14 +17,14 @@ const (
CONFIG
=
"wechat"
)
func
initCheck
()
{
func
initCheck
(
weConfig
*
config
.
WeChatConfig
)
{
var
err
error
_
,
err
=
os
.
Stat
(
CONFIG
)
if
err
!=
nil
{
if
os
.
IsNotExist
(
err
)
{
_
,
err
=
git
.
PlainClone
(
CONFIG
,
false
,
&
git
.
CloneOptions
{
URL
:
"https://github.com/LinuxSuRen/jenkins.wechat"
,
URL
:
weConfig
.
GitURL
,
Progress
:
os
.
Stdout
,
})
if
err
!=
nil
{
...
...
@@ -45,19 +46,19 @@ func initCheck() {
}
else
{
log
.
Println
(
"open work tree with git error"
,
err
)
os
.
Remove
(
CONFIG
)
initCheck
()
initCheck
(
weConfig
)
}
}
else
{
log
.
Println
(
"open dir with git error"
,
err
)
os
.
Remove
(
CONFIG
)
initCheck
()
initCheck
(
weConfig
)
}
}
}
else
{
log
.
Println
(
"can't get config dir status"
,
err
)
if
os
.
RemoveAll
(
CONFIG
)
==
nil
{
initCheck
()
initCheck
(
weConfig
)
}
}
...
...
@@ -115,10 +116,6 @@ func update() {
}
}
func
getWelcome
()
string
{
return
""
}
func
getKeywords
()
map
[
string
]
string
{
return
nil
}
config/config.go
浏览文件 @
7319042d
...
...
@@ -9,10 +9,12 @@ import (
// WeChatConfig represents WeChat config
type
WeChatConfig
struct
{
GitURL
string
GitBranch
string
Token
string
GitHubWebHookSecret
string
GitURL
string
`yaml:"git_url"`
GitBranch
string
`yaml:"git_branch"`
Token
string
`yaml:"token"`
GitHubWebHookSecret
string
`yaml:"github_webhook_secret"`
ServerPort
int
`yaml:"server_port"`
}
// LoadConfig load config
...
...
@@ -29,4 +31,5 @@ func LoadConfig(configFile string) (config *WeChatConfig, err error) {
if
err
!=
nil
{
log
.
Printf
(
"parse config file error: %v
\n
"
,
err
)
}
return
}
config/config.yaml
0 → 100644
浏览文件 @
7319042d
token
:
Token
git_url
:
GitURL
git_branch
:
GitBranch
github_webhook_secret
:
GitHubWebHookSecret
server_port
:
80
\ No newline at end of file
config/config_test.go
0 → 100644
浏览文件 @
7319042d
package
config
import
"testing"
func
TestLoadConfig
(
t
*
testing
.
T
)
{
config
,
err
:=
LoadConfig
(
"config.yaml"
)
if
err
!=
nil
{
t
.
Errorf
(
"load config error %v"
,
err
)
}
if
config
.
Token
!=
"Token"
||
config
.
GitURL
!=
"GitURL"
||
config
.
GitBranch
!=
"GitBranch"
||
config
.
GitHubWebHookSecret
!=
"GitHubWebHookSecret"
||
config
.
ServerPort
!=
80
{
t
.
Errorf
(
"parse error, config %#v"
,
config
)
}
}
webhook.go
→
github/
webhook.go
浏览文件 @
7319042d
package
main
package
github
import
(
"log"
"net/http"
"github.com/linuxsuren/wechat-backend/config"
"gopkg.in/go-playground/webhooks.v5/github"
)
func
webhookHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
WebhookHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
,
weConfig
*
config
.
WeChatConfig
,
initCheck
func
(
*
config
.
WeChatConfig
)
)
{
hook
,
_
:=
github
.
New
(
github
.
Options
.
Secret
(
"secret"
))
payload
,
err
:=
hook
.
Parse
(
r
,
github
.
PushEvent
)
...
...
@@ -30,5 +31,5 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
}
log
.
Println
(
"Going to update wechat config."
)
initCheck
()
initCheck
(
weConfig
)
}
main.go
浏览文件 @
7319042d
...
...
@@ -13,24 +13,26 @@ import (
"time"
"github.com/linuxsuren/wechat-backend/config"
"github.com/linuxsuren/wechat-backend/github"
)
const
(
token
=
"wechat4go"
)
// WeChat represents WeChat
type
WeChat
struct
{
Config
*
config
.
WeChatConfig
}
func
makeSignature
(
timestamp
,
nonce
string
)
string
{
sl
:=
[]
string
{
t
oken
,
timestamp
,
nonce
}
func
(
w
*
WeChat
)
makeSignature
(
timestamp
,
nonce
string
)
string
{
sl
:=
[]
string
{
w
.
Config
.
T
oken
,
timestamp
,
nonce
}
sort
.
Strings
(
sl
)
s
:=
sha1
.
New
()
io
.
WriteString
(
s
,
strings
.
Join
(
sl
,
""
))
return
fmt
.
Sprintf
(
"%x"
,
s
.
Sum
(
nil
))
}
func
validateUrl
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
bool
{
func
(
we
*
WeChat
)
validateUrl
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
bool
{
timestamp
:=
strings
.
Join
(
r
.
Form
[
"timestamp"
],
""
)
nonce
:=
strings
.
Join
(
r
.
Form
[
"nonce"
],
""
)
signatureGen
:=
makeSignature
(
timestamp
,
nonce
)
signatureGen
:=
we
.
makeSignature
(
timestamp
,
nonce
)
signatureIn
:=
strings
.
Join
(
r
.
Form
[
"signature"
],
""
)
if
signatureGen
!=
signatureIn
{
...
...
@@ -41,9 +43,9 @@ func validateUrl(w http.ResponseWriter, r *http.Request) bool {
return
true
}
func
procRequest
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
we
*
WeChat
)
procRequest
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
r
.
ParseForm
()
if
!
validateUrl
(
w
,
r
)
{
if
!
we
.
validateUrl
(
w
,
r
)
{
log
.
Println
(
"Wechat Service: this http request is not from Wechat platform!"
)
return
}
...
...
@@ -51,73 +53,70 @@ func procRequest(w http.ResponseWriter, r *http.Request) {
switch
r
.
Method
{
case
"POST"
:
wechatRequest
(
w
,
r
)
we
.
we
chatRequest
(
w
,
r
)
case
"GET"
:
normalRequest
(
w
,
r
)
we
.
normalRequest
(
w
,
r
)
}
}
func
normalRequest
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
fmt
.
Fprintf
(
w
,
"welcome aboard."
)
func
(
we
*
WeChat
)
normalRequest
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
w
.
Write
([]
byte
(
"welcome aboard WeChat."
)
)
}
func
wechatRequest
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
textRequestBody
:=
parseTextRequestBody
(
r
)
func
(
we
*
WeChat
)
wechatRequest
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
textRequestBody
:=
we
.
parseTextRequestBody
(
r
)
if
textRequestBody
!=
nil
{
fmt
.
Printf
(
"Wechat Service: Recv text msg [%s] from user [%s]!"
,
textRequestBody
.
Content
,
textRequestBody
.
FromUserName
)
if
"event"
==
textRequestBody
.
MsgType
&&
"subscribe"
==
textRequestBody
.
Event
{
resp
,
err
:=
makeWelcomeResponseBody
(
textRequestBody
.
ToUserName
,
textRequestBody
.
FromUserName
)
resp
,
err
:=
we
.
replyResponse
(
"welcome"
,
textRequestBody
.
ToUserName
,
textRequestBody
.
FromUserName
)
if
err
!=
nil
{
log
.
Println
(
"Wechat Service: makeTextResponseBody error: "
,
err
)
return
log
.
Println
(
"handle welcome replay error:"
,
err
)
}
else
{
fmt
.
Fprintf
(
w
,
string
(
resp
))
}
fmt
.
Fprintf
(
w
,
string
(
resp
))
}
else
{
keyword
:=
textRequestBody
.
Content
fmt
.
Println
(
textRequestBody
.
MsgType
,
keyword
,
respMap
)
if
"text"
==
textRequestBody
.
MsgType
{
if
resp
,
ok
:=
respMap
[
keyword
];
ok
{
if
text
,
ok
:=
resp
.
(
TextResponseBody
);
ok
{
textResp
,
err
:=
makeTextResponseBody
(
textRequestBody
.
ToUserName
,
textRequestBody
.
FromUserName
,
text
.
Content
)
if
err
!=
nil
{
log
.
Println
(
"Wechat Service: makeTextResponseBody error: "
,
err
)
return
}
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
)
}
resp
,
err
:=
we
.
replyResponse
(
keyword
,
textRequestBody
.
ToUserName
,
textRequestBody
.
FromUserName
)
if
err
!=
nil
{
log
.
Println
(
"handle auto replay error:"
,
err
)
}
else
{
log
.
Printf
(
"can't find keyword %s
\n
"
,
keyword
)
fmt
.
Fprintf
(
w
,
string
(
resp
)
)
}
}
}
}
}
func
makeTextResponseBody
(
fromUserName
,
toUserName
,
content
string
)
([]
byte
,
error
)
{
func
(
we
*
WeChat
)
replyResponse
(
keyword
string
,
from
string
,
to
string
)
(
data
[]
byte
,
err
error
)
{
if
resp
,
ok
:=
respMap
[
keyword
];
ok
{
if
text
,
ok
:=
resp
.
(
TextResponseBody
);
ok
{
data
,
err
=
we
.
makeTextResponseBody
(
from
,
to
,
text
.
Content
)
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"Wechat Service: makeTextResponseBody error: %v"
,
err
)
}
}
else
if
image
,
ok
:=
resp
.
(
ImageResponseBody
);
ok
{
data
,
err
=
we
.
makeImageResponseBody
(
from
,
to
,
image
.
Image
.
MediaID
)
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"Wechat Service: makeImageResponseBody error: %v"
,
err
)
}
}
else
if
news
,
ok
:=
resp
.
(
NewsResponseBody
);
ok
{
data
,
err
=
we
.
makeNewsResponseBody
(
from
,
to
,
news
)
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"Wechat Service: makeNewsResponseBody error: %v"
,
err
)
}
}
else
{
err
=
fmt
.
Errorf
(
"type error"
)
}
}
return
}
func
(
w
*
WeChat
)
makeTextResponseBody
(
fromUserName
,
toUserName
,
content
string
)
([]
byte
,
error
)
{
textResponseBody
:=
&
TextResponseBody
{}
textResponseBody
.
FromUserName
=
fromUserName
textResponseBody
.
ToUserName
=
toUserName
...
...
@@ -127,7 +126,7 @@ func makeTextResponseBody(fromUserName, toUserName, content string) ([]byte, err
return
xml
.
MarshalIndent
(
textResponseBody
,
" "
,
" "
)
}
func
makeImageResponseBody
(
fromUserName
,
toUserName
,
mediaID
string
)
([]
byte
,
error
)
{
func
(
w
*
WeChat
)
makeImageResponseBody
(
fromUserName
,
toUserName
,
mediaID
string
)
([]
byte
,
error
)
{
imageResponseBody
:=
&
ImageResponseBody
{}
imageResponseBody
.
FromUserName
=
fromUserName
imageResponseBody
.
ToUserName
=
toUserName
...
...
@@ -139,11 +138,7 @@ func makeImageResponseBody(fromUserName, toUserName, mediaID string) ([]byte, er
return
xml
.
MarshalIndent
(
imageResponseBody
,
" "
,
" "
)
}
func
makeWelcomeResponseBody
(
fromUserName
string
,
toUserName
string
)
([]
byte
,
error
)
{
return
makeTextResponseBody
(
fromUserName
,
toUserName
,
"welcome"
)
}
func
makeNewsResponseBody
(
fromUserName
,
toUserName
string
,
news
NewsResponseBody
)
([]
byte
,
error
)
{
func
(
w
*
WeChat
)
makeNewsResponseBody
(
fromUserName
,
toUserName
string
,
news
NewsResponseBody
)
([]
byte
,
error
)
{
newsResponseBody
:=
&
NewsResponseBody
{}
newsResponseBody
.
FromUserName
=
fromUserName
newsResponseBody
.
ToUserName
=
toUserName
...
...
@@ -156,7 +151,7 @@ func makeNewsResponseBody(fromUserName, toUserName string, news NewsResponseBody
return
xml
.
MarshalIndent
(
newsResponseBody
,
" "
,
" "
)
}
func
parseTextRequestBody
(
r
*
http
.
Request
)
*
TextRequestBody
{
func
(
w
*
WeChat
)
parseTextRequestBody
(
r
*
http
.
Request
)
*
TextRequestBody
{
body
,
err
:=
ioutil
.
ReadAll
(
r
.
Body
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
...
...
@@ -168,19 +163,29 @@ func parseTextRequestBody(r *http.Request) *TextRequestBody {
return
requestBody
}
func
handler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
fmt
.
Fprintf
(
w
,
"Hi there, I love %s!"
,
r
.
URL
.
Path
[
1
:
])
}
func
main
()
{
config
.
LoadConfig
(
""
)
weConfig
,
err
:=
config
.
LoadConfig
(
"config/wechat.yaml"
)
if
err
!=
nil
{
log
.
Printf
(
"load config error %v
\n
"
,
err
)
}
initCheck
()
if
weConfig
.
ServerPort
<=
0
{
weConfig
.
ServerPort
=
8080
}
wechat
:=
WeChat
{
Config
:
weConfig
,
}
go
func
()
{
initCheck
(
weConfig
)
}()
createWxMenu
()
http
.
HandleFunc
(
"/"
,
procRequest
)
http
.
HandleFunc
(
"/"
,
wechat
.
procRequest
)
http
.
HandleFunc
(
"/status"
,
healthHandler
)
http
.
HandleFunc
(
"/webhook"
,
webhookHandler
)
http
.
HandleFunc
(
"/webhook"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
github
.
WebhookHandler
(
w
,
r
,
weConfig
,
initCheck
)
})
log
.
Fatal
(
http
.
ListenAndServe
(
":18080"
,
nil
))
log
.
Fatal
(
http
.
ListenAndServe
(
fmt
.
Sprintf
(
":%d"
,
weConfig
.
ServerPort
)
,
nil
))
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录