reddit-to-telegram 0.8.1 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -44
- data/lib/reddit_to_telegram/configuration.rb +18 -1
- data/lib/reddit_to_telegram/errors.rb +31 -2
- data/lib/reddit_to_telegram/post.rb +18 -11
- data/lib/reddit_to_telegram/reddit/fetch.rb +17 -2
- data/lib/reddit_to_telegram/reddit/output.rb +2 -1
- data/lib/reddit_to_telegram/telegram/post/gallery.rb +9 -9
- data/lib/reddit_to_telegram/telegram/post.rb +12 -25
- data/lib/reddit_to_telegram/telegram/prepare_request.rb +31 -20
- data/lib/reddit_to_telegram/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '05893e8e006a0f401dbcec8e91cfd46be3cf3b53bbc1c9e89ccd95620db3087c'
|
4
|
+
data.tar.gz: 62ef80526ab1051177eafcab26de219b6d196262024c473dc53813fcbac32365
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4319bf2ff9cbe27433514492093865c55ee65a77ef6d1eaae1a89376ea37cf0b16a998856b8f5c7ad21f7442d4ca2aca9f710d74fd40a7def9d8859f429d0dee
|
7
|
+
data.tar.gz: 58a917a293a78fdca894d5249114d26f19c79d27574ea02d3f412152aec321a45fb7ebc6672e63e46ebafcad5c3ed561867ace3420b15a1dd5b77d33d26ad392
|
data/README.md
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
Beware, this is remotely not production-ready, API will change, you'll see lots of bugs and it may break at any time.
|
8
8
|
Be sure to check for gem updates.
|
9
9
|
|
10
|
-
You can set this bot up absolutely for free [via AWS Lambda](https://gist.github.com/dersnek/851c32a6b45eab19f1c8748095b2a481#file-free-rtt-bot-in-aws-lambda).
|
10
|
+
You can set this bot up absolutely for free [via AWS Lambda](https://gist.github.com/dersnek/851c32a6b45eab19f1c8748095b2a481#file-free-rtt-bot-in-aws-lambda), no ruby knowledge required.
|
11
11
|
|
12
12
|
## Installation
|
13
13
|
In your `Gemfile` add:
|
@@ -19,33 +19,34 @@ Then run `bundle install`.
|
|
19
19
|
Or `gem install reddit-to-telegram`. Don't forget to `require` it.
|
20
20
|
|
21
21
|
## Prerequisites
|
22
|
+
- [Obtain a telegram bot token](https://core.telegram.org/bots/tutorial#obtain-your-bot-token)
|
22
23
|
- (Optionally) You'll need an [AWS account](https://aws.amazon.com/) to host a free SimpleDB (best available storage type, also default one). I also recommend hosting the bot on AWS lambda, since it would be free.
|
23
24
|
- (Optionally) [Create a Reddit app](https://www.reddit.com/prefs/apps), which would allow more requests to reddit
|
24
|
-
- [Obtain a telegram bot token](https://core.telegram.org/bots/tutorial#obtain-your-bot-token)
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
| RTT_AWS_ACCESS_KEY_ID | Your AWS access key ID. Needed for AWS SimpleDB storage | No |
|
30
|
-
| RTT_AWS_DOMAIN_NAME | Domain name to use for SimpleDB | No |
|
31
|
-
| RTT_AWS_REGION | AWS region your SimpleDB will be hosted on. Beware, it's not available in all regions. | No |
|
32
|
-
| RTT_AWS_SECRET_ACCESS_KEY | Your AWS access key ID. Needed for AWS SimpleDB storage. | No |
|
33
|
-
| RTT_GOOGLE_API_KEY | Your Google API key to translate posts via Google Translate. | No |
|
34
|
-
| RTT_MAX_STORED_POSTS | Number of posts to store in the database to avoid duplicates, default is 25. | No |
|
35
|
-
| RTT_REDDIT_CLIENT_ID | Reddit app credentials to access API. Reddit allows more authenticated requests. | No |
|
36
|
-
| RTT_REDDIT_CLIENT_SECRET | Reddit app credentials to access API. Reddit allows more authenticated requests. | No |
|
37
|
-
| RTT_STORE_TYPE | Choose between `aws_simple_db`, `memory` or `temp_file`. Default is `aws_simple_db`, so if you're not specifying your AWS credentials, you have to choose another store type. | No |
|
38
|
-
| RTT_TELEGRAM_BOT_TOKEN | The token you've received when you've created a telegram bot. | Yes |
|
39
|
-
| RTT_TELEGRAM_ERROR_CHANNEL_ID | Telegram channel to send errors to (without `@`, only errors from Telegram API responses would be sent for now) | No |
|
40
|
-
| RTT_TEMP_DIR | Directory to write temp files to without trailing `/` | No |
|
26
|
+
It is pretty congifurable, either dynamically or via ENV variables.
|
27
|
+
To assign variables dynamically, set them via `RedditToTelegram.config.variable_name= `, e.g. `RedditToTelegram.config.aws.access_key_id = ...`.
|
28
|
+
You can also create an ENV variable with a corresponding name. Here is the full configuration explained. Required options have a * next to them.
|
41
29
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
30
|
+
Config variable | Corresponding ENV Variable | Description |
|
31
|
+
| ----------------------- | ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
32
|
+
add_channel_handle | - | Add channel handle to Telegram posts. Accepted values: true or false. Default is false |
|
33
|
+
add_reddit_link | - | Add reddit link to Telegram posts. Accepted values: true or false. Default is false. |
|
34
|
+
logger | - | Which logger to use. You can pass your own ruby logger |
|
35
|
+
on_error | - | What to do when an error happens. Default is :log, but you can also :raise or :ignore |
|
36
|
+
send_errors_to_telegram | - | Also log errors to telegram (besides regular logging). Accepted values: true or false, default is false |
|
37
|
+
translate | - | Translate posts via Google Translate. Leave empty for no translation. More details below |
|
38
|
+
aws.access_key_id | RTT_AWS_ACCESS_KEY_ID | Your AWS access key ID. Needed for AWS SimpleDB storage |
|
39
|
+
aws.domain_name | RTT_AWS_DOMAIN_NAME | Domain name to use for SimpleDB |
|
40
|
+
aws.region | RTT_AWS_REGION | AWS region your SimpleDB will be hosted on. Beware, it's not available in all regions. |
|
41
|
+
aws.secret_access_key | RTT_AWS_SECRET_ACCESS_KEY | Your AWS access key ID. Needed for AWS SimpleDB storage. |
|
42
|
+
google.api_key | RTT_GOOGLE_API_KEY | Your Google API key to translate posts via Google Translate |
|
43
|
+
reddit.client_id | RTT_REDDIT_CLIENT_ID | Reddit app credentials to access API. Reddit allows more authenticated requests |
|
44
|
+
reddit.client_secret | RTT_REDDIT_CLIENT_SECRET | Reddit app credentials to access API. Reddit allows more authenticated requests |
|
45
|
+
store.max_stored_posts | RTT_MAX_STORED_POSTS | Number of posts to store in the database to avoid duplicates, default is 25 |
|
46
|
+
store.tmp_dir | RTT_TEMP_DIR | Directory to write temp files to without trailing `/` |
|
47
|
+
store.type | RTT_STORE_TYPE | Choose between `aws_simple_db`, `memory` or `temp_file`. Default is `aws_simple_db`, so if you're not specifying your AWS credentials, you have to choose another store type |
|
48
|
+
telegram.bot_token * | RTT_TELEGRAM_BOT_TOKEN | The token you've received when you've created a telegram bot |
|
49
|
+
telegram.error_channel_id | RTT_TELEGRAM_ERROR_CHANNEL_ID | Telegram channel to send errors to (without `@`, only errors from Telegram API responses would be sent for now) |
|
49
50
|
|
50
51
|
## Usage
|
51
52
|
|
@@ -53,30 +54,19 @@ Check out `lib/configuration` for full configuration.
|
|
53
54
|
2. To fetch latest hot post which hasn't been pushed yet:
|
54
55
|
```
|
55
56
|
RedditToTelegram.hot(
|
56
|
-
|
57
|
-
|
57
|
+
telegram_channel_id_1: :subreddit_name_1,
|
58
|
+
telegram_channel_id_2: :subreddit_name_2
|
58
59
|
)
|
59
60
|
```
|
60
|
-
|
61
|
+
You can push posts from one subreddit to one telegram channel, several-to-one, one-to-several, several-to-several, whatever you like.
|
62
|
+
You can also push one specific post:
|
61
63
|
```
|
62
|
-
RedditToTelegram.from_link("regular_link_to_post"
|
64
|
+
RedditToTelegram.from_link(telegram_channel_id: "regular_link_to_post")
|
63
65
|
```
|
64
66
|
Use `:telegram_channel_id` without the `@`.
|
65
67
|
|
66
|
-
###
|
68
|
+
### Translation
|
67
69
|
|
68
|
-
|
69
|
-
|
70
|
-
RedditToTelegram.
|
71
|
-
{ subreddit_name_1: :telegram_channel_id_1 },
|
72
|
-
translate: :ja
|
73
|
-
)
|
74
|
-
```
|
75
|
-
You can also specify if you want to add reddit link or telegram channel handle to the post text. By default they won't be added.
|
76
|
-
```
|
77
|
-
RedditToTelegram.hot(
|
78
|
-
{ subreddit_name_1: :telegram_channel_id_1 },
|
79
|
-
add_reddit_link: true,
|
80
|
-
add_channel_handle: true
|
81
|
-
)
|
82
|
-
```
|
70
|
+
Translation option is supported.
|
71
|
+
You will have to set `RedditToTelegram.config.translate` to the language key you want to translate to. You can find available languages in [Google Translate docs](https://cloud.google.com/translate/docs/languages).
|
72
|
+
You will also have to set up Google Translate API key assign it to `RedditToTelegram.config.google.api_key`.
|
@@ -5,7 +5,16 @@ require "logger"
|
|
5
5
|
module RedditToTelegram
|
6
6
|
module Configuration
|
7
7
|
class << self
|
8
|
-
attr_writer :
|
8
|
+
attr_writer :add_channel_handle, :add_reddit_link, :logger,
|
9
|
+
:on_error, :send_errors_to_telegram, :translate
|
10
|
+
|
11
|
+
def add_channel_handle
|
12
|
+
@add_channel_handle ||= false
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_reddit_link
|
16
|
+
@add_reddit_link ||= false
|
17
|
+
end
|
9
18
|
|
10
19
|
def logger
|
11
20
|
@logger ||= Logger.new($stdout).tap do |log|
|
@@ -16,6 +25,14 @@ module RedditToTelegram
|
|
16
25
|
def on_error
|
17
26
|
@on_error ||= :log
|
18
27
|
end
|
28
|
+
|
29
|
+
def send_errors_to_telegram
|
30
|
+
@send_errors_to_telegram ||= false
|
31
|
+
end
|
32
|
+
|
33
|
+
def translate
|
34
|
+
@translate ||= nil
|
35
|
+
end
|
19
36
|
end
|
20
37
|
|
21
38
|
class Store
|
@@ -3,19 +3,48 @@
|
|
3
3
|
module RedditToTelegram
|
4
4
|
class RedditToTelegramError < StandardError; end
|
5
5
|
|
6
|
+
class BadResponseFromTelegram < RedditToTelegramError; end
|
7
|
+
class FailedToFetchFromReddit < RedditToTelegramError; end
|
6
8
|
class InvalidStoreType < RedditToTelegramError; end
|
7
9
|
class MissingConfiguration < RedditToTelegramError; end
|
8
10
|
|
9
11
|
class Errors
|
10
12
|
class << self
|
11
13
|
def new(error, message = nil)
|
14
|
+
log_message = error.to_s
|
15
|
+
log_message += ": #{message}" unless message.nil?
|
16
|
+
|
12
17
|
if Configuration.on_error == :raise
|
13
18
|
raise(error.new(message))
|
14
19
|
elsif Configuration.on_error == :log
|
15
|
-
log_message = error.to_s
|
16
|
-
log_message += ": #{message}" unless message.nil?
|
17
20
|
Configuration.logger.error(log_message)
|
18
21
|
end
|
22
|
+
|
23
|
+
return unless Configuration.send_errors_to_telegram
|
24
|
+
|
25
|
+
push_error_to_telegram(log_message)
|
26
|
+
|
27
|
+
nil
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def push_error_to_telegram(message)
|
33
|
+
if Configuration.telegram.error_channel_id.to_s.empty?
|
34
|
+
Configuration.logger.warn("Telegram Error Channel ID is not set up, can't send errors there")
|
35
|
+
return
|
36
|
+
end
|
37
|
+
|
38
|
+
Telegram::Post.push(
|
39
|
+
{
|
40
|
+
type: :text,
|
41
|
+
text: message,
|
42
|
+
misc: { no_retry: true, disable_link_preview: true }
|
43
|
+
},
|
44
|
+
Configuration.telegram.error_channel_id
|
45
|
+
)
|
46
|
+
|
47
|
+
nil
|
19
48
|
end
|
20
49
|
end
|
21
50
|
end
|
@@ -3,29 +3,27 @@
|
|
3
3
|
module RedditToTelegram
|
4
4
|
class Post
|
5
5
|
class << self
|
6
|
-
def hot(sources
|
6
|
+
def hot(sources)
|
7
7
|
check_config
|
8
|
-
return if sources.empty?
|
9
|
-
|
10
8
|
Store.setup
|
11
9
|
|
12
|
-
sources.each do |
|
10
|
+
sources.each do |telegram_chat_id, subreddit|
|
13
11
|
res = Reddit::Fetch.hot(subreddit)
|
14
|
-
handle_res(res, subreddit, telegram_chat_id
|
12
|
+
handle_res(res, subreddit, telegram_chat_id)
|
15
13
|
end
|
16
14
|
end
|
17
15
|
|
18
|
-
def from_link(
|
16
|
+
def from_link(sources)
|
19
17
|
check_config
|
20
|
-
return
|
18
|
+
return unless check_from_link_sources(sources)
|
21
19
|
|
22
20
|
Configuration.store.type = :memory
|
23
21
|
Store.setup
|
24
22
|
|
25
|
-
res = Reddit::Fetch.post(
|
23
|
+
res = Reddit::Fetch.post(sources.values.first)
|
26
24
|
return unless res_ok?(res)
|
27
25
|
|
28
|
-
Telegram::Post.push(res,
|
26
|
+
Telegram::Post.push(res, sources.keys.first)
|
29
27
|
res
|
30
28
|
end
|
31
29
|
|
@@ -35,7 +33,7 @@ module RedditToTelegram
|
|
35
33
|
Errors.new(MissingConfiguration, "Missing Telegram bot token") if Configuration.telegram.bot_token.to_s.empty?
|
36
34
|
end
|
37
35
|
|
38
|
-
def handle_res(res, subreddit, telegram_chat_id
|
36
|
+
def handle_res(res, subreddit, telegram_chat_id)
|
39
37
|
return unless res_ok?(res)
|
40
38
|
|
41
39
|
post = find_new_post(subreddit, res)
|
@@ -45,7 +43,7 @@ module RedditToTelegram
|
|
45
43
|
return
|
46
44
|
end
|
47
45
|
|
48
|
-
res = Telegram::Post.push(post, telegram_chat_id
|
46
|
+
res = Telegram::Post.push(post, telegram_chat_id)
|
49
47
|
Store::Posts.add(subreddit, post[:id])
|
50
48
|
res
|
51
49
|
end
|
@@ -65,6 +63,15 @@ module RedditToTelegram
|
|
65
63
|
def find_new_post(subreddit, posts)
|
66
64
|
posts.find { |post| !Store::Posts.dup?(subreddit, post[:id]) }
|
67
65
|
end
|
66
|
+
|
67
|
+
def check_from_link_sources(sources)
|
68
|
+
if !sources.is_a?(Hash) || sources.keys.count != 1 || sources.values.count != 1
|
69
|
+
Errors.new(ArgumentError, "Check documentation on usage")
|
70
|
+
return false
|
71
|
+
end
|
72
|
+
|
73
|
+
true
|
74
|
+
end
|
68
75
|
end
|
69
76
|
end
|
70
77
|
end
|
@@ -55,12 +55,22 @@ module RedditToTelegram
|
|
55
55
|
handle_429(func_name, func_args)
|
56
56
|
when 200
|
57
57
|
Output.format_response(res)
|
58
|
+
else
|
59
|
+
Errors.new(FailedToFetchFromReddit, res.to_s)
|
58
60
|
end
|
59
61
|
end
|
60
62
|
|
61
63
|
def handle_401(func_name, func_args)
|
64
|
+
retries_left = func_args.last
|
65
|
+
func_args[func_args.length - 1] = retries_left - 1
|
66
|
+
|
62
67
|
Store::Reddit.token = Auth.token
|
63
|
-
|
68
|
+
|
69
|
+
if retries_left > 0
|
70
|
+
send(func_name, *func_args)
|
71
|
+
else
|
72
|
+
Errors.new(FailedToFetchFromReddit, "Failed to authenticate")
|
73
|
+
end
|
64
74
|
end
|
65
75
|
|
66
76
|
def handle_429(func_name, func_args)
|
@@ -68,7 +78,12 @@ module RedditToTelegram
|
|
68
78
|
|
69
79
|
sleep(10 / retries_left) if retries_left > 0
|
70
80
|
func_args[func_args.length - 1] = retries_left - 1
|
71
|
-
|
81
|
+
|
82
|
+
if retries_left > 0
|
83
|
+
send(func_name, *func_args)
|
84
|
+
else
|
85
|
+
Errors.new(FailedToFetchFromReddit, "Too many requests")
|
86
|
+
end
|
72
87
|
end
|
73
88
|
end
|
74
89
|
end
|
@@ -5,30 +5,30 @@ module RedditToTelegram
|
|
5
5
|
class Post
|
6
6
|
class Gallery
|
7
7
|
class << self
|
8
|
-
def push_remaining_gallery_data(post, channel, res
|
8
|
+
def push_remaining_gallery_data(post, channel, res)
|
9
9
|
if post[:additional_media]
|
10
|
-
push_remaining_gallery_images(post, channel
|
10
|
+
push_remaining_gallery_images(post, channel)
|
11
11
|
else
|
12
|
-
push_gallery_caption(post, channel, res
|
12
|
+
push_gallery_caption(post, channel, res)
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
private
|
17
17
|
|
18
|
-
def push_remaining_gallery_images(post, channel
|
18
|
+
def push_remaining_gallery_images(post, channel)
|
19
19
|
post[:media] = post[:additional_media].first(10)
|
20
20
|
remaining = post.delete(:additional_media).drop(10)
|
21
21
|
post[:additional_media] = remaining unless remaining.empty?
|
22
|
-
Post.push(post, channel
|
22
|
+
Post.push(post, channel)
|
23
23
|
end
|
24
24
|
|
25
|
-
def push_gallery_caption(post, channel, res
|
25
|
+
def push_gallery_caption(post, channel, res)
|
26
26
|
Telegram::Post.push(
|
27
27
|
{ type: :text,
|
28
28
|
id: post[:id],
|
29
|
-
text: post[:text]
|
30
|
-
|
31
|
-
|
29
|
+
text: post[:text],
|
30
|
+
misc: gallery_caption_opts(res) },
|
31
|
+
channel
|
32
32
|
)
|
33
33
|
end
|
34
34
|
|
@@ -19,20 +19,20 @@ module RedditToTelegram
|
|
19
19
|
}.freeze
|
20
20
|
|
21
21
|
class << self
|
22
|
-
def push(post, channel
|
22
|
+
def push(post, channel)
|
23
23
|
res = HTTParty.post(
|
24
24
|
"#{BASE_URI}#{Configuration.telegram.bot_token}/send#{METHOD_MAP[post[:type]]}",
|
25
|
-
**params(post, channel
|
25
|
+
**params(post, channel)
|
26
26
|
)
|
27
27
|
|
28
|
-
handle_response(post, channel, res
|
28
|
+
handle_response(post, channel, res)
|
29
29
|
end
|
30
30
|
|
31
31
|
private
|
32
32
|
|
33
|
-
def params(post, channel
|
33
|
+
def params(post, channel)
|
34
34
|
binary = post.dig(:misc)&.dig(:binary)
|
35
|
-
body = PrepareRequest.body(post, channel
|
35
|
+
body = PrepareRequest.body(post, channel)
|
36
36
|
|
37
37
|
pars = {
|
38
38
|
body: binary ? body : body.to_json,
|
@@ -42,29 +42,16 @@ module RedditToTelegram
|
|
42
42
|
pars
|
43
43
|
end
|
44
44
|
|
45
|
-
def handle_response(post, channel, res
|
46
|
-
|
47
|
-
Gallery.push_remaining_gallery_data(post, channel, res
|
48
|
-
Video.delete_file if post[:type] == :video && post.dig(:misc
|
45
|
+
def handle_response(post, channel, res)
|
46
|
+
log_error(post, channel, res) unless res["ok"]
|
47
|
+
Gallery.push_remaining_gallery_data(post, channel, res) if post[:type] == :gallery
|
48
|
+
Video.delete_file if post[:type] == :video && post.dig(:misc, :binary)
|
49
49
|
res
|
50
50
|
end
|
51
51
|
|
52
|
-
def
|
53
|
-
|
54
|
-
|
55
|
-
push(
|
56
|
-
{
|
57
|
-
type: :text,
|
58
|
-
id: post[:id],
|
59
|
-
text: "Channel: @#{channel}\n\nResponse: #{res}"
|
60
|
-
},
|
61
|
-
Configuration.telegram.error_channel_id,
|
62
|
-
opts.merge(
|
63
|
-
add_reddit_link: true,
|
64
|
-
disable_link_preview: true,
|
65
|
-
no_retry: true
|
66
|
-
)
|
67
|
-
)
|
52
|
+
def log_error(post, channel, res)
|
53
|
+
message = "\n\nChannel: #{channel}\n\nPost data: #{post}\n\nResponse: #{res}"
|
54
|
+
Errors.new(BadResponseFromTelegram, message)
|
68
55
|
end
|
69
56
|
end
|
70
57
|
end
|
@@ -4,54 +4,65 @@ module RedditToTelegram
|
|
4
4
|
module Telegram
|
5
5
|
class PrepareRequest
|
6
6
|
class << self
|
7
|
-
def body(post, chat_id
|
8
|
-
body = prepare_body(post, chat_id
|
9
|
-
body[:link_preview_options] = { is_disabled: true } if
|
10
|
-
body[:reply_parameters] = { message_id:
|
7
|
+
def body(post, chat_id)
|
8
|
+
body = prepare_body(post, chat_id)
|
9
|
+
body[:link_preview_options] = { is_disabled: true } if post.dig(:misc, :disable_link_preview)
|
10
|
+
body[:reply_parameters] = { message_id: post[:misc][:reply_to] } if post.dig(:misc, :reply_to)
|
11
11
|
body
|
12
12
|
end
|
13
13
|
|
14
14
|
private
|
15
15
|
|
16
|
-
def prepare_body(post, chat_id
|
16
|
+
def prepare_body(post, chat_id)
|
17
17
|
case post[:type]
|
18
18
|
when :image
|
19
|
-
{ chat_id: "@#{chat_id}", photo: post[:media], caption: prepare_text(post, chat_id
|
19
|
+
{ chat_id: "@#{chat_id}", photo: post[:media], caption: prepare_text(post, chat_id) }
|
20
20
|
when :gallery
|
21
|
-
{ chat_id: "@#{chat_id}", media: prepare_gallery_media(post), caption: prepare_text(post, chat_id
|
21
|
+
{ chat_id: "@#{chat_id}", media: prepare_gallery_media(post), caption: prepare_text(post, chat_id) }
|
22
22
|
when :gif
|
23
|
-
{ chat_id: "@#{chat_id}", animation: post[:media], caption: prepare_text(post, chat_id
|
23
|
+
{ chat_id: "@#{chat_id}", animation: post[:media], caption: prepare_text(post, chat_id) }
|
24
24
|
when :text
|
25
|
-
{ chat_id: "@#{chat_id}", text: prepare_text(post, chat_id
|
25
|
+
{ chat_id: "@#{chat_id}", text: prepare_text(post, chat_id) }
|
26
26
|
when :video
|
27
27
|
{
|
28
28
|
chat_id: "@#{chat_id}",
|
29
29
|
video: prepare_video(post),
|
30
30
|
height: post[:misc][:video_height],
|
31
31
|
width: post[:misc][:video_width],
|
32
|
-
caption: prepare_text(post, chat_id
|
32
|
+
caption: prepare_text(post, chat_id)
|
33
33
|
}
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
-
def prepare_text(post, chat_id
|
37
|
+
def prepare_text(post, chat_id)
|
38
38
|
text = post[:text]
|
39
39
|
|
40
|
-
text =
|
40
|
+
text = translate(text)
|
41
|
+
text = add_reddit_link(text, post)
|
42
|
+
add_channel_handle(text, chat_id)
|
43
|
+
end
|
41
44
|
|
42
|
-
|
43
|
-
|
44
|
-
text += "\n\nhttps://redd.it/#{id}"
|
45
|
-
end
|
45
|
+
def translate(text)
|
46
|
+
return text unless Configuration.translate
|
46
47
|
|
47
|
-
|
48
|
-
|
49
|
-
text += "@#{chat_id}"
|
50
|
-
end
|
48
|
+
Services::Translate.text(text, Configuration.translate)
|
49
|
+
end
|
51
50
|
|
51
|
+
def add_reddit_link(text, post)
|
52
|
+
return text unless Configuration.add_reddit_link
|
53
|
+
|
54
|
+
id = post[:id]&.split("_")&.dig(1)
|
55
|
+
text += "\n\nhttps://redd.it/#{id}" if id
|
52
56
|
text
|
53
57
|
end
|
54
58
|
|
59
|
+
def add_channel_handle(text, chat_id)
|
60
|
+
return text unless Configuration.add_channel_handle
|
61
|
+
|
62
|
+
text += Configuration.add_reddit_link ? "\n" : "\n\n"
|
63
|
+
text + "@#{chat_id}"
|
64
|
+
end
|
65
|
+
|
55
66
|
def prepare_gallery_media(post)
|
56
67
|
Array(post[:media]).map { |link| { type: "photo", media: link } }
|
57
68
|
end
|