Skip to content

Commit

Permalink
Switch to markdownv2
Browse files Browse the repository at this point in the history
Change markdown to markdownv2 since the older one is legacy now.
Also fix an issue where the some names of the article could cause a formatting error and would not send feed updates to their respective channels!

Also added proper escaping based on the recommendations from telegram, so every text that is not part of markdown formatting code itself, will now be escaped properly.

Upgraded the telegram bot api to a newer version!

Added github actions for automatic builds for multiple platforms!
  • Loading branch information
0x111 committed Jun 1, 2020
1 parent a0ba26b commit a394911
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 35 deletions.
93 changes: 93 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
name: Build telegram-rss-bot

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
linux_amd64:
name: Build telegram-rss-bot on linux/amd64
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.13
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Get dependencies
run: |
go get -v -t -d ./...
- name: Build telegram-rss-bot on linux/amd64
run: go build -v -o build/telegram-rss-bot-linux-amd64

- name: Test
run: go test -v .

- name: Upload a Build Artifact
uses: actions/upload-artifact@v2
with:
# A file, directory or wildcard pattern that describes what to upload
path: build/
windows_amd64:
name: Build telegram-rss-bot on windows/amd64
runs-on: windows-latest
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.13
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Get dependencies
run: |
go get -v -t -d ./...
- name: Build telegram-rss-bot on windows/amd64
run: go build -v -o build/telegram-rss-bot-windows-amd64.exe

- name: Test
run: go test -v .

- name: Upload a Build Artifact
uses: actions/upload-artifact@v2
with:
# A file, directory or wildcard pattern that describes what to upload
path: build/
darwin_amd64:
name: Build telegram-rss-bot on darwin/amd64
runs-on: macos-latest
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.13
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Get dependencies
run: |
go get -v -t -d ./...
- name: Build telegram-rss-bot on darwin/amd64
run: go build -v -o build/telegram-rss-bot-darwin-amd64

- name: Test
run: go test -v .

- name: Upload a Build Artifact
uses: actions/upload-artifact@v2
with:
# A file, directory or wildcard pattern that describes what to upload
path: build/
21 changes: 7 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,20 @@ all: linux_amd64 darwin_amd64 windows_amd64 checksums

.PHONY: linux_amd64
linux_amd64:
GOOS=linux GOARCH=amd64 go build -v -a -gcflags=-trimpath=$$PWD -asmflags=-trimpath=$$PWD -o build/$(binary_name)-linux-amd64

.PHONY: linux_i386
linux_i386:
GOOS=linux GOARCH=386 go build -v -a -gcflags=-trimpath=$$PWD -asmflags=-trimpath=$$PWD -o build/$(binary_name)-linux-i386
CGO_ENABLED=1 CC=/usr/local/bin/x86_64-linux-musl-gcc-9.2.0 GOOS=linux GOARCH=amd64 go build -v -a -ldflags "-linkmode external -extldflags -static" -gcflags "all=-trimpath=$$PWD;$$HOME" -asmflags "all=-trimpath=$$PWD;$$HOME" -o build/$(binary_name)-linux-amd64

.PHONY: darwin_amd64
darwin_amd64:
GOOS=darwin GOARCH=amd64 go build -v -a -gcflags=-trimpath=$$PWD -asmflags=-trimpath=$$PWD -o build/$(binary_name)-darwin-amd64

.PHONY: darwin_i386
darwin_i386:
GOOS=darwin GOARCH=386 go build -v -a -gcflags=-trimpath=$$PWD -asmflags=-trimpath=$$PWD -o build/$(binary_name)-darwin-i386
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build -v -a -gcflags "all=-trimpath=$$PWD;$$HOME" -asmflags "all=-trimpath=$$PWD;$$HOME" -o build/$(binary_name)-darwin-amd64

.PHONY: windows_amd64
windows_amd64:
CC=/usr/local/bin/x86_64-w64-mingw32-gcc GOOS=windows GOARCH=amd64 go build -v -a -gcflags=-trimpath=$$PWD -asmflags=-trimpath=$$PWD -o build/$(binary_name)-windows-amd64.exe
CGO_ENABLED=1 CC=/usr/local/bin/x86_64-w64-mingw32-gcc GOOS=windows GOARCH=amd64 go build -v -a -gcflags "all=-trimpath=$$PWD;$$HOME" -asmflags "all=-trimpath=$$PWD;$$HOME" -o build/$(binary_name)-windows-amd64.exe

.PHONY: gh_linux_amd64
gh_linux_amd64:
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -v -a -ldflags "-linkmode external -extldflags -static" -gcflags "all=-trimpath=$$PWD;$$HOME" -asmflags "all=-trimpath=$$PWD;$$HOME" -o build/$(binary_name)-linux-amd64

