summaryrefslogtreecommitdiff
path: root/luamods/telegram.lua
blob: 975740a08292d2e96f88566f44dbc7e280053a94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
local lines = {
    english = {
        ["not_configured"] = "{sender.alias_name}: This command is not set up properly. Try again later.",
        ["not_found"] = "{sender.alias_name}: Channel %s not found.",
        ["no_value"] = "{sender.alias_name}: Public Telegram channel must be specified.",
        ["message"] = "{sender.alias_name}: %s's last post: %s (posted %s ago)",
        ["no_messages"] = "{sender.alias_name}: This Telegram channel does not have any posts."
    },
    russian = {
        ["not_configured"] = "{sender.alias_name}: Команда не настроена. Попробуйте позже.",
        ["not_found"] = "{sender.alias_name}: Канал %s не найден.",
        ["no_value"] = "{sender.alias_name}: Нужно указать публичный Telegram канал.",
        ["message"] = "{sender.alias_name}: Последний пост %s: %s (опубликовано %s назад)",
        ["no_messages"] = "{sender.alias_name}: Этот Telegram канал не содержит каких-либо постов."
    },
}

return {
    name = "telegram",
    description = [[
Get the latest post from the specified public Telegram channel.

## Syntax

`!telegram [username]`

+ `[username]` - Valid public Telegram channel.

## Usage

+ `!telegram durov`
]],
    delay_sec = 5,
    options = {},
    subcommands = {},
    aliases = { "tg", "tgc", "тгк" },
    minimal_rights = "user",
    handle = function(request)
        local cfg = bot_config()
        if cfg.url.rssbridge == nil then
            return l10n_custom_formatted_line_request(request, lines, "not_configured", {})
        end

        if request.message == nil then
            return l10n_custom_formatted_line_request(request, lines, "no_value", {})
        end

        local url = str_format(cfg.url.rssbridge, { "TelegramBridge", request.message })
        local channel = rss_get(url)
        if channel == nil then
            return l10n_custom_formatted_line_request(request, lines, "not_found", { request.message })
        end

        local posts = channel.messages
        if #posts == 0 then
            return l10n_custom_formatted_line_request(request, lines, "no_messages", {})
        end

        local latest_post = posts[1]
        local post_time = "N/A"
        if latest_post.timestamp ~= 0 then
            post_time = time_humanize(time_current() - latest_post.timestamp)
        end

        return l10n_custom_formatted_line_request(request, lines, "message",
            { request.message, latest_post.title, post_time })
    end,
}