This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot-2/plugins/translate.lua
2016-04-08 14:12:02 -07:00

51 lines
1.1 KiB
Lua
Executable File

local command = 'translate [text]'
local doc = [[```
/translate [text]
Translates input or the replied-to message into the bot's language.
```]]
local triggers = {
'^/translate[@'..bot.username..']*',
'^/tl[@'..bot.username..']*'
}
local action = function(msg)
local input = utilities.input(msg.text)
if not input then
if msg.reply_to_message and msg.reply_to_message.text then
input = msg.reply_to_message.text
else
sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return
end
end
local url = 'https://translate.yandex.net/api/v1.5/tr.json/translate?key=' .. config.yandex_key .. '&lang=' .. config.lang .. '&text=' .. URL.escape(input)
local str, res = HTTPS.request(url)
if res ~= 200 then
sendReply(msg, config.errors.connection)
return
end
local jdat = JSON.decode(str)
if jdat.code ~= 200 then
sendReply(msg, config.errors.connection)
return
end
local output = jdat.text[1]
output = 'Translation:\n"' .. output .. '"'
sendReply(msg.reply_to_message or msg, output)
end
return {
action = action,
triggers = triggers,
doc = doc,
command = command
}