package controller import ( "bytes" "encoding/json" "fmt" "kng_feed_api/helper" "log" "net/http" "os" "path/filepath" "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 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.NewReader(jsonStr)) req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) defer resp.Body.Close() if err != nil { fmt.Println(err) } } func sendFile(context *gin.Context, fileMark string) { var fileName = filepath.Join(helper.RootDir(), fileMark) _, err := os.Stat(fileName) if err != nil { if os.IsNotExist(err) { context.JSON(http.StatusInternalServerError, gin.H{ "description": "The file does not exist yet. Contact the administrator.", "err": "ERR-001", }, ) return } else { context.JSON(http.StatusInternalServerError, gin.H{ "description": err.Error(), "err": "unknown error", }, ) return } } context.FileAttachment(fileName, fileMark) context.Status(http.StatusOK) }