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

49 lines
1.0 KiB
Lua
Raw Normal View History

local command = 'translate [text]'
local doc = [[```
/translate [text]
Translates input or the replied-to message into the bot's language.
```]]
2015-07-21 01:29:40 +02:00
local triggers = {
'^/translate[@'..bot.username..']*'
}
2015-07-21 01:29:40 +02:00
local action = function(msg)
2015-07-21 01:29:40 +02:00
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
2015-07-21 01:29:40 +02:00
end
2016-02-13 20:37:59 +01:00
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)
2015-07-21 01:29:40 +02:00
if res ~= 200 then
sendReply(msg, config.errors.connection)
return
2015-07-21 01:29:40 +02:00
end
2016-02-13 20:37:59 +01:00
local jdat = JSON.decode(str)
if jdat.code ~= 200 then
sendReply(msg, config.errors.connection)
return
end
local output = jdat.text[1]
2015-07-21 01:29:40 +02:00
sendReply(msg.reply_to_message or msg, output)
2015-07-21 01:29:40 +02:00
end
return {
action = action,
triggers = triggers,
doc = doc,
command = command
}