wecomchan/go-scf/service/wecomchan.go

91 lines
2.4 KiB
Go
Raw Permalink Normal View History

2021-07-04 20:55:34 +08:00
package service
import (
"bytes"
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/riba2534/wecomchan/go-scf/consts"
2021-07-07 00:10:22 +08:00
"github.com/riba2534/wecomchan/go-scf/dal"
2021-07-04 20:55:34 +08:00
"github.com/riba2534/wecomchan/go-scf/model"
"github.com/riba2534/wecomchan/go-scf/utils"
"github.com/tencentyun/scf-go-lib/events"
)
2021-07-07 00:10:22 +08:00
func WeComChanService(ctx context.Context, event events.APIGatewayRequest) map[string]interface{} {
2021-07-18 20:48:07 +08:00
sendKey := getQuery("sendkey", event)
msgType := getQuery("msg_type", event)
msg := getQuery("msg", event)
2021-07-05 01:21:03 +08:00
if msgType == "" || msg == "" {
2021-07-04 20:55:34 +08:00
return utils.MakeResp(-1, "param error")
}
2021-07-05 01:21:03 +08:00
if sendKey != consts.SEND_KEY {
2021-07-04 20:55:34 +08:00
return utils.MakeResp(-1, "sendkey error")
}
2021-07-18 20:48:07 +08:00
toUser := getQuery("to_user", event)
if toUser == "" {
toUser = consts.WECOM_TOUID
}
if err := postWechatMsg(dal.AccessToken, msg, msgType, toUser); err != nil {
2021-07-04 20:55:34 +08:00
return utils.MakeResp(0, err.Error())
}
return utils.MakeResp(0, "success")
}
func postWechatMsg(accessToken, msg, msgType, toUser string) error {
2021-07-04 20:55:34 +08:00
content := &model.WechatMsg{
ToUser: toUser,
2021-07-04 20:55:34 +08:00
AgentId: consts.WECOM_AID,
MsgType: msgType,
DuplicateCheckInterval: 600,
Text: &model.MsgText{
Content: msg,
},
}
b, _ := jsoniter.Marshal(content)
client := http.Client{Timeout: 10 * time.Second}
req, _ := http.NewRequest("POST", fmt.Sprintf(consts.WeComMsgSendURL, accessToken), bytes.NewBuffer(b))
req.Header.Set("Content-type", "application/json")
resp, err := client.Do(req)
if err != nil {
fmt.Println("[postWechatMsg] failed, err=", err)
return nil
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
fmt.Println("postWechatMsg statusCode is not 200")
return errors.New("statusCode is not 200")
}
respBodyBytes, _ := ioutil.ReadAll(resp.Body)
postResp := &model.PostResp{}
if err := jsoniter.Unmarshal(respBodyBytes, postResp); err != nil {
fmt.Println("postWechatMsg json Unmarshal failed, err=", err)
return err
}
if postResp.Errcode != 0 {
fmt.Println("postWechatMsg postResp.Errcode != 0, err=", postResp.Errmsg)
return errors.New(postResp.Errmsg)
}
return nil
}
2021-07-18 20:48:07 +08:00
func getQuery(key string, event events.APIGatewayRequest) string {
switch event.Method {
case "GET":
value := event.QueryString[key]
if len(value) > 0 && value[0] != "" {
return value[0]
}
return ""
case "POST":
return jsoniter.Get([]byte(event.Body), key).ToString()
default:
return ""
2021-07-04 20:55:34 +08:00
}
}