-
-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Publish to telegram #15
Merged
Merged
Changes from 2 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
d938e0f
Add telegram client, for send notification
sgaynetdinov 908cda8
Send message telegram if get new item RSS
sgaynetdinov 3ae8267
Add new package in vendor
sgaynetdinov d8fb331
Not raise error if telegram token empty
sgaynetdinov c741b89
Add 'TestSendIfChannelIDEmpty'
sgaynetdinov ee439da
Fix: 'NewTelegramClient' return 'TelegramClient' not 'nil'
sgaynetdinov 635a674
Add in environment 'TELEGRAM_TOKEN'
sgaynetdinov 80ca198
Refactoring test
sgaynetdinov be4eeb7
Add test for 'TelegramClient.tagLinkOnlySupport'
sgaynetdinov 117ef40
Add test for 'TelegramClient.getMessageHTML'
sgaynetdinov bec8544
Rename 'html_expected' -> 'htmlExpected'
sgaynetdinov 010a331
Pretty log, if error send telegram message
sgaynetdinov d3d0adb
dd prefix "@" for "channel" if not found
sgaynetdinov b8312f0
Add interface 'Notification'
sgaynetdinov 69f08f9
Set timeout for telegram
sgaynetdinov 2b98130
Revert "Set timeout for telegram"
sgaynetdinov 336fa15
Use timeout from http.Client without context timeout
sgaynetdinov c5a94a5
Pretty log
sgaynetdinov c2f5de1
Send audio if file size less 50 Mb or send only text
sgaynetdinov 727df45
Add const 'maxTelegramFileSize'
sgaynetdinov 395831a
Refactoring, message error
sgaynetdinov ec63f0e
Get filename audio file from url
sgaynetdinov 588a63f
Fix: []byte is a slice backed by pointer already
sgaynetdinov e27a045
Telegram timeout moved to opts
sgaynetdinov 7193518
Not reads file content to memory
sgaynetdinov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package proc | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
log "github.com/go-pkgz/lgr" | ||
"github.com/microcosm-cc/bluemonday" | ||
tb "gopkg.in/tucnak/telebot.v2" | ||
|
||
"github.com/umputun/feed-master/app/feed" | ||
) | ||
|
||
// TelegramClient client | ||
type TelegramClient struct { | ||
Bot *tb.Bot | ||
} | ||
|
||
// NewTelegramClient init telegram client | ||
func NewTelegramClient(token string) (*TelegramClient, error) { | ||
bot, err := tb.NewBot(tb.Settings{ | ||
Token: token, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
result := TelegramClient{ | ||
Bot: bot, | ||
} | ||
return &result, err | ||
} | ||
|
||
// Send message, skip if telegram token empty | ||
func (client TelegramClient) Send(channelID string, item feed.Item) error { | ||
if channelID == "" { | ||
return nil | ||
} | ||
|
||
message, err := client.Bot.Send( | ||
recipient{chatID: channelID}, | ||
client.getMessageHTML(item), | ||
tb.ModeHTML, | ||
tb.NoPreview, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] send telegram message: \n%s", message.Text) | ||
return err | ||
} | ||
|
||
// https://core.telegram.org/bots/api#html-style | ||
func (client TelegramClient) tagLinkOnlySupport(html string) string { | ||
p := bluemonday.NewPolicy() | ||
p.AllowAttrs("href").OnElements("a") | ||
return p.Sanitize(html) | ||
} | ||
|
||
func (client TelegramClient) getMessageHTML(item feed.Item) string { | ||
title := strings.TrimSpace(item.Title) | ||
|
||
description := client.tagLinkOnlySupport(string(item.Description)) | ||
description = strings.TrimSpace(description) | ||
|
||
messageHTML := fmt.Sprintf("%s\n\n%s\n\n%s", title, description, item.Enclosure.URL) | ||
|
||
return messageHTML | ||
} | ||
|
||
type recipient struct { | ||
chatID string | ||
} | ||
|
||
func (r recipient) Recipient() string { | ||
return r.chatID | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when starting without
TELEGRAM_TOKEN
there will be an error,There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not right, telegram support should be optional
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for "need to register this env in docker-compose?" - I think you are asking if it should be passed via docker-compose? Yes, if a user needs to set (or pass)
TELEGRAM_TOKEN
it has to be defined/declared in the composeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed