Skip to content

Commit

Permalink
feat: add option to disable markdown parsing for message (resolve #67)
Browse files Browse the repository at this point in the history
  • Loading branch information
muety committed Dec 28, 2023
1 parent 921f773 commit 4bd1fd4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ Following inlets are currently available:

You can also **define your own, custom inlets** in YAML. Further documentation on this and about the individual pre-existing inlets is available [here](/inlets).

For all inlets, the following options are available to be passed as query parameters:

| Parameter | Description |
|-------------------------|-----------------------------------------------------------------------------------|
| `disable_link_previews` | Disable a preview version of web links detected in the message. Default: `false`. |
| `disable_markdown` | Disable the message being attempted to be parsed as Markdown. Default: `false`. |

Example: `POST https://telepush.dev/api/inlets/default?disable_markdown=true&disable_link_previews=true`.

## 📊 Metrics

Fundamental [Prometheus](https://prometheus) metrics are exposed under `/metrics`, if the `-metrics` flag gets passed.
Expand Down
5 changes: 4 additions & 1 deletion inlets/config_inlet.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ func (c *ConfigInlet) Handler(h http.Handler) http.Handler {

ctx := r.Context()
ctx = context.WithValue(ctx, config.KeyMessage, message)
ctx = context.WithValue(ctx, config.KeyParams, &model.MessageOptions{DisableLinkPreviews: true})
ctx = context.WithValue(ctx, config.KeyParams, &model.MessageOptions{
DisableLinkPreviews: strings.ToLower(r.URL.Query().Get("disable_link_previews")) == "true",
DisableMarkdown: strings.ToLower(r.URL.Query().Get("disable_markdown")) == "true",
})

h.ServeHTTP(w, r.WithContext(ctx))
})
Expand Down
12 changes: 11 additions & 1 deletion inlets/default/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/muety/telepush/model"
"github.com/muety/telepush/util"
"net/http"
"strings"

"github.com/muety/telepush/inlets"
)
Expand Down Expand Up @@ -37,6 +38,15 @@ func (i *DefaultInlet) Handler(h http.Handler) http.Handler {
return
}

// make query params take precedence
q := r.URL.Query()
if strings.ToLower(q.Get("disable_link_previews")) == "true" {
m.Options.DisableLinkPreviews = true
}
if strings.ToLower(q.Get("disable_markdown")) == "true" {
m.Options.DisableMarkdown = true
}

if len(m.Origin) == 0 {
m.Origin = model.DefaultOrigin
}
Expand All @@ -60,7 +70,7 @@ func (i *DefaultInlet) tryParseBody(r *http.Request) (m model.MessageWithOptions
func (i *DefaultInlet) tryParseQuery(r *http.Request) (m model.MessageWithOptions, err error) {
query := r.URL.Query()
queryParams := make(map[string]string)
for k := range r.URL.Query() {
for k := range query {
queryParams[k] = query.Get(k)
}
decoder, _ := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Expand Down
8 changes: 8 additions & 0 deletions model/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ type MessageWithOptions struct {

type MessageOptions struct {
DisableLinkPreviews bool `json:"disable_link_previews" mapstructure:"disable_link_previews"`
DisableMarkdown bool `json:"disable_markdown" mapstructure:"disable_markdown"`
}

func (o *MessageOptions) ParseMode() string {
if o.DisableMarkdown {
return ""
}
return "Markdown"
}
2 changes: 1 addition & 1 deletion resolvers/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (r FileResolver) Resolve(recipientId string, m *model.Message, params *mode
err = api.SendDocument(&model.TelegramOutDocument{
ChatId: recipientId,
Caption: "*" + m.Origin + "* sent a document",
ParseMode: "Markdown",
ParseMode: params.ParseMode(),
Document: &model.TelegramInputFile{
Name: m.Filename,
Data: decodedFile,
Expand Down
2 changes: 1 addition & 1 deletion resolvers/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (r TextResolver) Resolve(recipientId string, m *model.Message, params *mode
err := api.SendMessage(&model.TelegramOutMessage{
ChatId: recipientId,
Text: m.Text,
ParseMode: "Markdown",
ParseMode: params.ParseMode(),
DisableLinkPreview: disableLinkPreview,
})

Expand Down

0 comments on commit 4bd1fd4

Please sign in to comment.