.PHONY: windows_i386
windows_i386:
CC=/usr/local/bin/x86_64-w64-mingw32-gcc GOOS=windows GOARCH=386 go build -v -a -gcflags=-trimpath=$$PWD -asmflags=-trimpath=$$PWD -o build/$(binary_name)-windows-i386.exe

.PHONY: checksums
checksums:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# telegram-rss-bot

![Build telegram-rss-bot](https://github.com/0x111/telegram-rss-bot/workflows/Build%20telegram-rss-bot/badge.svg)

## Introduction
This is an another telegram bot for usage with RSS feeds.

Expand Down
7 changes: 3 additions & 4 deletions chans/feeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"github.com/0x111/telegram-rss-bot/feeds"
"github.com/0x111/telegram-rss-bot/replies"
"github.com/go-telegram-bot-api/telegram-bot-api"
log "github.com/sirupsen/logrus"
"gopkg.in/telegram-bot-api.v4"
)

// Get Feed Updates from the feeds
Expand All @@ -24,9 +24,8 @@ func FeedPosts(Bot *tgbotapi.BotAPI) {
feedPosts := feeds.PostFeedUpdatesChan()

for feedPost := range feedPosts {
msg := fmt.Sprintf(`
%s - %s
`, feedPost.Title, feedPost.Link)
link := replies.FilterMessageChars(feedPost.Link)
msg := fmt.Sprintf("*%s* \\- [%s](%s)", replies.FilterMessageChars(feedPost.Title), link, link)
log.WithFields(log.Fields{"feedPost": feedPost, "chatID": feedPost.ChatID}).Debug("Posting feed update to the Telegram API")
err := replies.SimpleMessage(Bot, feedPost.ChatID, 0, msg)
if err == nil {
Expand Down
14 changes: 7 additions & 7 deletions commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"github.com/0x111/telegram-rss-bot/feeds"
"github.com/0x111/telegram-rss-bot/replies"
"github.com/go-telegram-bot-api/telegram-bot-api"
log "github.com/sirupsen/logrus"
"gopkg.in/telegram-bot-api.v4"
"strconv"
"strings"
)
Expand All @@ -17,7 +17,7 @@ func AddCommand(Bot *tgbotapi.BotAPI, update *tgbotapi.Update) {
commandArguments := strings.Split(update.Message.CommandArguments(), " ")

if len(commandArguments) < 2 {
log.Debug("Not enough arguments. We need \"/add name url\"")
log.Debug("Not enough arguments\\. We need \"/add name url\"")
return
}

Expand All @@ -30,7 +30,7 @@ func AddCommand(Bot *tgbotapi.BotAPI, update *tgbotapi.Update) {
txt := ""

if err == nil {
txt = fmt.Sprintf("The feed with the url [%s] was successfully added to this channel!", feedUrl)
txt = fmt.Sprintf("The feed with the url [%s] was successfully added to this channel\\!", replies.FilterMessageChars(feedUrl))
replies.SimpleMessage(Bot, chatid, update.Message.MessageID, txt)
}
}
Expand All @@ -51,7 +51,7 @@ func DeleteCommand(Bot *tgbotapi.BotAPI, update *tgbotapi.Update) {
commandArguments := strings.Split(update.Message.CommandArguments(), " ")

if len(commandArguments) < 1 {
panic("Not enough arguments. We need \"/delete id\"")
panic("Not enough arguments\\. We need \"/delete id\"")
}

feedid, _ := strconv.Atoi(commandArguments[0])
Expand All @@ -60,12 +60,12 @@ func DeleteCommand(Bot *tgbotapi.BotAPI, update *tgbotapi.Update) {
err := feeds.DeleteFeedByID(feedid, chatid, userid)

if err != nil {
txt := fmt.Sprintf("There is no feed with the id [%d]!", feedid)
txt := fmt.Sprintf("There is no feed with the id [%d]\\!", feedid)
replies.SimpleMessage(Bot, chatid, update.Message.MessageID, txt)
return
}

txt := fmt.Sprintf("The feed with the id [%d] was successfully deleted!", feedid)
txt := fmt.Sprintf("The feed with the id [%d] was successfully deleted\\!", feedid)
replies.SimpleMessage(Bot, chatid, update.Message.MessageID, txt)
}

Expand All @@ -76,5 +76,5 @@ func HelpCommand(Bot *tgbotapi.BotAPI, update *tgbotapi.Update) {
/list - With this command you are able to list all the existing feeds with their ID numbers
/delete %ID - With this command you are able to delete an added feed if you do not need it anymore. The ID parameter is required and you can get it from the /list command
`
replies.SimpleMessage(Bot, update.Message.Chat.ID, update.Message.MessageID, txt)
replies.SimpleMessage(Bot, update.Message.Chat.ID, update.Message.MessageID, replies.FilterMessageChars(txt))
}
8 changes: 4 additions & 4 deletions feeds/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/0x111/telegram-rss-bot/db"
"github.com/0x111/telegram-rss-bot/models"
"github.com/0x111/telegram-rss-bot/replies"
"github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/mmcdole/gofeed"
log "github.com/sirupsen/logrus"
"gopkg.in/telegram-bot-api.v4"
"time"
)

Expand All @@ -20,20 +20,20 @@ func AddFeed(Bot *tgbotapi.BotAPI, name string, url string, chatid int64, userid

if err != nil {
log.WithFields(log.Fields{"error": err}).Error("There was an error while querying the database!")
replies.SimpleMessage(Bot, chatid, 0, "There was an error while adding your feed! Please try again!")
replies.SimpleMessage(Bot, chatid, 0, "There was an error while adding your feed\\! Please try again\\!")
return err
}

// Check if user is providing a valid feed URL
if err := isValidFeed(url); err != nil {
log.WithFields(log.Fields{"error": err}).Debug("Invalid feed!")
replies.SimpleMessage(Bot, chatid, 0, "The feed you are trying to add is not a valid feed URL!")
replies.SimpleMessage(Bot, chatid, 0, "The feed you are trying to add is not a valid feed URL\\!")
return errors.New("invalid_feed_url")
}

if exists {
log.WithFields(log.Fields{"exists": exists}).Debug("Feed exists!")
replies.SimpleMessage(Bot, chatid, 0, "The feed you are trying to add already exists!")
replies.SimpleMessage(Bot, chatid, 0, "The feed you are trying to add already exists\\!")
return errors.New("feed_exists")
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
github.com/PuerkitoBio/goquery v1.5.1 // indirect
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/mmcdole/gofeed v1.0.0-beta2
github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf // indirect
Expand Down
36 changes: 31 additions & 5 deletions replies/reply.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ package replies

import (
"github.com/0x111/telegram-rss-bot/models"
"gopkg.in/telegram-bot-api.v4"
"github.com/go-telegram-bot-api/telegram-bot-api"
"strconv"
"strings"
)

// This function replies the list of feeds to for the command /list
func ListOfFeeds(botAPI *tgbotapi.BotAPI, feeds *[]models.Feed, chatid int64, messageid int) {
txt := "Here is the list of your added Feeds for this Room: \n"

if len(*feeds) == 0 {
txt += "There is currently no feed added to the list for this Room!\n"
txt += "There is currently no feed added to the list for this Room\\!\n"
}

for _, feed := range *feeds {
txt += "[#" + strconv.Itoa(feed.ID) + "] *" + feed.Name + "*: " + feed.Url + "\n"
txt += "[\\#" + strconv.Itoa(feed.ID) + "] *" + FilterMessageChars(feed.Name) + "*: " + FilterMessageChars(feed.Url) + "\n"
}

msg := tgbotapi.NewMessage(chatid, txt)
msg.ReplyToMessageID = messageid

msg.ParseMode = "markdown"
msg.ParseMode = "markdownv2"
msg.DisableWebPagePreview = true

botAPI.Send(msg)
Expand All @@ -35,7 +36,7 @@ func SimpleMessage(botAPI *tgbotapi.BotAPI, chatid int64, messageid int, text st
msg.ReplyToMessageID = messageid
}

msg.ParseMode = "markdown"
msg.ParseMode = "markdownv2"
msg.DisableWebPagePreview = false

_, err := botAPI.Send(msg)
Expand All @@ -46,3 +47,28 @@ func SimpleMessage(botAPI *tgbotapi.BotAPI, chatid int64, messageid int, text st

return nil
}

func FilterMessageChars(msg string) string {
var markdownEscaper = strings.NewReplacer(
"_", "\\_",
"*", "\\*",
"[", "\\[",
"]", "\\]",
"(", "\\(",
")", "\\)",
"~", "\\~",
"`", "\\`",
">", "\\>",
"#", "\\#",
"+", "\\+",
"-", "\\-",
"=", "\\=",
"|", "\\|",
"{", "\\{",
"}", "\\}",
".", "\\.",
"!", "\\!",
)

return markdownEscaper.Replace(msg)
}
2 changes: 1 addition & 1 deletion telegram-rss-bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/0x111/telegram-rss-bot/conf"
"github.com/0x111/telegram-rss-bot/db"
"github.com/0x111/telegram-rss-bot/migrations"
"github.com/go-telegram-bot-api/telegram-bot-api"
log "github.com/sirupsen/logrus"
"gopkg.in/telegram-bot-api.v4"
)

func main() {
Expand Down

0 comments on commit a394911

Please sign in to comment.