Skip to content

Commit a394911

Browse files
authored
Switch to markdownv2
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!
1 parent a0ba26b commit a394911

File tree

9 files changed

+149
-35
lines changed

9 files changed

+149
-35
lines changed

.github/workflows/go.yml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
name: Build telegram-rss-bot
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
linux_amd64:
11+
name: Build telegram-rss-bot on linux/amd64
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Set up Go 1.x
15+
uses: actions/setup-go@v2
16+
with:
17+
go-version: ^1.13
18+
id: go
19+
20+
- name: Check out code into the Go module directory
21+
uses: actions/checkout@v2
22+
23+
- name: Get dependencies
24+
run: |
25+
go get -v -t -d ./...
26+
27+
- name: Build telegram-rss-bot on linux/amd64
28+
run: go build -v -o build/telegram-rss-bot-linux-amd64
29+
30+
- name: Test
31+
run: go test -v .
32+
33+
- name: Upload a Build Artifact
34+
uses: actions/upload-artifact@v2
35+
with:
36+
# A file, directory or wildcard pattern that describes what to upload
37+
path: build/
38+
windows_amd64:
39+
name: Build telegram-rss-bot on windows/amd64
40+
runs-on: windows-latest
41+
steps:
42+
- name: Set up Go 1.x
43+
uses: actions/setup-go@v2
44+
with:
45+
go-version: ^1.13
46+
id: go
47+
48+
- name: Check out code into the Go module directory
49+
uses: actions/checkout@v2
50+
51+
- name: Get dependencies
52+
run: |
53+
go get -v -t -d ./...
54+
55+
- name: Build telegram-rss-bot on windows/amd64
56+
run: go build -v -o build/telegram-rss-bot-windows-amd64.exe
57+
58+
- name: Test
59+
run: go test -v .
60+
61+
- name: Upload a Build Artifact
62+
uses: actions/upload-artifact@v2
63+
with:
64+
# A file, directory or wildcard pattern that describes what to upload
65+
path: build/
66+
darwin_amd64:
67+
name: Build telegram-rss-bot on darwin/amd64
68+
runs-on: macos-latest
69+
steps:
70+
- name: Set up Go 1.x
71+
uses: actions/setup-go@v2
72+
with:
73+
go-version: ^1.13
74+
id: go
75+
76+
- name: Check out code into the Go module directory
77+
uses: actions/checkout@v2
78+
79+
- name: Get dependencies
80+
run: |
81+
go get -v -t -d ./...
82+
83+
- name: Build telegram-rss-bot on darwin/amd64
84+
run: go build -v -o build/telegram-rss-bot-darwin-amd64
85+
86+
- name: Test
87+
run: go test -v .
88+
89+
- name: Upload a Build Artifact
90+
uses: actions/upload-artifact@v2
91+
with:
92+
# A file, directory or wildcard pattern that describes what to upload
93+
path: build/

Makefile

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,20 @@ all: linux_amd64 darwin_amd64 windows_amd64 checksums
55

66
.PHONY: linux_amd64
77
linux_amd64:
8-
GOOS=linux GOARCH=amd64 go build -v -a -gcflags=-trimpath=$$PWD -asmflags=-trimpath=$$PWD -o build/$(binary_name)-linux-amd64
9-
10-
.PHONY: linux_i386
11-
linux_i386:
12-
GOOS=linux GOARCH=386 go build -v -a -gcflags=-trimpath=$$PWD -asmflags=-trimpath=$$PWD -o build/$(binary_name)-linux-i386
8+
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
139

1410
.PHONY: darwin_amd64
1511
darwin_amd64:
16-
GOOS=darwin GOARCH=amd64 go build -v -a -gcflags=-trimpath=$$PWD -asmflags=-trimpath=$$PWD -o build/$(binary_name)-darwin-amd64
17-
18-
.PHONY: darwin_i386
19-
darwin_i386:
20-
GOOS=darwin GOARCH=386 go build -v -a -gcflags=-trimpath=$$PWD -asmflags=-trimpath=$$PWD -o build/$(binary_name)-darwin-i386
12+
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
2113

2214
.PHONY: windows_amd64
2315
windows_amd64:
24-
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
16+
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
17+
18+
.PHONY: gh_linux_amd64
19+
gh_linux_amd64:
20+
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
2521

26-
.PHONY: windows_i386
27-
windows_i386:
28-
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
2922

3023
.PHONY: checksums
3124
checksums:

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# telegram-rss-bot
22

3+
![Build telegram-rss-bot](https://github.com/0x111/telegram-rss-bot/workflows/Build%20telegram-rss-bot/badge.svg)
4+
35
## Introduction
46
This is an another telegram bot for usage with RSS feeds.
57

chans/feeds.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"fmt"
55
"github.com/0x111/telegram-rss-bot/feeds"
66
"github.com/0x111/telegram-rss-bot/replies"
7+
"github.com/go-telegram-bot-api/telegram-bot-api"
78
log "github.com/sirupsen/logrus"
8-
"gopkg.in/telegram-bot-api.v4"
99
)
1010

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

