Merge pull request #5516 from rainzm/feat/feishu_webhook

feat: Add SendWebhookRobotMessage for feishu driver
This commit is contained in:
Zexi Li
2020-03-14 20:45:27 +08:00
committed by GitHub
2 changed files with 32 additions and 0 deletions
@@ -35,6 +35,8 @@ const (
ApiRobotSendMessage = "https://open.feishu.cn/open-apis/message/v4/send/"
// 使用手机号或邮箱获取用户ID
ApiFetchUserID = "https://open.feishu.cn/open-apis/user/v1/batch_get_id"
// 使用 webhook 机器人发送消息
ApiWebhookRobotSendMessage = "https://open.feishu.cn/open-apis/bot/hook/"
)
var (
@@ -183,3 +185,23 @@ func (t *Tenant) UserIdByMobile(mobile string) (string, error) {
// len(list) must be positive
return list[0].GetString("open_id")
}
// SendWebhookRobotMessage will send to message to Webhook address.
// Webhook's format: https://open.feishu.cn/open-apis/bot/hook/xxxxxxxxxxxxxxxxxxxxxxxxxxx.
// The hook represents the last part of webhook: 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'.
func SendWebhookRobotMessage(hook string, msg WebhookRobotMsgReq) (*WebhookRobotMsgResp, error) {
url := ApiWebhookRobotSendMessage + hook
obj, err := Request(httputils.POST, url, http.Header{}, jsonutils.Marshal(msg))
if err != nil {
return nil, err
}
resp := new(WebhookRobotMsgResp)
err = obj.Unmarshal(resp)
if err != nil {
return nil, errors.Wrap(err, "unmarshal json")
}
if !resp.Ok {
return resp, fmt.Errorf("response error, msg: %s", resp.Error)
}
return resp, err
}
+10
View File
@@ -252,3 +252,13 @@ type UserIDRespData struct {
MobileUsers jsonutils.JSONObject `json:"mobile_users"`
MobilesNotExist []string `json:"mobiles_not_exist"`
}
type WebhookRobotMsgReq struct {
Title string `json:"title"`
Text string `json:"text"`
}
type WebhookRobotMsgResp struct {
Error string `json:"error"`
Ok bool `json:"ok"`
}