Merge branch 'master' of github.com:yagop/telegram-bot

This commit is contained in:
yago 2014-10-11 15:35:28 +02:00
commit 0d8de480db
3 changed files with 74 additions and 24 deletions

View File

@ -1,20 +1,27 @@
telegram-bot telegram-bot
============ ============
Bot for telegram with [tg](https://github.com/vysheng/tg). A telegram bot using https://github.com/vysheng/tg.
Installation
------------
* Requirements
```bash ```bash
sudo apt-get install lua-socket libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev fortune curl # Tested on Ubuntu 14.04, for other OSs check out https://github.com/vysheng/tg#installation
git submodule update --init --recursive $ sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev fortune curl luarocks
cd tg ```
./configure
make * Clone the repo & compile the telegram library
../launch.sh ```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 Command list
``` ```
!help : print this help !help : print command list
!ping : bot sends pong !ping : bot sends pong
!sh (text) : send commands to bash (only privileged users) !sh (text) : send commands to bash (only privileged users)
!echo (text) : echo the msg !echo (text) : echo the msg
@ -25,6 +32,8 @@ Command list
!fortune : print a random adage !fortune : print a random adage
!weather [city] : weather in that city (Madrid if not city) !weather [city] : weather in that city (Madrid if not city)
!9gag : send random image from 9gag !9gag : send random image from 9gag
!rae (word): Spanish dictionary
!eur : EURUSD market value
!img (text) : search image with Google API and sends it !img (text) : search image with Google API and sends it
!uc3m : fortunes from Universidad Carlos III !uc3m : fortunes from Universidad Carlos III
``` ```

View File

@ -2,7 +2,7 @@ http = require("socket.http")
URL = require("socket.url") URL = require("socket.url")
json = (loadfile "./bot/JSON.lua")() json = (loadfile "./bot/JSON.lua")()
VERSION = 'v0.4' VERSION = 'v0.5'
function on_msg_receive (msg) function on_msg_receive (msg)
@ -16,9 +16,11 @@ function on_msg_receive (msg)
else else
if is_image_url(msg.text) then if is_image_url(msg.text) then
send_image_from_url (msg) send_image_from_url (msg)
elseif is_youtube_url(msg.text) then
send_youtube_thumbnail(msg.text)
else else
if is_file_url(msg.text) then if is_file_url(msg.text) then
send_file_from_url (msg) send_file_from_url(msg)
end end
end end
end end
@ -60,13 +62,27 @@ end
function is_image_url(text) function is_image_url(text)
last = string.get_last_word(text) last = string.get_last_word(text)
extension = string.get_extension_from_filename(last) extension = string.get_extension_from_filename(last) -- TODO: Change it please
if extension == 'jpg' or extension == 'png' then if extension == 'jpg' or extension == 'png' or extension == 'jpeg' then
return true return true
end end
return false return false
end 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) function is_file_url(text)
last = string.get_last_word(text) last = string.get_last_word(text)
extension = string.get_extension_from_filename(last) extension = string.get_extension_from_filename(last)
@ -162,6 +178,12 @@ function do_action(msg)
return return
end 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 if string.starts(msg.text, '!version') then
text = 'James Bot '.. VERSION .. [[ text = 'James Bot '.. VERSION .. [[
Licencia GNU v2, código disponible en http://git.io/6jdjGg Licencia GNU v2, código disponible en http://git.io/6jdjGg
@ -184,6 +206,8 @@ Puedes hacer una donación a la ONG que decidas y ayudar a otras personas.]]
!fortune : print a random adage !fortune : print a random adage
!weather [city] : weather in that city (Madrid if not city) !weather [city] : weather in that city (Madrid if not city)
!9gag : send random url image from 9gag !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 !img (text) : search image with Google API and sends it
!uc3m : fortunes from Universidad Carlos III]] !uc3m : fortunes from Universidad Carlos III]]
send_msg(receiver, text, ok_cb, false) send_msg(receiver, text, ok_cb, false)
@ -338,6 +362,16 @@ function get_9GAG()
return link_image, title return link_image, title
end 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 ) function download_to_file( url )
print("url a descargar: "..url) print("url a descargar: "..url)
@ -415,6 +449,13 @@ function split_by_space ( text )
return words return words
end 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) function vardump(value, depth, key)
local linePrefix = "" local linePrefix = ""
local spaces = "" local spaces = ""

2
tg

@ -1 +1 @@
Subproject commit 7a9764f24ac745eb03a72e2ddb69c8574f6a8c2f Subproject commit 54f6743db74461e6a3394f577671a273a37e01a1