2626
for feedPost := range feedPosts {
27-
msg := fmt.Sprintf(`
28-
%s - %s
29-
`, feedPost.Title, feedPost.Link)
27+
link := replies.FilterMessageChars(feedPost.Link)
28+
msg := fmt.Sprintf("*%s* \\- [%s](%s)", replies.FilterMessageChars(feedPost.Title), link, link)
3029
log.WithFields(log.Fields{"feedPost": feedPost, "chatID": feedPost.ChatID}).Debug("Posting feed update to the Telegram API")
3130
err := replies.SimpleMessage(Bot, feedPost.ChatID, 0, msg)
3231
if err == nil {

commands/command.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"fmt"
55
"github.com/0x111/telegram-rss-bot/feeds"
66
"github.com/0x111/telegram-rss-bot/replies"
7+
"github.com/go-telegram-bot-api/telegram-bot-api"
78
log "github.com/sirupsen/logrus"
8-
"gopkg.in/telegram-bot-api.v4"
99
"strconv"
1010
"strings"
1111
)
@@ -17,7 +17,7 @@ func AddCommand(Bot *tgbotapi.BotAPI, update *tgbotapi.Update) {
1717
commandArguments := strings.Split(update.Message.CommandArguments(), " ")
1818

1919
if len(commandArguments) < 2 {
20-
log.Debug("Not enough arguments. We need \"/add name url\"")
20+
log.Debug("Not enough arguments\\. We need \"/add name url\"")
2121
return
2222
}
2323

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

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

5353
if len(commandArguments) < 1 {
54-
panic("Not enough arguments. We need \"/delete id\"")
54+
panic("Not enough arguments\\. We need \"/delete id\"")
5555
}
5656

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

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

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

@@ -76,5 +76,5 @@ func HelpCommand(Bot *tgbotapi.BotAPI, update *tgbotapi.Update) {
7676
/list - With this command you are able to list all the existing feeds with their ID numbers
7777
/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
7878
`
79-
replies.SimpleMessage(Bot, update.Message.Chat.ID, update.Message.MessageID, txt)
79+
replies.SimpleMessage(Bot, update.Message.Chat.ID, update.Message.MessageID, replies.FilterMessageChars(txt))
8080
}

feeds/feed.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"github.com/0x111/telegram-rss-bot/db"
88
"github.com/0x111/telegram-rss-bot/models"
99
"github.com/0x111/telegram-rss-bot/replies"
10+
"github.com/go-telegram-bot-api/telegram-bot-api"
1011
"github.com/mmcdole/gofeed"
1112
log "github.com/sirupsen/logrus"
12-
"gopkg.in/telegram-bot-api.v4"
1313
"time"
1414
)
1515

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

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

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

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

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.14
44

55
require (
66
github.com/PuerkitoBio/goquery v1.5.1 // indirect
7+
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
78
github.com/mattn/go-sqlite3 v2.0.3+incompatible
89
github.com/mmcdole/gofeed v1.0.0-beta2
910
github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf // indirect

replies/reply.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@ package replies
22

33
import (
44
"github.com/0x111/telegram-rss-bot/models"
5-
"gopkg.in/telegram-bot-api.v4"
5+
"github.com/go-telegram-bot-api/telegram-bot-api"
66
"strconv"
7+
"strings"
78
)
89

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

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

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

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

24-
msg.ParseMode = "markdown"
25+
msg.ParseMode = "markdownv2"
2526
msg.DisableWebPagePreview = true
2627

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

38-
msg.ParseMode = "markdown"
39+
msg.ParseMode = "markdownv2"
3940
msg.DisableWebPagePreview = false
4041

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

4748
return nil
4849
}
50+
51+
func FilterMessageChars(msg string) string {
52+
var markdownEscaper = strings.NewReplacer(
53+
"_", "\\_",
54+
"*", "\\*",
55+
"[", "\\[",
56+
"]", "\\]",
57+
"(", "\\(",
58+
")", "\\)",
59+
"~", "\\~",
60+
"`", "\\`",
61+
">", "\\>",
62+
"#", "\\#",
63+
"+", "\\+",
64+
"-", "\\-",
65+
"=", "\\=",
66+
"|", "\\|",
67+
"{", "\\{",
68+
"}", "\\}",
69+
".", "\\.",
70+
"!", "\\!",
71+
)
72+
73+
return markdownEscaper.Replace(msg)
74+
}

telegram-rss-bot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"github.com/0x111/telegram-rss-bot/conf"
77
"github.com/0x111/telegram-rss-bot/db"
88
"github.com/0x111/telegram-rss-bot/migrations"
9+
"github.com/go-telegram-bot-api/telegram-bot-api"
910
log "github.com/sirupsen/logrus"
10-
"gopkg.in/telegram-bot-api.v4"
1111
)
1212

1313
func main() {

0 commit comments

Comments
 (0)