mirror of
https://github.com/bbernhard/signal-cli-rest-api.git
synced 2026-05-18 13:24:15 +00:00
Compare commits
5 Commits
46bb0da9d5
...
84788c47e2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
84788c47e2 | ||
|
|
e5e21518a5 | ||
|
|
354df7472c | ||
|
|
8ea7456812 | ||
|
|
95c14a5f2b |
211
src/api/graylogapi.go
Normal file
211
src/api/graylogapi.go
Normal file
@ -0,0 +1,211 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"fmt"
|
||||
|
||||
_ "runtime/debug"
|
||||
_ "github.com/yassinebenaid/godump"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/bbernhard/signal-cli-rest-api/client"
|
||||
)
|
||||
|
||||
type AlertManagerNotification struct {
|
||||
Receiver string `json:"receiver"`
|
||||
Status string `json:"status"`
|
||||
Alerts []Alert `json:"alerts"`
|
||||
GroupLabels Labels `json:"groupLabels"`
|
||||
CommonLabels Labels `json:"commonLabels"`
|
||||
CommonAnnotations Annotations `json:"commonAnnotations"`
|
||||
ExternalURL string `json:"externalURL"`
|
||||
Version string `json:"version"`
|
||||
GroupKey string `json:"groupKey"`
|
||||
TruncatedAlerts int64 `json:"truncatedAlerts"`
|
||||
OrgID int64 `json:"orgId"`
|
||||
Title string `json:"title"`
|
||||
State string `json:"state"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type Alert struct {
|
||||
Status string `json:"status"`
|
||||
Labels Labels `json:"labels"`
|
||||
Annotations Annotations `json:"annotations"`
|
||||
StartsAt string `json:"startsAt"`
|
||||
EndsAt string `json:"endsAt"`
|
||||
GeneratorURL string `json:"generatorURL"`
|
||||
Fingerprint string `json:"fingerprint"`
|
||||
SilenceURL string `json:"silenceURL"`
|
||||
DashboardURL string `json:"dashboardURL"`
|
||||
PanelURL string `json:"panelURL"`
|
||||
Values interface{} `json:"values"`
|
||||
ValueString string `json:"valueString"`
|
||||
}
|
||||
|
||||
type Annotations struct {
|
||||
Summary string `json:"summary"`
|
||||
}
|
||||
|
||||
type Labels struct {
|
||||
Alertname string `json:"alertname"`
|
||||
Instance string `json:"instance"`
|
||||
}
|
||||
|
||||
type GrafanaMessage struct {
|
||||
Number string `json:"number"`
|
||||
Recipients string `json:"recipients"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type GraylogNotification struct {
|
||||
EventDefinitionID string `json:"event_definition_id"`
|
||||
EventDefinitionType string `json:"event_definition_type"`
|
||||
EventDefinitionTitle string `json:"event_definition_title"`
|
||||
EventDefinitionDescription string `json:"event_definition_description"`
|
||||
JobDefinitionID string `json:"job_definition_id"`
|
||||
JobTriggerID string `json:"job_trigger_id"`
|
||||
Event GraylogEvent `json:"event"`
|
||||
Backlog []interface{} `json:"backlog"`
|
||||
}
|
||||
|
||||
type GraylogEvent struct {
|
||||
ID string `json:"id"`
|
||||
EventDefinitionType string `json:"event_definition_type"`
|
||||
EventDefinitionID string `json:"event_definition_id"`
|
||||
OriginContext string `json:"origin_context"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
TimestampProcessing string `json:"timestamp_processing"`
|
||||
TimerangeStart interface{} `json:"timerange_start"`
|
||||
TimerangeEnd interface{} `json:"timerange_end"`
|
||||
Streams []string `json:"streams"`
|
||||
SourceStreams []interface{} `json:"source_streams"`
|
||||
Message string `json:"message"`
|
||||
Source string `json:"source"`
|
||||
KeyTuple []string `json:"key_tuple"`
|
||||
Key string `json:"key"`
|
||||
Priority int64 `json:"priority"`
|
||||
Alert bool `json:"alert"`
|
||||
Fields Fields `json:"fields"`
|
||||
}
|
||||
|
||||
type Fields struct {
|
||||
Recipients string `json:"recipients"`
|
||||
FromNumber string `json:"fromnumber"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// @Summary Send a signal message.
|
||||
// @Tags Messages
|
||||
// @Description Send a signal message. Set the text_mode to 'styled' in case you want to add formatting to your text message. Styling Options: *italic text*, **bold text**, ~strikethrough text~.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 201 {object} SendMessageResponse
|
||||
// @Failure 400 {object} SendMessageError
|
||||
// @Param data body SendMessageV2 true "Input Data"
|
||||
// @Router /v2/send [post]
|
||||
func (a *Api) SendAlertManagerV2(c *gin.Context) {
|
||||
var req AlertManagerNotification
|
||||
var msg GrafanaMessage
|
||||
base64Attachments := []string{}
|
||||
|
||||
err := c.BindJSON(&req)
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{"error": "Couldn't process request - invalid request"})
|
||||
log.Error(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
//fmt.Printf(">>>%s\n",[]byte(req.Message))
|
||||
|
||||
// Unmarshal or Decode the JSON to the interface.
|
||||
json.Unmarshal([]byte(req.Message), &msg)
|
||||
|
||||
// timestamp, err := a.signalClient.SendV1(msg.Number, msg.Message, msg.Recipients, base64Attachments, msg.IsGroup)
|
||||
// if err != nil {
|
||||
// c.JSON(400, Error{Msg: err.Error()})
|
||||
// return
|
||||
// }
|
||||
// c.JSON(201, SendMessageResponse{Timestamp: strconv.FormatInt(timestamp.Timestamp, 10)})
|
||||
|
||||
data, err := a.signalClient.SendV2(msg.Number, msg.Message, strings.Split(msg.Recipients,","), base64Attachments, "", nil, nil, nil, nil, nil, nil, nil, nil)
|
||||
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
case *client.RateLimitErrorType:
|
||||
if rateLimitError, ok := err.(*client.RateLimitErrorType); ok {
|
||||
extendedError := errors.New(err.Error() + ". Use the attached challenge tokens to lift the rate limit restrictions via the '/v1/accounts/{number}/rate-limit-challenge' endpoint.")
|
||||
c.JSON(429, SendMessageError{Msg: extendedError.Error(), ChallengeTokens: rateLimitError.ChallengeTokens, Account: msg.Number})
|
||||
return
|
||||
} else {
|
||||
c.JSON(400, Error{Msg: err.Error()})
|
||||
return
|
||||
}
|
||||
default:
|
||||
c.JSON(400, Error{Msg: err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(400, Error{Msg: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(201, SendMessageResponse{Timestamp: strconv.FormatInt((*data)[0].Timestamp, 10)})
|
||||
|
||||
}
|
||||
|
||||
// @Summary Send a signal message.
|
||||
// @Tags Messages
|
||||
// @Description Send a signal message. Set the text_mode to 'styled' in case you want to add formatting to your text message. Styling Options: *italic text*, **bold text**, ~strikethrough text~.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 201 {object} SendMessageResponse
|
||||
// @Failure 400 {object} SendMessageError
|
||||
// @Param data body SendMessageV2 true "Input Data"
|
||||
// @Router /v2/send [post]
|
||||
func (a *Api) SendGraylogNotificationV2(c *gin.Context) {
|
||||
var req GraylogNotification
|
||||
base64Attachments := []string{}
|
||||
// jsonData,err2 := io.ReadAll(c.Request.Body)
|
||||
//if err2 != nil {
|
||||
// log.Error(err2.Error())
|
||||
//}
|
||||
//fmt.Printf("<<<%s\n",jsonData)
|
||||
|
||||
err := c.BindJSON(&req)
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{"error": "Couldn't process request - invalid requestttttt"})
|
||||
log.Error(err.Error())
|
||||
fmt.Printf("<<<%s\n",c.Request.Body)
|
||||
return
|
||||
}
|
||||
|
||||
data, err := a.signalClient.SendV2(req.Event.Fields.FromNumber, req.Event.Fields.Message, strings.Split(req.Event.Fields.Recipients,","), base64Attachments, "", nil, nil, nil, nil, nil, nil, nil, nil)
|
||||
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
case *client.RateLimitErrorType:
|
||||
if rateLimitError, ok := err.(*client.RateLimitErrorType); ok {
|
||||
extendedError := errors.New(err.Error() + ". Use the attached challenge tokens to lift the rate limit restrictions via the '/v1/accounts/{number}/rate-limit-challenge' endpoint.")
|
||||
c.JSON(429, SendMessageError{Msg: extendedError.Error(), ChallengeTokens: rateLimitError.ChallengeTokens, Account: req.Event.Fields.FromNumber})
|
||||
return
|
||||
} else {
|
||||
c.JSON(400, Error{Msg: err.Error()})
|
||||
return
|
||||
}
|
||||
default:
|
||||
c.JSON(400, Error{Msg: err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(400, Error{Msg: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(201, SendMessageResponse{Timestamp: strconv.FormatInt((*data)[0].Timestamp, 10)})
|
||||
}
|
||||
|
||||
|
||||
@ -761,53 +761,75 @@ func (s *SignalClient) RegisterNumber(number string, useVoice bool, captcha stri
|
||||
|
||||
func (s *SignalClient) UnregisterNumber(number string, deleteAccount bool, deleteLocalData bool) error {
|
||||
if s.signalCliMode == JsonRpc {
|
||||
return errors.New("This functionality is only available in normal/native mode!")
|
||||
}
|
||||
|
||||
command := []string{"--config", s.signalCliConfig, "-a", number, "unregister"}
|
||||
if deleteAccount {
|
||||
command = append(command, "--delete-account")
|
||||
}
|
||||
|
||||
_, err := s.cliClient.Execute(true, command, "")
|
||||
|
||||
if deleteLocalData {
|
||||
command := []string{"--config", s.signalCliConfig, "-a", number, "deleteLocalAccountData"}
|
||||
_, err2 := s.cliClient.Execute(true, command, "")
|
||||
if (err2 != nil) && (err != nil) {
|
||||
err = fmt.Errorf("%w (%s)", err, err2.Error())
|
||||
} else if (err2 != nil) && (err == nil) {
|
||||
err = err2
|
||||
type Request struct {
|
||||
DeleteAccount bool `json:"delete-account,omitempty"`
|
||||
}
|
||||
}
|
||||
req := Request{}
|
||||
|
||||
return err
|
||||
if deleteAccount {
|
||||
req.DeleteAccount = true
|
||||
}
|
||||
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("unregister", &number, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if deleteLocalData {
|
||||
return s.DeleteLocalAccountData(number, false)
|
||||
}
|
||||
return nil
|
||||
|
||||
} else {
|
||||
command := []string{"--config", s.signalCliConfig, "-a", number, "unregister"}
|
||||
if deleteAccount {
|
||||
command = append(command, "--delete-account")
|
||||
}
|
||||
|
||||
_, err := s.cliClient.Execute(true, command, "")
|
||||
|
||||
if deleteLocalData {
|
||||
command := []string{"--config", s.signalCliConfig, "-a", number, "deleteLocalAccountData"}
|
||||
_, err2 := s.cliClient.Execute(true, command, "")
|
||||
if (err2 != nil) && (err != nil) {
|
||||
err = fmt.Errorf("%w (%s)", err, err2.Error())
|
||||
} else if (err2 != nil) && (err == nil) {
|
||||
err = err2
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SignalClient) DeleteLocalAccountData(number string, ignoreRegistered bool) error {
|
||||
if s.signalCliMode == JsonRpc {
|
||||
type Request struct {
|
||||
IgnoreRegistered bool `json:"ignore-registered,omitempty"`
|
||||
}
|
||||
req := Request{}
|
||||
if ignoreRegistered {
|
||||
req.IgnoreRegistered = true
|
||||
}
|
||||
if s.signalCliMode == JsonRpc {
|
||||
type Request struct {
|
||||
IgnoreRegistered bool `json:"ignore-registered,omitempty"`
|
||||
}
|
||||
req := Request{}
|
||||
if ignoreRegistered {
|
||||
req.IgnoreRegistered = true
|
||||
}
|
||||
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("deleteLocalAccountData", &number, req)
|
||||
return err
|
||||
} else {
|
||||
cmd := []string{"--config", s.signalCliConfig, "-a", number, "deleteLocalAccountData"}
|
||||
if ignoreRegistered {
|
||||
cmd = append(cmd, "--ignore-registered")
|
||||
}
|
||||
_, err := s.cliClient.Execute(true, cmd, "")
|
||||
return err
|
||||
}
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("deleteLocalAccountData", &number, req)
|
||||
return err
|
||||
} else {
|
||||
cmd := []string{"--config", s.signalCliConfig, "-a", number, "deleteLocalAccountData"}
|
||||
if ignoreRegistered {
|
||||
cmd = append(cmd, "--ignore-registered")
|
||||
}
|
||||
_, err := s.cliClient.Execute(true, cmd, "")
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SignalClient) VerifyRegisteredNumber(number string, token string, pin string) error {
|
||||
@ -1516,54 +1538,54 @@ func (s *SignalClient) GetQrCodeLink(deviceName string, qrCodeVersion int) ([]by
|
||||
}
|
||||
|
||||
func (s *SignalClient) GetDeviceLinkUri(deviceName string) (string, error) {
|
||||
if s.signalCliMode == JsonRpc {
|
||||
type StartResponse struct {
|
||||
DeviceLinkUri string `json:"deviceLinkUri"`
|
||||
}
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if s.signalCliMode == JsonRpc {
|
||||
type StartResponse struct {
|
||||
DeviceLinkUri string `json:"deviceLinkUri"`
|
||||
}
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
raw, err := jsonRpc2Client.getRaw("startLink", nil, struct{}{})
|
||||
if err != nil {
|
||||
return "", errors.New("Couldn't start link: " + err.Error())
|
||||
}
|
||||
raw, err := jsonRpc2Client.getRaw("startLink", nil, struct{}{})
|
||||
if err != nil {
|
||||
return "", errors.New("Couldn't start link: " + err.Error())
|
||||
}
|
||||
|
||||
var resp StartResponse
|
||||
if err := json.Unmarshal([]byte(raw), &resp); err != nil {
|
||||
return "", errors.New("Couldn't parse startLink response: " + err.Error())
|
||||
}
|
||||
var resp StartResponse
|
||||
if err := json.Unmarshal([]byte(raw), &resp); err != nil {
|
||||
return "", errors.New("Couldn't parse startLink response: " + err.Error())
|
||||
}
|
||||
|
||||
// Complete the linking handshake in the background, just like GetQrCodeLink does.
|
||||
s.finishLinkAsync(jsonRpc2Client, deviceName, resp.DeviceLinkUri)
|
||||
return resp.DeviceLinkUri, nil
|
||||
}
|
||||
// Complete the linking handshake in the background, just like GetQrCodeLink does.
|
||||
s.finishLinkAsync(jsonRpc2Client, deviceName, resp.DeviceLinkUri)
|
||||
return resp.DeviceLinkUri, nil
|
||||
}
|
||||
|
||||
cmd := []string{"--config", s.signalCliConfig, "link", "-n", deviceName}
|
||||
deviceLinkUri, err := s.cliClient.Execute(false, cmd, "")
|
||||
if err != nil {
|
||||
return "", errors.New("Couldn't create link URI: " + err.Error())
|
||||
}
|
||||
return strings.TrimSpace(deviceLinkUri), nil
|
||||
cmd := []string{"--config", s.signalCliConfig, "link", "-n", deviceName}
|
||||
deviceLinkUri, err := s.cliClient.Execute(false, cmd, "")
|
||||
if err != nil {
|
||||
return "", errors.New("Couldn't create link URI: " + err.Error())
|
||||
}
|
||||
return strings.TrimSpace(deviceLinkUri), nil
|
||||
}
|
||||
|
||||
func (s *SignalClient) finishLinkAsync(jsonRpc2Client *JsonRpc2Client, deviceName string, deviceLinkUri string) {
|
||||
type finishRequest struct {
|
||||
DeviceLinkUri string `json:"deviceLinkUri"`
|
||||
DeviceName string `json:"deviceName"`
|
||||
}
|
||||
type finishRequest struct {
|
||||
DeviceLinkUri string `json:"deviceLinkUri"`
|
||||
DeviceName string `json:"deviceName"`
|
||||
}
|
||||
|
||||
go func() {
|
||||
req := finishRequest{DeviceLinkUri: deviceLinkUri, DeviceName: deviceName}
|
||||
result, err := jsonRpc2Client.getRaw("finishLink", nil, &req)
|
||||
if err != nil {
|
||||
log.Debug("Error linking device: ", err.Error())
|
||||
return
|
||||
}
|
||||
log.Debug("Linking device result: ", result)
|
||||
s.signalCliApiConfig.Load(s.signalCliApiConfigPath)
|
||||
}()
|
||||
go func() {
|
||||
req := finishRequest{DeviceLinkUri: deviceLinkUri, DeviceName: deviceName}
|
||||
result, err := jsonRpc2Client.getRaw("finishLink", nil, &req)
|
||||
if err != nil {
|
||||
log.Debug("Error linking device: ", err.Error())
|
||||
return
|
||||
}
|
||||
log.Debug("Linking device result: ", result)
|
||||
s.signalCliApiConfig.Load(s.signalCliApiConfigPath)
|
||||
}()
|
||||
}
|
||||
|
||||
func (s *SignalClient) GetAccounts() ([]string, error) {
|
||||
|
||||
@ -359,6 +359,14 @@ func main() {
|
||||
{
|
||||
sendV2.POST("", api.SendV2)
|
||||
}
|
||||
sendalertmanagerV2 := v2.Group("/sendalertmanager")
|
||||
{
|
||||
sendalertmanagerV2.POST("", api.SendAlertManagerV2)
|
||||
}
|
||||
sendgraylognotificationV2 := v2.Group("/sendgraylognotification")
|
||||
{
|
||||
sendgraylognotificationV2.POST("", api.SendGraylogNotificationV2)
|
||||
}
|
||||
}
|
||||
|
||||
protocol := "http"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user