From a19c444db9d5625a9281684ba01961526533d535 Mon Sep 17 00:00:00 2001 From: yago Date: Thu, 9 Oct 2014 15:09:06 +0200 Subject: [PATCH 1/8] EURUSD :money_with_wings: --- bot/bot.lua | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/bot/bot.lua b/bot/bot.lua index eccd4d6..5317ec2 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -157,6 +157,12 @@ function do_action(msg) return end + if string.starts(msg.text, '!eur') then + local eur = getEURUSD( ) + send_msg(receiver, eur, ok_cb, false) + return + end + if string.starts(msg.text, '!version') then text = 'James Bot '.. VERSION .. [[ Licencia GNU v2, código disponible en http://git.io/6jdjGg @@ -294,6 +300,16 @@ function get_9GAG() return link_image, title end +function getEURUSD( ) + b = http.request("http://webrates.truefx.com/rates/connect.html?c=EUR/USD&f=csv&s=n") + local rates = b:split(", ") + local symbol = rates[1] + local timestamp = rates[2] + local sell = rates[3]..rates[4] + local buy = rates[5]..rates[6] + return symbol..'\n'..'Buy: '..buy..'\n'..'Sell: '..sell +end + function download_to_file( url ) print("url a descargar: "..url) @@ -371,6 +387,13 @@ function split_by_space ( text ) return words end +function string:split(sep) + local sep, fields = sep or ":", {} + local pattern = string.format("([^%s]+)", sep) + self:gsub(pattern, function(c) fields[#fields+1] = c end) + return fields +end + function vardump(value, depth, key) local linePrefix = "" local spaces = "" From 06aa4ecfab2dc4220bcd49312b98657307807dec Mon Sep 17 00:00:00 2001 From: yago Date: Thu, 9 Oct 2014 15:11:21 +0200 Subject: [PATCH 2/8] updated tg --- tg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tg b/tg index 7a9764f..54f6743 160000 --- a/tg +++ b/tg @@ -1 +1 @@ -Subproject commit 7a9764f24ac745eb03a72e2ddb69c8574f6a8c2f +Subproject commit 54f6743db74461e6a3394f577671a273a37e01a1 From 33364b803e8b1db5a7b51306a29516f655169460 Mon Sep 17 00:00:00 2001 From: yago Date: Fri, 10 Oct 2014 15:25:01 +0200 Subject: [PATCH 3/8] updated version and readme --- README.md | 12 +++++++----- bot/bot.lua | 4 +++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8a3a35b..0855edd 100644 --- a/README.md +++ b/README.md @@ -5,19 +5,19 @@ Bot for telegram with [tg](https://github.com/vysheng/tg). ```bash sudo apt-get install lua-socket libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev fortune curl +git clone https://github.com/yagop/telegram-bot.git && cd telegram-bot git submodule update --init --recursive cd tg -./configure -make +./configure && make ../launch.sh ``` Command list ``` -!help : print this help -!ping : bot sends pong +!help : print command list +!ping : bot sends pong !sh (text) : send commands to bash (only privileged users) -!echo (text) : echo the msg +!echo (text) : echo the msg !version : version info !cpu : status (uname + top) !fwd : forward msg @@ -25,6 +25,8 @@ Command list !fortune : print a random adage !weather [city] : weather in that city (Madrid if not city) !9gag : send random image from 9gag +!rae (word): Spanish dictionary +!eur : EURUSD market value !img (text) : search image with Google API and sends it !uc3m : fortunes from Universidad Carlos III ``` diff --git a/bot/bot.lua b/bot/bot.lua index 8aa1acc..be07b71 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -2,7 +2,7 @@ http = require("socket.http") URL = require("socket.url") json = (loadfile "./bot/JSON.lua")() -VERSION = 'v0.4' +VERSION = 'v0.5' function on_msg_receive (msg) @@ -190,6 +190,8 @@ Puedes hacer una donación a la ONG que decidas y ayudar a otras personas.]] !fortune : print a random adage !weather [city] : weather in that city (Madrid if not city) !9gag : send random url image from 9gag +!rae (word): Spanish dictionary +!eur : EURUSD market value !img (text) : search image with Google API and sends it !uc3m : fortunes from Universidad Carlos III]] send_msg(receiver, text, ok_cb, false) From 2ed585b1fa7a0923246d5e800788c8614b8896cd Mon Sep 17 00:00:00 2001 From: Yago Date: Sat, 11 Oct 2014 11:53:53 +0200 Subject: [PATCH 4/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0855edd..ed5fca6 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ telegram-bot ============ Bot for telegram with [tg](https://github.com/vysheng/tg). - ```bash +# Tested on Ubuntu 14.04 sudo apt-get install lua-socket libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev fortune curl git clone https://github.com/yagop/telegram-bot.git && cd telegram-bot git submodule update --init --recursive From b2951061b717f48dd36274f94f3f5e38cf573141 Mon Sep 17 00:00:00 2001 From: Javier Honduvilla Coto Date: Sat, 11 Oct 2014 12:34:51 +0200 Subject: [PATCH 5/8] Update README.md --- README.md | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ed5fca6..e8c4543 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,22 @@ telegram-bot ============ -Bot for telegram with [tg](https://github.com/vysheng/tg). +A telegram bot using https://github.com/vysheng/tg. + +Installation +------------ + +* Requirements ```bash # Tested on Ubuntu 14.04 -sudo apt-get install lua-socket libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev fortune curl -git clone https://github.com/yagop/telegram-bot.git && cd telegram-bot -git submodule update --init --recursive -cd tg -./configure && make -../launch.sh +$ sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev fortune curl luarocks +``` + +* Clone the repo & compile the telegram library +```bash +$ git clone https://github.com/yagop/telegram-bot --recursive && cd telegram-bot/tg +$ ./configure && make +$ cd .. && ./launch.sh # Will ask you for a telegram enabled number phone & confirmation code. ``` Command list From 5d80cfa156466e7d9fa60abbe4daeae1ddc85852 Mon Sep 17 00:00:00 2001 From: Javier Honduvilla Coto Date: Sat, 11 Oct 2014 12:38:13 +0200 Subject: [PATCH 6/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e8c4543..fea2230 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Installation * Requirements ```bash -# Tested on Ubuntu 14.04 +# Tested on Ubuntu 14.04, for other OSs check out https://github.com/vysheng/tg#installation $ sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev fortune curl luarocks ``` From 24d48fc7cb6ec1923b2e950e29c71b7829ac5780 Mon Sep 17 00:00:00 2001 From: Yago Date: Sat, 11 Oct 2014 12:43:12 +0200 Subject: [PATCH 7/8] jpeg for image url --- bot/bot.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/bot.lua b/bot/bot.lua index be07b71..ae17f51 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -60,8 +60,8 @@ end function is_image_url(text) last = string.get_last_word(text) - extension = string.get_extension_from_filename(last) - if extension == 'jpg' or extension == 'png' then + extension = string.get_extension_from_filename(last) -- TODO: Change it please + if extension == 'jpg' or extension == 'png' or extension == 'jpeg' then return true end return false @@ -512,4 +512,4 @@ end function on_binlog_replay_end () started = 1 -end \ No newline at end of file +end From 89c25cc4266f790acdfb56aaef524a1b47efba5a Mon Sep 17 00:00:00 2001 From: Javier Date: Sat, 11 Oct 2014 13:54:43 +0200 Subject: [PATCH 8/8] Added yt thumbnails --- bot/bot.lua | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/bot/bot.lua b/bot/bot.lua index be07b71..be2b0c0 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -16,9 +16,11 @@ function on_msg_receive (msg) else if is_image_url(msg.text) then send_image_from_url (msg) + elseif is_youtube_url(msg.text) then + send_youtube_thumbnail(msg.text) else if is_file_url(msg.text) then - send_file_from_url (msg) + send_file_from_url(msg) end end end @@ -67,6 +69,20 @@ function is_image_url(text) return false end +function is_youtube_url(text) + -- http://stackoverflow.com/questions/19377262/regex-for-youtube-url + not_full_yt_url = string.match(text, "youtube.com/watch%?v=([A-Za-z0-9-]*)") == nil + not_short_yt_url = string.match(text, "youtu.be/([A-Za-z0-9-]*)") == nil + yt = full_yt_url or short_yt_url + return yt +end + +function send_youtube_thumbnail(msg) + yt_thumbnail = "http://img.youtube.com/vi/".. string.match(msg.text, "([A-Za-z0-9-]*)").."/hqdefault.jpg" + file = download_to_file(yt_thumbnail) + send_photo(get_receiver(msg), file, ok_cb, false) +end + function is_file_url(text) last = string.get_last_word(text) extension = string.get_extension_from_filename(last) @@ -106,14 +122,14 @@ function do_action(msg) meaning = getDulcinea(text) send_msg(receiver, meaning, ok_cb, false) end - - if string.starts(msg.text, '!9gag') then - url, title = get_9GAG() - file_path = download_to_file(url) - send_photo(receiver, file_path, ok_cb, false) - send_msg(receiver, title, ok_cb, false) - return - end + + if string.starts(msg.text, '!9gag') then + url, title = get_9GAG() + file_path = download_to_file(url) + send_photo(receiver, file_path, ok_cb, false) + send_msg(receiver, title, ok_cb, false) + return + end if string.starts(msg.text, '!fortune') then text = run_bash('fortune') @@ -512,4 +528,4 @@ end function on_binlog_replay_end () started = 1 -end \ No newline at end of file +end