From 09d7d52d8945d2b59f7b08a82b6ebbaf11392b51 Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Mon, 22 Jun 2015 18:33:04 +0200 Subject: [PATCH 1/5] test send_raw --- scripts/misc.coffee | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/scripts/misc.coffee b/scripts/misc.coffee index df7205a..deac2c1 100644 --- a/scripts/misc.coffee +++ b/scripts/misc.coffee @@ -24,7 +24,9 @@ # Gabriele Della Torre # + module.exports = (robot) -> + send_raw = require('telegram-adapter').functions.send_raw.bind(robot.adapter) # Ringraziamenti ringr = ['prego :)', "non c'è di che", 'faccio solo il mio lavoro', 'no problemo amigo', 'non fate complimenti ;)'] complim = ['bravo', 'grande', 'grandissimo', 'bella' ] @@ -80,3 +82,6 @@ module.exports = (robot) -> robot.hear /(?:ehi|ciao|we|(?:bella(?: li)?)) (?:asjon|assa|assion(?:i|e))/i, (res) -> saluti = ['ciao', 'bella', 'è arrivato', 'eccolooo', 'dimmi', 'weeee', 'ehilà'] res.send res.random(saluti)+' '+res.message.user.name+'!' + + robot.respond /test nuof/i, (res) -> + send_raw 'get_self', (info) -> res.send 'mi dicono di essere ' + info From f6391e24c66a95ba162d7a5ee6a750e651b40e5f Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Mon, 22 Jun 2015 18:50:15 +0200 Subject: [PATCH 2/5] typo --- scripts/misc.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/misc.coffee b/scripts/misc.coffee index deac2c1..cac911d 100644 --- a/scripts/misc.coffee +++ b/scripts/misc.coffee @@ -26,7 +26,7 @@ module.exports = (robot) -> - send_raw = require('telegram-adapter').functions.send_raw.bind(robot.adapter) + send_raw = require('hubot-tg').functions.send_raw.bind(robot.adapter) # Ringraziamenti ringr = ['prego :)', "non c'è di che", 'faccio solo il mio lavoro', 'no problemo amigo', 'non fate complimenti ;)'] complim = ['bravo', 'grande', 'grandissimo', 'bella' ] From 23c9cb6bdd832174e32fff0356b225de1c39ebfa Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Mon, 22 Jun 2015 19:19:46 +0200 Subject: [PATCH 3/5] Remove send_raw --- scripts/misc.coffee | 4 ---- 1 file changed, 4 deletions(-) diff --git a/scripts/misc.coffee b/scripts/misc.coffee index cac911d..5ae85bb 100644 --- a/scripts/misc.coffee +++ b/scripts/misc.coffee @@ -26,7 +26,6 @@ module.exports = (robot) -> - send_raw = require('hubot-tg').functions.send_raw.bind(robot.adapter) # Ringraziamenti ringr = ['prego :)', "non c'è di che", 'faccio solo il mio lavoro', 'no problemo amigo', 'non fate complimenti ;)'] complim = ['bravo', 'grande', 'grandissimo', 'bella' ] @@ -82,6 +81,3 @@ module.exports = (robot) -> robot.hear /(?:ehi|ciao|we|(?:bella(?: li)?)) (?:asjon|assa|assion(?:i|e))/i, (res) -> saluti = ['ciao', 'bella', 'è arrivato', 'eccolooo', 'dimmi', 'weeee', 'ehilà'] res.send res.random(saluti)+' '+res.message.user.name+'!' - - robot.respond /test nuof/i, (res) -> - send_raw 'get_self', (info) -> res.send 'mi dicono di essere ' + info From d66080f5314c918423fc90c935384e2a6604d1f4 Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Tue, 23 Jun 2015 01:07:54 +0200 Subject: [PATCH 4/5] New telegram module Add new hub commands to * ask asjon about your contact data * ask asjon who he is * ask asjon which groups he has joined * ask asjon to invite you in a group --- scripts/telegram.coffee | 89 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 scripts/telegram.coffee diff --git a/scripts/telegram.coffee b/scripts/telegram.coffee new file mode 100644 index 0000000..f59e92c --- /dev/null +++ b/scripts/telegram.coffee @@ -0,0 +1,89 @@ +# Description: +# funzioni speciali per telegram-cli +# +# Configuration: +# Uses hubot-tg enviroment variables +# +# Commands +# hubot chi sei - chiedi a hubot di identificarsi +# hubot chi sono / cosa sai di me - mostra il tuo contatto nel registro di hubot +# hubot in che gruppi sei / dove scrivi - chiedi a hubot dove chatta +# hubot (mi inviti / invitami) (nel gruppo / in) - chiedi ad hubot di invitardi in un gruppo +# +# Author: +# Michele Guerini Rocco (rnhmjoj) +# + +net = require 'net' + +port = process.env['HUBOT_TG_PORT'] || 1123 +host = process.env['HUBOT_TG_HOST'] || 'localhost' + +module.exports = (robot) -> + + # directly run a command in telegram-cli and return its output + # (a list of strings) + run_command = (command, callback) -> + client = net.connect port, host, -> client.write command+'\n' + client.setEncoding 'utf8' + client.on 'data', (reply) -> + if callback? + callback (reply.split '\n')[1..-3] + client.end() + + # return an object with the known user information + user_info = (command, callback) -> + run_command command, (data) -> + match = (data.join '\n').match /// + User\ (\w+(\ ?\w+)+) # contact name + (\ @(\w+))? # telegram username + (\ \(\#(\d+)\))? # telegram id + [\s\S]+phone:\ (\d+)? # phone number + /// + user = + name: match[1] + username: match[4] + id: match[6] + phone: match[7] + robot.logger.info 'parsed user data: ' + JSON.stringify user, null, 2 + callback user + + # return the list of active chat groups + group_list = (callback) -> + run_command 'dialog_list', (list) -> + chats = list.filter (item) -> not item.lastIndexOf 'Chat', 0 + chats = chats.map (chat) -> (chat.match /Chat (.+):/)[1] + robot.logger.info 'parsed bot chats list: ' + JSON.stringify chats, null, 2 + callback chats + + robot.respond /chi sei/, (res) -> + user_info 'get_self', (user) -> + username = if user.username? then " noto anche come #{user.username}" else '' + phone = if user.phone? then " chiamatemi al #{user.phone}. " else '. ' + id = "il mio id di telegram è #{user.id}" + res.send "sono #{user.name}#{username},#{phone}#{id}" + + robot.respond /(cosa sai di me|chi sono)/i, (res) -> + user_info 'user_info ' + res.message.user.name, (user) -> + username = if user.username? then " ma ti chiamano anche #{user.username}" else '' + phone = if user.phone? then " so il tuo numero: #{user.phone} e " else ' ' + id = "il tuo id di telegram è #{user.id}" + res.send "so che sei #{user.name}#{username},#{phone}#{id}" + + robot.respond /(in che gruppi sei| dove scrivi)/, (res) -> + intro = ['scrivo in questi gruppi', 'chatto qui', 'sono attivo in'] + group_list (list) -> + res.send (res.random intro) + ':\n' + (list.map (i) -> '* '+i).join '\n' + + robot.respond /(mi )?invit(i|ami) (in|nel gruppo) (.+)\??/, (res) -> + nope = ['dove?', 'mai sentito questo?', 'invita anche me magari'] + ok = ['provvedo', 'ok', 'certo', 'va bene'] + done = ['fatto', 'ecco qui', 'ecco'] + group_list (list) -> + chat = res.match[4] + if not chat in list + res.send res.random nope + return + res.send res.random ok + run_command "chat_add_user #{chat} #{res.message.user.name}" + res.send res.random done From 1fadad01de9bbf70b75a4ab3b5f4dc935d65458f Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Tue, 23 Jun 2015 01:10:36 +0200 Subject: [PATCH 5/5] Use variables defined by the adapter --- scripts/telegram.coffee | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/telegram.coffee b/scripts/telegram.coffee index f59e92c..78ea317 100644 --- a/scripts/telegram.coffee +++ b/scripts/telegram.coffee @@ -16,15 +16,15 @@ net = require 'net' -port = process.env['HUBOT_TG_PORT'] || 1123 -host = process.env['HUBOT_TG_HOST'] || 'localhost' - module.exports = (robot) -> + + console.log robot.host # directly run a command in telegram-cli and return its output # (a list of strings) run_command = (command, callback) -> - client = net.connect port, host, -> client.write command+'\n' + client = net.connect robot.adapter.port, robot.adapter.host, -> + client.write command+'\n' client.setEncoding 'utf8' client.on 'data', (reply) -> if callback?