// 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 slackmessage "go.dev.pztrn.name/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[2]+``, -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.connName, message) // We should send notices as it is preferred behavior for bots and // appservices. 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.roomID+"/send/m.room.message/"+mxc.generateTnxID(), msgStr) if err != nil { c.Log.Fatalf("Failed to send message to room '%s' (conn: '%s'): %s", mxc.roomID, mxc.connName, err.Error()) } c.Log.Debugf("Message sent, reply: %s", string(reply)) } func (mxc *MatrixConnection) Shutdown() { c.Log.Infof("Shutting down connection '%s'...", mxc.connName) _, err := mxc.doPostRequest("/logout", "{}") if err != nil { c.Log.Errorf("Error occurred while trying to log out from Matrix (conn %s): %s", mxc.connName, err.Error()) } mxc.token = "" c.Log.Infof("Connection '%s' successfully shutted down", mxc.connName) }