2016-05-29 19:08:39 +02:00
|
|
|
local channel = {}
|
|
|
|
|
2016-06-07 06:31:34 +02:00
|
|
|
local bindings = require('otouto.bindings')
|
|
|
|
local utilities = require('otouto.utilities')
|
2016-05-29 19:08:39 +02:00
|
|
|
|
2016-07-25 11:03:35 +02:00
|
|
|
function channel:init(config)
|
2016-08-14 04:46:18 +02:00
|
|
|
channel.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('ch', true).table
|
|
|
|
channel.command = 'ch <channel> \\n <message>'
|
|
|
|
channel.doc = config.cmd_pat .. [[ch <channel>
|
2016-05-29 19:08:39 +02:00
|
|
|
<message>
|
|
|
|
|
|
|
|
Sends a message to a channel. Channel may be specified via ID or username. Messages are markdown-enabled. Users may only send messages to channels for which they are the owner or an administrator.
|
|
|
|
|
|
|
|
The following markdown syntax is supported:
|
|
|
|
*bold text*
|
|
|
|
_italic text_
|
|
|
|
[text](URL)
|
|
|
|
`inline fixed-width code`
|
2016-10-04 16:07:15 +02:00
|
|
|
```pre-formatted fixed-width code block```]]
|
2016-05-29 19:08:39 +02:00
|
|
|
end
|
|
|
|
|
2016-06-07 09:02:05 +02:00
|
|
|
function channel:action(msg, config)
|
2016-08-14 04:46:18 +02:00
|
|
|
local input = utilities.input(msg.text)
|
2016-10-04 16:07:15 +02:00
|
|
|
if not input then
|
|
|
|
utilities.send_reply(msg, channel.doc, 'html')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local chat_id = utilities.get_word(input, 1)
|
|
|
|
local chat, t = bindings.getChat{chat_id = chat_id}
|
|
|
|
if not chat then
|
|
|
|
utilities.send_reply(msg, 'Sorry, I was unable to retrieve information for that channel.\n`' .. t.description .. '`', true)
|
|
|
|
return
|
|
|
|
elseif chat.result.type ~= 'channel' then
|
|
|
|
utilities.send_reply(msg, 'Sorry, that group does not appear to be a channel.')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local admin_list, t = bindings.getChatAdministrators{ chat_id = chat_id }
|
|
|
|
if not admin_list then
|
|
|
|
utilities.send_reply(msg, 'Sorry, I was unable to retrieve a list of administrators for that channel.\n`' .. t.description .. '`', true)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local is_admin = false
|
|
|
|
for _, admin in ipairs(admin_list.result) do
|
|
|
|
if admin.user.id == msg.from.id then
|
|
|
|
is_admin = true
|
2016-08-14 04:46:18 +02:00
|
|
|
end
|
2016-10-04 16:07:15 +02:00
|
|
|
end
|
|
|
|
if not is_admin then
|
|
|
|
utilities.send_reply(msg, 'Sorry, you do not appear to be an administrator for that channel.')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local text = input:match('\n(.+)')
|
|
|
|
if not text then
|
|
|
|
utilities.send_reply(msg, 'Please enter a message to be sent on a new line. Markdown is supported.')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local success, result = utilities.send_message(chat_id, text, true, nil, true)
|
|
|
|
if success then
|
|
|
|
utilities.send_reply(msg, 'Your message has been sent!')
|
2016-08-14 04:46:18 +02:00
|
|
|
else
|
2016-10-04 16:07:15 +02:00
|
|
|
utilities.send_reply(msg, 'Sorry, I was unable to send your message.\n`' .. result.description .. '`', true)
|
2016-08-14 04:46:18 +02:00
|
|
|
end
|
2016-05-29 19:08:39 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return channel
|