123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- package hook
- import (
- "encoding/base64"
- "encoding/json"
- "fmt"
- "github.com/gorilla/websocket"
- "path/filepath"
- "strings"
- "wechat-api/workphone"
- )
- func (h *Hook) ConfigureMsgRecive(isEnable string, url string) (err error) {
- resp, err := h.Client.R().SetBody(&ConfigureMsgReciveReq{
- IsEnable: isEnable,
- URL: url,
- }).Post("http://" + h.ServerIp + ":" + h.WxPort + "/ConfigureMsgRecive")
- if err != nil {
- return
- }
- if !resp.IsSuccessState() {
- err = fmt.Errorf("ConfigureMsgRecive failed with status code %d", resp.StatusCode)
- return
- }
- return
- }
- func (h *Hook) SendTextMsg(wxid, msg, wxWxid string) error {
- if h.ServerIp == "" {
- conn, err := h.connWorkPhone()
- if err != nil {
- err = fmt.Errorf("SendTextMsg failed")
- return err
- }
- defer func(conn *websocket.Conn) {
- err = conn.Close()
- if err != nil {
- err = fmt.Errorf("SendTextMsg failed")
- }
- }(conn)
- encodedString := base64.StdEncoding.EncodeToString([]byte(msg))
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- message := map[string]interface{}{
- "MsgType": "TalkToFriendTask",
- "Content": map[string]interface{}{
- "WeChatId": wxWxid,
- "FriendId": wxid,
- "ContentType": "Text",
- "Content": encodedString,
- },
- }
- transportMessageJSON, err := json.Marshal(message)
- if err != nil {
- return err
- }
-
- err = conn.WriteMessage(websocket.TextMessage, transportMessageJSON)
- if err != nil {
- return fmt.Errorf("failed to send message: %v", err)
- }
-
- _, replyMessage, err := conn.ReadMessage()
- if err != nil {
- return fmt.Errorf("failed to read message: %v", err)
- }
- var replyMessageData map[string]interface{}
- err = json.Unmarshal([]byte(replyMessage), &replyMessageData)
- if err != nil {
- return fmt.Errorf("failed to send message: %v", err)
- }
- if replyMessageData["msgType"] == nil || replyMessageData["msgType"] != "MsgReceivedAck" {
- return fmt.Errorf("failed to send message: %v")
- }
- return nil
- } else {
- resp, err := h.Client.R().SetBody(&SendTextMsgReq{
- Wxid: wxid,
- Msg: msg,
- }).Post("http://" + h.ServerIp + ":" + h.WxPort + "/SendTextMsg")
- if err != nil {
- return err
- }
- if !resp.IsSuccessState() {
- err = fmt.Errorf("SendTextMsg failed with status code %d", resp.StatusCode)
- return err
- }
- return nil
- }
- }
- func (h *Hook) SendPicMsg(wxid, picpath, diyfilename, wxWxid string) (err error) {
- if h.ServerIp == "" {
- conn, err := h.connWorkPhone()
- if err != nil {
- err = fmt.Errorf("SendTextMsg failed")
- return err
- }
- defer func(conn *websocket.Conn) {
- err = conn.Close()
- if err != nil {
- err = fmt.Errorf("SendTextMsg failed")
- }
- }(conn)
- encodedString := base64.StdEncoding.EncodeToString([]byte(picpath))
- contentType := "File"
- if isImageFile(diyfilename) {
- contentType = "Picture"
- }
- message := map[string]interface{}{
- "MsgType": "TalkToFriendTask",
- "Content": map[string]interface{}{
- "WeChatId": wxWxid,
- "FriendId": wxid,
- "ContentType": contentType,
- "Content": encodedString,
- },
- }
- transportMessageJSON, err := json.Marshal(message)
- if err != nil {
- return err
- }
-
- err = conn.WriteMessage(websocket.TextMessage, transportMessageJSON)
- if err != nil {
- return fmt.Errorf("failed to send message: %v", err)
- }
-
- _, replyMessage, err := conn.ReadMessage()
- if err != nil {
- return fmt.Errorf("failed to read message: %v", err)
- }
- var replyMessageData map[string]interface{}
- err = json.Unmarshal([]byte(replyMessage), &replyMessageData)
- if err != nil {
- return fmt.Errorf("failed to send message: %v", err)
- }
- if replyMessageData["msgType"] == nil || replyMessageData["msgType"] != workphone.EnumMsgType_name[int32(workphone.EnumMsgType_MsgReceivedAck)] {
- return fmt.Errorf("failed to send message: %v")
- }
- return nil
- } else {
- resp, err := h.Client.R().SetBody(&SendPicMsgReq{
- Wxid: wxid,
- Picpath: picpath,
- Diyfilename: diyfilename,
- }).Post("http://" + h.ServerIp + ":" + h.WxPort + "/SendPicMsg")
- if err != nil {
- return err
- }
- if !resp.IsSuccessState() {
- err = fmt.Errorf("SendPicMsg failed with status code %d", resp.StatusCode)
- return err
- }
- return nil
- }
- }
- func (h *Hook) SendPicMsgLocal(wxid, picpath string) (err error) {
- resp, err := h.Client.R().SetBody(&SendPicMsgLocalReq{
- Wxid: wxid,
- Picpath: picpath,
- }).Post("http://" + h.ServerIp + ":" + h.WxPort + "/SendPicMsg")
- if err != nil {
- return
- }
- if !resp.IsSuccessState() {
- err = fmt.Errorf("SendPicMsgLocal failed with status code %d", resp.StatusCode)
- return
- }
- return
- }
- func isImageFile(filename string) bool {
- ext := strings.ToLower(filepath.Ext(filename))
- imageExtensions := []string{".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".webp"}
- for _, extToCheck := range imageExtensions {
- if ext == extToCheck {
- return true
- }
- }
- return false
- }
|