From 547bdec7afcbcf5b8ba7cb7f506a16c65259b30b Mon Sep 17 00:00:00 2001 From: topkecleon Date: Mon, 20 Jul 2015 19:29:40 -0400 Subject: [PATCH] added translation plugin --- loc/en.lua | 3 ++- plugins/gImages.lua | 1 + plugins/giphy.lua | 1 + plugins/pun.lua | 4 ++-- plugins/translate.lua | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 plugins/translate.lua diff --git a/loc/en.lua b/loc/en.lua index 084d69d..b5fc4e8 100644 --- a/loc/en.lua +++ b/loc/en.lua @@ -16,6 +16,7 @@ return { results = 'No results found.', argument = 'Invalid argument.', syntax = 'Invalid syntax.' - } + }, + translate = 'en' } diff --git a/plugins/gImages.lua b/plugins/gImages.lua index efb8a19..7892b0d 100644 --- a/plugins/gImages.lua +++ b/plugins/gImages.lua @@ -3,6 +3,7 @@ local PLUGIN = {} PLUGIN.doc = [[ /images This command performs a Google Images search for the given query. One random top result is returned. Safe search is enabled by default; use '/insfw' to get potentially NSFW results. + Want images sent directly to chat? Try @ImageBot. ]] PLUGIN.triggers = { diff --git a/plugins/giphy.lua b/plugins/giphy.lua index a1f0c14..413afab 100644 --- a/plugins/giphy.lua +++ b/plugins/giphy.lua @@ -3,6 +3,7 @@ local PLUGIN = {} PLUGIN.doc = [[ /giphy [query] Returns a random or search-resulted GIF from giphy.com. Results are limited to PG-13 by default; use '/gifnsfw' to get potentially NSFW results. + Want GIFs sent directly to chat? Try @ImageBot. ]] PLUGIN.triggers = { diff --git a/plugins/pun.lua b/plugins/pun.lua index 46d6c59..0741f46 100644 --- a/plugins/pun.lua +++ b/plugins/pun.lua @@ -101,7 +101,7 @@ PLUGIN.puns = { "Why are there no knock-knock jokes about America? Because freedom rings.", "When the cannibal showed up late to dinner, they gave him the cold shoulder.", "I should have been sad when my flashlight died, but I was delighted.", - "Why don't tennis players ever get marries? Love means nothing to them.", + "Why don't tennis players ever get married? Love means nothing to them.", "Pterodactyls can't be heard going to the bathroom because the P is silent.", "Mermaids make calls on their shell phones.", "What do you call an aardvark with three feet? A yaardvark.", @@ -125,7 +125,7 @@ PLUGIN.puns = { "Visitors in Cuba are always Havana good time.", "Why does electricity shock people? It doesn't know how to conduct itself.", "Lancelot had a scary dream about his horse. It was a knight mare.", - "A tribe of cannibals caught a missionary and ate him. Afterward, they all had violent food poisoning. This just goes to show that you can't keep a good man down.", + "A tribe of cannibals captured a missionary and ate him. Afterward, they all had violent food poisoning. This just goes to show that you can't keep a good man down.", "Heaven for gamblers is a paradise.", "Old wheels aren't thrown away, they're just retired.", "Horses are very stable animals.", diff --git a/plugins/translate.lua b/plugins/translate.lua new file mode 100644 index 0000000..02666ea --- /dev/null +++ b/plugins/translate.lua @@ -0,0 +1,40 @@ + -- Glanced at https://github.com/yagop/telegram-bot/blob/master/plugins/translate.lua + + local PLUGIN = {} + + PLUGIN.triggers = { + '^/translate' +} + +PLUGIN.doc = [[ + /translate [target lang] + Reply to a message to translate it to the default language. +]] + +PLUGIN.action = function(msg) + + if not msg.reply_to_message then + return send_msg(msg, PLUGIN.doc) + end + + local tl = config.locale.translate or 'en' + local input = get_input(msg.text) + if input then + tl = input + end + + local url = 'http://translate.google.com/translate_a/single?client=t&ie=UTF-8&oe=UTF-8&hl=en&dt=t&tl=' .. tl .. '&sl=auto&text=' .. URL.escape(msg.reply_to_message.text) + + local str, res = HTTP.request(url) + + if res ~= 200 then + return send_msg(msg, config.locale.errors.connection) + end + + local output = str:gmatch("%[%[%[\"(.*)\"")():gsub("\"(.*)", "") + + send_msg(msg.reply_to_message, output) + +end + +return PLUGIN