Browse Source

Оповещения в bitrix - v2

portowyi 10 months ago
parent
commit
f3f5ddd371
1 changed files with 27 additions and 6 deletions
  1. 27 6
      controller/cdn.go

+ 27 - 6
controller/cdn.go

@@ -2,8 +2,10 @@ package controller
 
 import (
 	"bytes"
+	"encoding/json"
 	"fmt"
 	"kng_feed_api/helper"
+	"log"
 	"net/http"
 	"os"
 	"path/filepath"
@@ -11,25 +13,44 @@ import (
 	"github.com/gin-gonic/gin"
 )
 
+type BitrixRequest struct {
+	DIALOG_ID string `json:"DIALOG_ID"`
+	MESSAGE   string `json:"MESSAGE"`
+}
+
 func SendXmlFeedToClient(context *gin.Context) {
 	var ra = context.Request.RemoteAddr
-	var reqUrl = context.Request.RequestURI
-
-	var preString = fmt.Sprintf(`"DIALOG_ID":%s, "MESSAGE":"Запрошен %s c ip адреса [%s]"`, "", reqUrl, ra)
-	var jsonStr = []byte(preString)
-	sendBitrix24Log(jsonStr)
+	msg := BitrixRequest{
+		DIALOG_ID: "chat128124",
+		MESSAGE:   fmt.Sprintf("Был запрошен полый фид c ip адреса [%s]", ra),
+	}
+	marshalled, err := json.Marshal(msg)
+	if err != nil {
+		log.Printf("impossible to marshall teacher: %s\n", err)
+	}
+	sendBitrix24Log(marshalled)
 
 	sendFile(context, "feed.xml")
 }
 
 func SendCsvDeltaToClient(context *gin.Context) {
+	var ra = context.Request.RemoteAddr
+	msg := BitrixRequest{
+		DIALOG_ID: "chat128124",
+		MESSAGE:   fmt.Sprintf("Был запрошен delta-фид c ip адреса [%s]", ra),
+	}
+	marshalled, err := json.Marshal(msg)
+	if err != nil {
+		log.Printf("impossible to marshall teacher: %s\n", err)
+	}
+	sendBitrix24Log(marshalled)
 	sendFile(context, "delta.csv")
 }
 
 func sendBitrix24Log(jsonStr []byte) {
 	url := "https://kng.bitrix24.ru/rest/1/vu5dqvx5sifh1334/im.message.add"
 
-	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
+	req, err := http.NewRequest("POST", url, bytes.NewReader(jsonStr))
 	req.Header.Set("Content-Type", "application/json")
 
 	client := &http.Client{}