2015-06-23 01:07:54 +02:00
|
|
|
# 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
|
2015-06-23 03:35:54 +02:00
|
|
|
# hubot (aggiungi[mi] / crea) (il mio / [tra]i contatto/i) [telefono] - chiedi ad hubot creare il tuo contatto
|
2015-06-23 01:07:54 +02:00
|
|
|
#
|
|
|
|
# Author:
|
|
|
|
# Michele Guerini Rocco (rnhmjoj)
|
|
|
|
#
|
|
|
|
|
|
|
|
net = require 'net'
|
|
|
|
|
|
|
|
module.exports = (robot) ->
|
|
|
|
|
|
|
|
# directly run a command in telegram-cli and return its output
|
|
|
|
# (a list of strings)
|
|
|
|
run_command = (command, callback) ->
|
2015-06-23 01:10:36 +02:00
|
|
|
client = net.connect robot.adapter.port, robot.adapter.host, ->
|
|
|
|
client.write command+'\n'
|
2015-06-23 01:07:54 +02:00
|
|
|
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 ///
|
2015-06-23 03:18:23 +02:00
|
|
|
User\ (\w+(\ ?\w+)*) # contact name
|
2015-06-23 01:07:54 +02:00
|
|
|
(\ @(\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
|
|
|
|
|
2015-06-23 03:35:54 +02:00
|
|
|
# add a user to the contact list
|
|
|
|
add_contact = (name, phone, callback) ->
|
|
|
|
robot.logger.info "create contact for user #{name}"
|
|
|
|
user_info 'user_info ' + name, (user) ->
|
|
|
|
[first, last...] = user.name.split ' '
|
|
|
|
phone = if phone? then phone else user.phone
|
|
|
|
if not phone?
|
|
|
|
return callback 'serve per forza il tuo numero'
|
|
|
|
run_command "add_contact #{phone} '#{first}' '#{last.join ' '}'", (reply) ->
|
|
|
|
robot.logger.info 'result: ' + reply
|
|
|
|
reply = reply[0].split ' '
|
|
|
|
if reply[0] is 'FAIL:'
|
|
|
|
return callback reply[1..]
|
|
|
|
callback()
|
|
|
|
|
2015-06-23 01:07:54 +02:00
|
|
|
# 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'
|
|
|
|
|
2015-06-23 03:35:54 +02:00
|
|
|
robot.respond /(aggiungi(mi)?|crea) ((trai? (i )?contatti)|(il mio contatto))( (\d+))?/, (res) ->
|
|
|
|
add_contact res.message.user.name, res.match[8], (err) ->
|
|
|
|
if err?
|
|
|
|
res.send "c'è un problema: " + err
|
|
|
|
else
|
|
|
|
res.send "fatto!"
|
|
|
|
|
2015-06-23 01:07:54 +02:00
|
|
|
robot.respond /(mi )?invit(i|ami) (in|nel gruppo) (.+)\??/, (res) ->
|
2015-06-23 01:34:09 +02:00
|
|
|
denied = ['BZBZ ADMIN-NOT-DETECTED', 'BZBZ IS-NOT-AUTHORIZED', 'BZBZ ACCESS-DENIED']
|
|
|
|
nope = ['ahahah NO', 'mai sentito questo?', 'invita anche me magari']
|
|
|
|
ok = ['provvedo subito', 'ok', 'certo', 'va bene']
|
|
|
|
done = ['fatto', 'ecco qui', 'ecco']
|
2015-06-23 01:07:54 +02:00
|
|
|
group_list (list) ->
|
|
|
|
chat = res.match[4]
|
2015-06-23 01:42:49 +02:00
|
|
|
user = res.message.user.name
|
2015-06-23 01:07:54 +02:00
|
|
|
if not chat in list
|
|
|
|
res.send res.random nope
|
|
|
|
return
|
2015-06-23 01:34:09 +02:00
|
|
|
if chat is process.env['ADMIN_ROOM_NAME']
|
|
|
|
res.send res.random denied
|
|
|
|
return
|
2015-06-23 01:07:54 +02:00
|
|
|
res.send res.random ok
|
2015-06-23 03:18:57 +02:00
|
|
|
|
2015-06-23 01:42:49 +02:00
|
|
|
run_command "chat_add_user #{chat} #{user}", (reply) ->
|
|
|
|
robot.logger.info "add user #{user} to chat #{chat}"
|
|
|
|
robot.logger.info 'result: ' + reply
|
2015-06-23 03:18:57 +02:00
|
|
|
if (reply.split ' ')[-1..][0] is 'USER_NOT_MUTUAL_CONTACT'
|
|
|
|
res.send 'aah, non posso: ho bisogno del tuo contatto'
|
|
|
|
else
|
|
|
|
res.send res.random done
|