-
-
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
Conversation
tg, err := proc.NewTelegramClient(opts.TG) | ||
if err != nil { | ||
log.Fatalf("[ERROR] failed initilization telegram client %s, %v", opts.TG, err) | ||
} |
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,
- is this the right behavior?
- need to register this env in docker-compose?
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 compose
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.
Fixed
Looks good to me, I think I can merge it as-is, but first need to test it locally. In the meantime, I would suggest 2 more changes (we can add it after the merge, up to you):
|
I can't make it work. Created a new token, opened a new channel and all I get is "api error: Bad Request: chat not found". Btw, the message is not too helpful, knowing what chat is not found will be nice. In addition, it doesn't respect the lack of It is also unclear to me if you expect me to set a channel with |
Ok, this one is running fine, I got a bunch of messages which is good. They also seem to be formatted nice, which is also good. However, none of them publishes mp3, they all links regardless of the size for mp3. From your earlier screenshots, I thought this is going to have media published if size allows (under 50m) but looks like it doesn't do it at all. |
Add timeouts 69f08f9 |
Early, I published in preview mode only one link and telegram published audio itself. |
app/proc/telegram.go
Outdated
response := make(chan *responseTelegram) | ||
go client.send(channelID, item, response) | ||
|
||
select { |
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.
it looks wrong as it leaks goroutine on timeout. Timeout support has to be or on telegram's client level or on the level of http client injected to the telegram client.
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.
see https://github.com/tucnak/telebot/blob/v2/bot.go#L69 - you can pass http client and set timeout for the client, without any need in this dangerous workaround with goroutine.
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.
use http.Client 69f08f9
|
I'd suggest 3 for files under 50M and just a text (with a link to mp3) for bigger files |
return err | ||
} | ||
|
||
func getContentLength(url string) (int64, 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.
@umputun how mocking http response?
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.
see httptest. You can run test server and provide whatever response you need
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 an example of httptest.Server use in real life, from one of my projects https://github.com/go-pkgz/rest/blob/master/httperrors_test.go#L16
app/proc/telegram.go
Outdated
} | ||
|
||
func (client TelegramClient) downloadAudio(url string) (*[]byte, error) { | ||
clientHTTP := &http.Client{Timeout: 60 * 10 * time.Second} |
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.
@umputun I set big timeout, it is OK ?
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.
hard-coded timeout is something to avoid. But I can add it to opts and pass in latter on
app/proc/telegram.go
Outdated
return message, err | ||
} | ||
|
||
func (client TelegramClient) downloadAudio(url string) (*[]byte, 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.
no need in pointer return here, []byte is a slice backed by pointer already
app/proc/telegram.go
Outdated
return err | ||
audio := tb.Audio{ | ||
File: tb.FromReader(bytes.NewReader(*file)), | ||
FileName: "1.mp3", |
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.
1.mp3 ? probably should be the file name from the feed?
app/proc/telegram.go
Outdated
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != 200 { | ||
return 0, errors.New("status code != 200") |
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.
probably include the code in error will be useful
app/proc/telegram.go
Outdated
|
||
var message *tb.Message | ||
|
||
if contentLength < 50_000_000 { |
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.
moving 50_000_000 to a const and naming it logically, like maxTelegramFileSize
or smth like this will be nice
app/proc/telegram.go
Outdated
|
||
log.Printf("[DEBUG] start download audio: %s", url) | ||
|
||
file, err := ioutil.ReadAll(resp.Body) |
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 code reads file content to memory. For 50M file it is not too bad, but running multiple feeds at the same time can make it way too big. I don't even see why you need to read it to memory at all. Looks like all you need in your consumer is io.Reader, i.e. resp.Body can be passed to tb.FromReader directly
I still see a few minor things, but generally, it seems to be ok and working. I'll merge it and we could address things after that. |
@sgaynetdinov thx for your work on PR. I have it merged and fixed a bunch of small things after the merge. |
@umputun thanks for your review on my code |
old PR #14
close #13