1
0
mirror of https://github.com/fazo96/telecommander.git synced 2025-01-10 11:34:20 +01:00

can write to groups! reading is wip

This commit is contained in:
Enrico Fasoli 2015-08-30 14:40:01 +00:00
parent b9d66b3130
commit 8a04f1c44c

View File

@ -1,7 +1,7 @@
#!/usr/bin/env node #!/usr/bin/env node
// Prepare deps and fs var path = require('path')
var cfgDir = (process.env.HOME || process.env.USERPROFILE) + '/.config/telecommander/' var cfgDir = path.join(process.env.XDG_CONFIG_HOME || (path.join(process.env.HOME || process.env.USERPROFILE, '/.config/')), 'telecommander/')
process.env.LOGGER_FILE = cfgDir+'log' process.env.LOGGER_FILE = cfgDir+'log'
var os = require('os') var os = require('os')
@ -9,6 +9,20 @@ var fs = require('fs')
var moment = require('moment') var moment = require('moment')
var blessed = require('blessed') var blessed = require('blessed')
/* IF YOU FORK THE APP PLEASE CHANGE THE ID
* AND HASH IN THE APP OBJECT! THEY IDENTIFY
* THE APPLICATION CREATOR AND YOU CAN
* OBTAIN YOURS FROM http://my.telegram.org
*/
var app = {
id: '42419',
hash: '90a3c2cdbf9b391d9ed72c0639dc0786',
version: require('./package.json').version,
lang: 'en',
deviceModel: os.type(),
systemVersion: os.platform()+'/'+os.release()
}
try { fs.makeDirSync(cfgDir,'0770') } catch (e) { } try { fs.makeDirSync(cfgDir,'0770') } catch (e) { }
var getLogger = require('get-log') var getLogger = require('get-log')
@ -87,7 +101,7 @@ screen.append(msgBox[statusWindow]);
var contacts = { } var contacts = { }
// Groups hold all the data about groups // Groups hold all the data about groups
var groups = { } var groups = { }
// nameToUid is used to match a name to its user id (for the contact list) // nameToUid is used to match a name to its user/chat id (for the contact/chat list)
var nameToUid = { } var nameToUid = { }
var state = { } // keeps track of the telegram update state var state = { } // keeps track of the telegram update state
@ -206,7 +220,7 @@ function getMsgBox(chat,switchto){
// Is a real user: download messages and stuff // Is a real user: download messages and stuff
getMessages(uid,msgBox[chat]) getMessages(uid,msgBox[chat])
} }
} else log('Getting window','"'+chat+'"') } // else log('Getting window','"'+chat+'"')
if(switchto === true){ if(switchto === true){
switchToBox(chat) switchToBox(chat)
} }
@ -238,12 +252,19 @@ cmdline.key(['escape','C-c'], function(ch,key){
} else process.exit(0); } else process.exit(0);
}); });
function idToPeer(uid){
if(contacts[uid])
return new telegramLink.type.InputPeerContact({ props: { user_id: ''+uid } })
else
return new telegramLink.type.InputPeerChat({ props: { chat_id: ''+uid } })
}
// Send a message // Send a message
function sendMsg(uid,str){ function sendMsg(uid,str){
if(!connected){ if(!connected){
return log('Error: not ready to send messages') return log('Error: not ready to send messages')
} }
var peer = new telegramLink.type.InputPeerContact({ props: { user_id: ''+uid } }) var peer = idToPeer(uid)
var randid = parseInt(Math.random() * 1000000000) var randid = parseInt(Math.random() * 1000000000)
client.messages.sendMessage(peer,str,randid,function(sent){ client.messages.sendMessage(peer,str,randid,function(sent){
log('Send message:','"'+str+'"','to:',selectedWindow+':',sent.toPrintable()) log('Send message:','"'+str+'"','to:',selectedWindow+':',sent.toPrintable())
@ -258,16 +279,6 @@ function log(){
logger.info(msg) logger.info(msg)
} }
// Prepare data to feed to telegramLink
var app = {
id: '42419',
hash: '90a3c2cdbf9b391d9ed72c0639dc0786',
version: require('./package.json').version,
lang: 'en',
deviceModel: os.type(),
systemVersion: os.platform()+'/'+os.release()
}
// Connects to telegram // Connects to telegram
function connect(){ function connect(){
client = telegramLink.createClient(app, telegramLink.PROD_PRIMARY_DC, function(){ client = telegramLink.createClient(app, telegramLink.PROD_PRIMARY_DC, function(){
@ -314,11 +325,8 @@ function downloadData(){
}) })
client.messages.getDialogs(0,0,10,function(dialogs){ client.messages.getDialogs(0,0,10,function(dialogs){
dialogs.dialogs.list.forEach(function(item){ dialogs.chats.list.forEach(function(item){
if(item.peer.chat_id){ // is a group addGroup(item)
groups[item.peer.chat_id] = item
//log('Added group:',item.peer.chat_id)
}
}) })
}) })
@ -332,6 +340,15 @@ function downloadData(){
}) })
} }
function addGroup(group){
if(groups[group.id]) return;
if(group.left === true) return;
groups[group.id] = { id: group.id, title: group.title }
nameToUid[group.title] = group.id
chats.addItem(group.title)
log('Added group:',group.title)
}
// Updates the current state // Updates the current state
function updateState(newstate){ function updateState(newstate){
state.pts = newstate.pts state.pts = newstate.pts
@ -354,16 +371,25 @@ function onUpdate(upd){
function downloadUpdates(){ function downloadUpdates(){
client.updates.getDifference(state.pts,state.date,state.qts,function(res){ client.updates.getDifference(state.pts,state.date,state.qts,function(res){
log('Got Diff: ',res.toPrintable()) if(!res.instanceOf('api.type.updates.DifferenceEmpty')){
if(res.state){ //log('Got Diff: ',res.toPrintable())
updateState(res.state) if(res.state){
} updateState(res.state)
if(res.new_messages){ }
res.new_messages.list.forEach(function(msg){ if(res.new_messages){
if(!msg.message) return log('Empty message!',msg) res.new_messages.list.forEach(function(msg){
//log('Scheduling message: '+msg.message) if(!msg.message) return log('Empty message!',msg)
appendMsg(msg,undefined,false,true) //log('Scheduling message: '+msg.message)
}) appendMsg(msg,undefined,false,true)
})
}
if(res.chats){
res.chats.list.forEach(function(c){
if(!groups[c.id]){
groups[c.id] = { id: c.id, title: c.title }
}
})
}
} }
setTimeout(downloadUpdates,1000) setTimeout(downloadUpdates,1000)
}) })
@ -373,7 +399,7 @@ function getName(uid){
var u var u
if(!contacts[uid]) if(!contacts[uid])
if(groups[uid]) if(groups[uid])
return uid return groups[uid].title
else{ else{
log('Failed to find name for:',uid) log('Failed to find name for:',uid)
return undefined return undefined
@ -388,17 +414,21 @@ function getMessages(uid,box){
return log('Uh cant get messages cuz not connected.....') return log('Uh cant get messages cuz not connected.....')
} }
box.add('Downloading message history for '+getName(uid)) box.add('Downloading message history for '+getName(uid))
var peer = new telegramLink.type.InputPeerContact({ props: { user_id: uid } }) var peer = idToPeer(uid)
if(!peer) return box.add('Could not find peer')
client.messages.getHistory(peer,0,-1,20,function(res){ client.messages.getHistory(peer,0,-1,20,function(res){
//log(res.toPrintable()) //log(res.toPrintable())
log('Got history for: '+getName(peer.user_id)) log('Got history for: '+getName(peer.user_id))
if(!res.messages){
return box.add(res.toPrintable())
}
res.messages.list.sort(function(msg1,msg2){ res.messages.list.sort(function(msg1,msg2){
return msg1.date - msg2.date return msg1.date - msg2.date
}) })
if(res.messages.list.length === 0) if(res.messages.list.length === 0)
return appendToUserBox('No messages.',res) return appendToUserBox('No messages.',res)
res.messages.list.forEach(function(msg){ res.messages.list.forEach(function(msg){
if(!msg.message) return log('Empty message!',msg) if(!msg.message) return log('Empty message!',msg.toPrintable())
//log('Scheduling message: '+msg.message) //log('Scheduling message: '+msg.message)
appendMsg(msg) appendMsg(msg)
}) })
@ -432,12 +462,12 @@ function appendMsg(msg,toBoxId,bare,smartmode){
} else { } else {
if(msg.from_id === msg.to_id.user_id || msg.from_id != user.id){ if(msg.from_id === msg.to_id.user_id || msg.from_id != user.id){
param = getName(msg.from_id) param = getName(msg.from_id)
} else if(msg.to_id != user.id) { } else if(msg.to_id.user_id != undefined && msg.to_id.user_id != user.id) {
// don't forget dat .user_id! don't need it in from_id... // don't forget dat .user_id! don't need it in from_id...
param = getName(msg.to_id.user_id) param = getName(msg.to_id.user_id)
} else { } else if(msg.to_id.chat_id != undefined){
// Wtf ? maybe a group // Is a group
return log('Unknown message: from',msg.from_id,'to',msg.to_id) param = msg.to_id.chat_id
} }
if(smartmode && !bare){ if(smartmode && !bare){
// Smart mode doesn't append the message to the box if it doesn't exist // Smart mode doesn't append the message to the box if it doesn't exist