Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
开发微信报警功能并添加微信报警说明文档
  • Loading branch information
chanyipiaomiao committed Mar 22, 2018
2 parents 02cd526 + 8019710 commit 1468341
Show file tree
Hide file tree
Showing 20 changed files with 223 additions and 10 deletions.
47 changes: 39 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# devops-api

Golang + Beego编写, 提供一些运维常见操作的 http 接口,方便使用
Golang + Beego编写, 提供一些开发/运维常见操作的HTTP API接口,方便使用

# 主要功能

- 微信报警
- 2步验证(Google Authenticator验证)
- 密码存储
- 发送邮件
Expand All @@ -17,6 +18,8 @@ Golang + Beego编写, 提供一些运维常见操作的 http 接口,方便使
- [安装使用](#安装使用)
- [依赖](#依赖)
- [功能列表](#功能列表)
- [微信报警](#微信报警)
- [发送消息](#发送消息)
- [2步验证](#2步验证)
- [启用2步验证](#启用2步验证)
- [验证6位数字](#验证google-authenticator或是其他的类似的app生成的6位数字)
Expand Down Expand Up @@ -69,6 +72,7 @@ Golang + Beego编写, 提供一些运维常见操作的 http 接口,方便使
- 配置邮箱地址、端口、用户名、密码
- 配置是否启用token验证
- 配置jwt token签名字符串,请自行生成修改
- 配置微信报警的配置, corpID、warningAppAgentID、warningAppSecret,可参考文档[设置微信报警流程](/doc/weixin.md)

2. 首先初始化, 会生成root token,该root token 管理其他的token(**该步骤可选**)

Expand Down Expand Up @@ -127,14 +131,41 @@ go get github.com/astaxie/beego
go get github.com/robfig/cron
go get github.com/chanyipiaomiao/hltool
go get gopkg.in/alecthomas/kingpin.v2
go get -u github.com/satori/go.uuid
go get github.com/satori/go.uuid
go get github.com/sec51/twofactor
go get github.com/chanyipiaomiao/weixin-kit
```

[返回到目录](#目录)

# API

## 微信报警

点击查看设置[微信报警流程](/doc/weixin.md)

还需要在 conf/app.conf 中配置已下3个参数
- corpID 公司或者组织的ID
- warningAppAgentID 报警应用的ID
- warningAppSecret 报警应用的密钥

这3个参数可以在企业微信后台管理页面可以看到,详情可以查看上面的文档。


#### 发送消息

```sh
POST /api/v1/sendmsg/weixin

msgType: text 消息类型,目前只支持文本消息
toTag: 标签ID 在企业微信后台可以查看到
toUser: 用户ID 在企业微信后台可以查看到
toParty: 部门ID 在企业微信后台可以查看到
text: 要发送的文本消息
```

[返回到目录](#目录)

## 2步验证

2步验证,可以方便的集成到系统中,只需要调用3个API即可.
Expand Down Expand Up @@ -300,7 +331,7 @@ GET /api/v1/storepass/delete?id=10.10.1.2,1.1.1.1
#### 发送邮件api接口

```sh
POST /api/v1/sendmail
POST /api/v1/sendmsg/mail
```

- DEVOPS-API-TOKEN 指定API-TOKEN
Expand All @@ -318,7 +349,7 @@ POST /api/v1/sendmail

```sh
curl -X POST \
http://127.0.0.1:8080/api/v1/sendmail \
http://127.0.0.1:8080/api/v1/sendmsg/mail \
-H 'DEVOPS-API-TOKEN: 生成Token' \
-H 'content-type: multipart/form-data' \
-F subject=haha \
Expand All @@ -342,7 +373,7 @@ pip install requests
```python
import requests

url = "http://127.0.0.1:8080/api/v1/sendmail"
url = "http://127.0.0.1:8080/api/v1/sendmsg/mail"
token = "生成的Token"
headers = {'DEVOPS-API-TOKEN': token}
payload = {
Expand All @@ -362,7 +393,7 @@ print(response.json())
```python
import requests

url = "http://127.0.0.1:8080/api/v1/sendmail"
url = "http://127.0.0.1:8080/api/v1/sendmsg/mail"
token = "生成的Token"
payload = {
"subject": u"测试邮件",
Expand Down Expand Up @@ -398,7 +429,7 @@ import (
)

func main() {
url := "http://127.0.0.1:8080/api/v1/sendmail"
url := "http://127.0.0.1:8080/api/v1/sendmsg/mail"
token := "生成的Token"
o := &grequests.RequestOptions{
Headers: map[string]string{
Expand Down Expand Up @@ -436,7 +467,7 @@ import (
)

func main() {
url := "http://127.0.0.1:8080/api/v1/sendmail"
url := "http://127.0.0.1:8080/api/v1/sendmsg/mail"
token := "生成的token"

fd, err := os.Open("文件路径")
Expand Down
48 changes: 48 additions & 0 deletions common/weixin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package common

import (
"fmt"

"github.com/astaxie/beego"
weixin "github.com/chanyipiaomiao/weixin-kit"
)

var (
corpID = beego.AppConfig.String("weixin::corpID")
appSecret = beego.AppConfig.String("weixin::warningAppSecret")
accessTokenAPI = beego.AppConfig.String("weixin::accessTokenAPI")
sendMessageAPIURL = beego.AppConfig.String("weixin::sendMessageAPIURL")
)

// SendWeixinMessage 发送消息
func SendWeixinMessage(msgType, text, toTag, toUser, toParty string) (bool, error) {
agentID, err := beego.AppConfig.Int64("weixin::warningAppAgentID")
if err != nil {
return false, fmt.Errorf("get agentID from app.conf error: %s ", err)
}

message := &weixin.Message{
MsgType: msgType, // 目前只支持发送文本消息
ToTag: toTag, // ToTag 是在企业微信后台定义的标签ID,标签里面可以包含很多人,多个请用|分开
ToUser: toUser, // ToUser 是企业微信后台看到的用户的ID,多个请用|分开
ToParty: toParty, // ToParty 是企业微信后台看到的部门的ID,多个请用|分开
AgentID: agentID, // 企业应用的id,整型。可在应用的设置页面查看
Safe: 0, // 表示是否是保密消息,0表示否,1表示是,默认0
Text: &weixin.Text{
Content: text,
},
}

client := &weixin.Client{
AccessTokenAPI: accessTokenAPI,
APIURL: sendMessageAPIURL,
CorpID: corpID,
CorpSecret: appSecret,
Message: message,
}
_, err = client.SendMessage()
if err != nil {
return false, err
}
return true, nil
}
20 changes: 20 additions & 0 deletions conf/app.conf
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,25 @@ jwtokenSignString = TshTGv9QHZbGLT7nqmqfUavjazrArcN8
# 生成的二维码图片目录
qrImageDir = static/download/qr

[weixin]
# 微信相关配置

# 获取accessTokenAPI
accessTokenAPI = https://qyapi.weixin.qq.com/cgi-bin/gettoken

# 发送消息API
sendMessageAPIURL = https://qyapi.weixin.qq.com/cgi-bin/message/send

# corpID 公司或组织的ID
corpID = xxxxx

# 报警应用的ID
warningAppAgentID = 11111111

# 报警应用的密钥
warningAppSecret = xxxxxxxxxxxx



include "dev.conf"
include "prod.conf"
5 changes: 5 additions & 0 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,8 @@ type TwoStepAuthController struct {
type StorePasswordController struct {
BaseController
}

// WeixinController 发送微信消息管理
type WeixinController struct {
BaseController
}
37 changes: 37 additions & 0 deletions controllers/weixin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package controllers

import (
"devops-api/common"
"fmt"
)

// SendMessage 发送消息
func (w *WeixinController) SendMessage() {
msgType := w.GetString("msgType")
toTag := w.GetString("toTag")
toUser := w.GetString("toUser")
toParty := w.GetString("toParty")
text := w.GetString("text")

requestID := w.Data["RequestID"].(string)
sendWeixinMessageLog := map[string]interface{}{
"entryType": "SendWeixinMessage",
"requestId": requestID,
}
_, err := common.SendWeixinMessage(msgType, text, toTag, toUser, toParty)
if err != nil {
sendWeixinMessageLog["statuscode"] = 1
sendWeixinMessageLog["errmsg"] = fmt.Sprintf("%s", err)
common.GetLogger().Error(sendWeixinMessageLog, "发送微信消息")
w.Data["json"] = sendWeixinMessageLog
w.ServeJSON()
return
}
sendWeixinMessageLog["statuscode"] = 0
sendWeixinMessageLog["errmsg"] = ""
sendWeixinMessageLog["result"] = "发送成功"
common.GetLogger().Info(sendWeixinMessageLog, text)
w.Data["json"] = sendWeixinMessageLog
w.ServeJSON()
return
}
Binary file added doc/image/weixin/add_app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/image/weixin/add_app_person.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/image/weixin/add_p.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/image/weixin/app_detail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/image/weixin/dep_id.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/image/weixin/home.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/image/weixin/mycorp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/image/weixin/person_click.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/image/weixin/person_id.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/image/weixin/reg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/image/weixin/tag_click.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/image/weixin/tag_id.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/image/weixin/weixin_plugin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 67 additions & 0 deletions doc/weixin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# 微信报警流程

以前是通过微信企业号做报警,现在企业微信替代了微信企业号,所以现在只能通过企业微信来进行报警

## 1. [注册](https://work.weixin.qq.com/wework_admin/register_wx?from=myhome)企业微信

![注册](/doc/image/weixin/reg.png)

## 2. [登录](https://work.weixin.qq.com/wework_admin/loginpage_wx)企业微信后台

使用管理员的微信扫描登录管理后台,登录之后如下图:

![主页](/doc/image/weixin/home.png)

## 3. 邀请成员

通过 微信扫描二维码的方式, 把所有要接收报警的人邀请进来,确保都在通讯录里面,才能接收到报警

![邀请成员](/doc/image/weixin/add_p.png)

可以不用下载企业微信客户端

## 4. 添加应用

在企业应用里面创建一个应用

![创建应用](/doc/image/weixin/add_app.png)

![填写应用信息](/doc/image/weixin/add_app_person.png)

注意: 可以选择 部门/用户/标签 做为接收人,这一步很重要.

创建之后,点开应用

![应用详情](/doc/image/weixin/app_detail.png)

**记录 AgentId 和 Secret 这个会在调用API时用到**

## 5. 接收报警的人都要使用微信扫码关注微信插件的二维码

这一步至关重要,不关注的话接收不到报警信息

![微信插件](/doc/image/weixin/weixin_plugin.png)

## 6. 查看企业信息

**记录 CorpID,这个会在调用API时用到**

![企业信息](/doc/image/weixin/mycorp.png)

## 7. 查看通讯录,记录部门/用户/标签的 ID

部门ID

![部门ID](/doc/image/weixin/dep_id.png)

用户ID

![用户ID](/doc/image/weixin/person_click.png)
![用户ID](/doc/image/weixin/person_id.png)

标签ID

![标签ID](/doc/image/weixin/tag_click.png)
![标签ID](/doc/image/weixin/tag_id.png)

记录这3种ID,然后调用API就可以发送了.
9 changes: 7 additions & 2 deletions routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ func init() {
beego.NSRouter("/authPassword", &controllers.PasswordController{}, "post:AuthGenPassword"),
beego.NSRouter("/manualGenAuthPassword", &controllers.PasswordController{}, "get:ManualGenAuthPassword"),
),
beego.NSNamespace("/sendmail",
beego.NSRouter("", &controllers.EmailController{}, "post:SendMail"),
beego.NSNamespace("/sendmsg",
beego.NSNamespace("/mail",
beego.NSRouter("", &controllers.EmailController{}, "post:SendMail"),
),
beego.NSNamespace("/weixin",
beego.NSRouter("", &controllers.WeixinController{}, "post:SendMessage"),
),
),
beego.NSNamespace("/md5",
beego.NSRouter("", &controllers.MD5Controller{}),
Expand Down

0 comments on commit 1468341

Please sign in to comment.