Skip to content

Commit

Permalink
Support parse_mode for telegram
Browse files Browse the repository at this point in the history
  • Loading branch information
orsinium authored and muesli committed Oct 2, 2020
1 parent d22455f commit 0c9f153
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
17 changes: 17 additions & 0 deletions bees/telegrambee/telegrambee.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ import (
"github.com/muesli/beehive/bees"
)

const (
ModeHTML = telegram.ModeHTML
ModeMarkdown = "MarkdownV2"
ModePlain = ""
)

// TelegramBee is a Bee that can connect to Telegram.
type TelegramBee struct {
bees.Bee
Expand All @@ -53,15 +59,26 @@ func (mod *TelegramBee) Action(action bees.Action) []bees.Placeholder {
case "send":
chatID := ""
text := ""
parseMode := ""
action.Options.Bind("chat_id", &chatID)
action.Options.Bind("text", &text)
action.Options.Bind("parse_mode", &parseMode)

cid, err := strconv.ParseInt(chatID, 10, 64)
if err != nil {
panic("Invalid telegram chat ID")
}

// use newer version of markdown parser
if parseMode == telegram.ModeMarkdown {
parseMode = ModeMarkdown
}
if parseMode != ModePlain && parseMode != ModeHTML && parseMode != ModeMarkdown {
panic("Invalid parse mode")
}

msg := telegram.NewMessage(cid, text)
msg.ParseMode = parseMode
_, err = mod.bot.Send(msg)
if err != nil {
mod.Logf("Error sending message %v", err)
Expand Down
12 changes: 11 additions & 1 deletion bees/telegrambee/telegrambeefactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@

package telegrambee

import "github.com/muesli/beehive/bees"
import (
"fmt"

"github.com/muesli/beehive/bees"
)

// TelegramBeeFactory is a factory for TelegramBees.
type TelegramBeeFactory struct {
Expand Down Expand Up @@ -130,6 +134,12 @@ func (factory *TelegramBeeFactory) Actions() []bees.ActionDescriptor {
Type: "string",
Mandatory: true,
},
{
Name: "parse_mode",
Description: fmt.Sprintf("Parse mode (%s, %s, or leave empty)", ModeHTML, ModeMarkdown),
Type: "string",
Mandatory: false,
},
},
}}
return actions
Expand Down

0 comments on commit 0c9f153

Please sign in to comment.