// OpenSAPS - Open Slack API server for everyone. // // Copyright (c) 2017, Stanislav N. aka pztrn. // All rights reserved. // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . package matrixpusher import ( // stdlib "bytes" crand "crypto/rand" "encoding/json" "errors" "fmt" "io/ioutil" "net/http" "strings" // local "gitlab.com/pztrn/opensaps/slack/message" ) // Constants for random transaction ID. const ( letterBytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // 36 possibilities letterIdxBits = 6 // 6 bits to represent 64 possibilities / indexes letterIdxMask = 1<", ``+link[3]+``, -1) } } // "\n" should be "
". messageToSend = strings.Replace(messageToSend, "\n", "
", -1) c.Log.Debugln("Crafted message:", messageToSend) // Send message. mxc.SendMessage(messageToSend) } // This function sends already prepared message to room. func (mxc *MatrixConnection) SendMessage(message string) { c.Log.Debugf("Sending message to connection '%s': '%s'", mxc.conn_name, message) // We should send notices as it is preferred behaviour for bots and // appservices. //msgStr := fmt.Sprintf(`{"msgtype": "m.text", "body": "%s", "format": "org.matrix.custom.html", "formatted_body": "%s"}`, message, message) msg := MatrixMessage{} msg.MsgType = "m.notice" msg.Body = message msg.Format = "org.matrix.custom.html" msg.FormattedBody = message msgBytes, err := json.Marshal(&msg) if err != nil { c.Log.Errorln("Failed to marshal message into JSON:", err.Error()) return } msgStr := string(msgBytes) reply, err := mxc.doPutRequest("/rooms/"+mxc.room_id+"/send/m.room.message/"+mxc.generateTnxId(), msgStr) if err != nil { c.Log.Fatalf("Failed to send message to room '%s' (conn: '%s'): %s", mxc.room_id, mxc.conn_name, err.Error()) } c.Log.Debugf("Message sent, reply: %s", string(reply)) } func (mxc *MatrixConnection) Shutdown() { c.Log.Infof("Shutting down connection '%s'...", mxc.conn_name) _, err := mxc.doPostRequest("/logout", "{}") if err != nil { c.Log.Errorf("Error occured while trying to log out from Matrix (conn %s): %s", mxc.conn_name, err.Error()) } mxc.token = "" c.Log.Infof("Connection '%s' successfully shutted down", mxc.conn_name) }