diff --git a/scripts/telegram.coffee b/scripts/telegram.coffee index c946d77..25c0eca 100644 --- a/scripts/telegram.coffee +++ b/scripts/telegram.coffee @@ -19,6 +19,9 @@ net = require 'net' module.exports = (robot) -> + # format a name a telegram-cli likes it + norm = (x) -> x.replace /\ /g, '_' + # directly run a command in telegram-cli and return its output # (a list of strings) run_command = (command, callback) -> @@ -31,8 +34,8 @@ module.exports = (robot) -> client.end() # return an object with the known user information - user_info = (command, callback) -> - run_command command, (data) -> + user_info = (name, callback) -> + run_command "user_info #{norm name}", (data) -> match = (data.join '\n').match /// User\ (\w+(\ ?\w+)*) # contact name (\ @(\w+))? # telegram username @@ -42,15 +45,40 @@ module.exports = (robot) -> user = name: match[1] username: match[4] - id: match[6] + id: 'user#'+match[6] phone: match[7] robot.logger.info 'parsed user data: ' + JSON.stringify user, null, 2 callback user + # get hubot telegram profile information + robot_info = (callback) -> + run_command 'get_self', (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: 'user#'+match[6] + phone: match[7] + robot.logger.info 'parsed robot data: ' + JSON.stringify user, null, 2 + callback user + + # return the list of active chat groups + chat_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 + # 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) -> + user_info name, (user) -> [first, last...] = user.name.split ' ' phone = phone or user.phone if not phone? @@ -71,14 +99,14 @@ module.exports = (robot) -> 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 '. ' + robot_info (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) -> + 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}"