Merge remote-tracking branch 'upstream/master' into nontext

* upstream/master:
  When text exceds 4096 chars, sends in multiple msgs
  resolves #18
  Update README.md
This commit is contained in:
Marcel van der Boom 2014-12-14 16:45:11 +01:00
commit 7173c9831d
2 changed files with 22 additions and 4 deletions

View File

@ -37,7 +37,7 @@ Installation
```bash ```bash
# Tested on Ubuntu 14.04, for other OSs check out https://github.com/vysheng/tg#installation # 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 $ sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev unzip git
$ cd /tmp $ cd /tmp
$ wget http://luarocks.org/releases/luarocks-2.2.0.tar.gz $ wget http://luarocks.org/releases/luarocks-2.2.0.tar.gz
$ tar -xzvf luarocks-2.2.0.tar.gz $ tar -xzvf luarocks-2.2.0.tar.gz
@ -50,7 +50,7 @@ $ sudo luarocks install luasocket
```bash ```bash
# After those dependencies, lets install the bot # After those dependencies, lets install the bot
$ cd $HOME $ cd $HOME
$ git clone git@github.com:yagop/telegram-bot.git --recursive $ git clone https://github.com/yagop/telegram-bot.git --recursive
$ cd telegram-bot/tg $ cd telegram-bot/tg
$ ./configure && make $ ./configure && make
$ cd .. && ./launch.sh # Will ask you for a phone number & confirmation code. $ cd .. && ./launch.sh # Will ask you for a phone number & confirmation code.

View File

@ -71,7 +71,7 @@
result = desc.run(msg, matches) result = desc.run(msg, matches)
print(" sending", result) print(" sending", result)
if (result) then if (result) then
send_msg(receiver, result, ok_cb, false) _send_msg(receiver, result)
return return
end end
end end
@ -79,6 +79,24 @@
end end
end end
-- If text is longer than 4096 chars, send multiple msg.
-- https://core.telegram.org/method/messages.sendMessage
function _send_msg( destination, text)
local msg_text_max = 4096
local len = string.len(text)
local iterations = math.ceil(len / msg_text_max)
for i = 1, iterations, 1 do
print ("iteracion: "..i)
local inital_c = i * msg_text_max - msg_text_max
local final_c = i * msg_text_max
-- dont worry about if text length < msg_text_max
local text_msg = string.sub(text,inital_c,final_c)
send_msg(destination, text_msg, ok_cb, false)
end
end
function load_config() function load_config()
local f = assert(io.open('./bot/config.json', "r")) local f = assert(io.open('./bot/config.json', "r"))
local c = f:read "*a" local c = f:read "*a"