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
topkecleon 03ce363c98 Switched from dkjson to cjson. lua-cjson is now a dependency.
administration.lua now saves its database every minute. This will be changed in a
future update.
2016-02-21 00:21:48 -05: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 = msg.text:input()
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 = latcyr(output)
sendReply(msg.reply_to_message or msg, output)
end
return {
action = action,
triggers = triggers,
doc = doc,
command = command
}