How would I get the message content and title from sent DMs using this API?
I can send it like this but I have no idea on how to parse the recieved Direct Messages from others?
function sendDM(user, text) {
// Either gain already gained session
var session = new Client.Session(device, cookiePath);
// Or if we cant, create a new session
var promise = Client.Session.create(device, cookiePath, 'username', 'password');
promise.then(function(sessionInstance) {
// Search for the User
Client.Account.searchForUser(session, user)
.then(function(accountInstance) {
var userId = accountInstance.id;
// Send a DM
Client.Thread.configureText(session, userId, text)
.then(function(threads) {
var thread = threads[0];
//thread.broadcastText(text);
console.log(thread.items) // -> see conversation
});
});
});
}
@dhillon2325 so here is a deal.
What you are talking about is real time chat between 2+ users. Instagram internally uses websocket connection, but this is not exploited yet.
Longterm solution would be to do some reverse engineering on instagram and find out how to work with socket connection. I was just too lazy to do so 馃槃.
Here is a solution. pooollling 馃槅. It is a litle bit evil solution, but will work for you just fine.
https://github.com/huttarichard/instagram-private-api/blob/master/proxy/controllers/v1/threads.js#L194
just setup proxy server (checkout readme how) and then use proxy-client
https://github.com/huttarichard/instagram-private-api/blob/master/proxy-client/v1/thread.js#L47
a subscribe or subscribeAll to get realtime events
@huttarichard I'm sorta new to NodeJS and wanted to make an Instagram client because i thought itd be a fun idea, could you please post an example of how to use subscribe or subscribeAll? :smile:
ok, going to do that.
var PrivateAPI = require('instagram-private-api');
var server = PrivateAPI.ProxyServer;
server.run({
port: 8080,
socketPort: 8888,
host: "0.0.0.0",
databaseDir: './databases',
cookiesDir: './cookies'
});
// this will run socket server 8888
var ClientProxy = PrivateAPI.ProxyClient.V1;
var server = new ClientProxy.Server('0.0.0.0', '8080', '8888');
var session = new ClientProxy.Session(server);
session.create('some instagram username', 'somepass')
.then(function(session) {
ClientProxy.Thread.subscribeAll(session, function(thread){
// this -> socket connection
console.log(thread, "thread change")
})
});
try this... if you get lucky it may works
This is the output I get when I run that.
Dhillons-iMac:InstagramClient dhillon$ node index.js
Socket port listening to 8888
Proxy APP started on 0.0.0.0:8080
Example app listening on port 3000!
Request Logs has been cleared (removed 0 docs)
127.0.0.1 - - [20/Jul/2016:15:43:38 +0000] "POST /v1/sessions HTTP/1.1" 200 561 "-" "-"
127.0.0.1 - - [20/Jul/2016:15:43:42 +0000] "POST /v1/sessions HTTP/1.1" 200 561 "-" "-"
127.0.0.1 - - [20/Jul/2016:15:43:43 +0000] "POST /v1/sessions HTTP/1.1" 200 561 "-" "-"
127.0.0.1 - - [20/Jul/2016:15:43:44 +0000] "POST /v1/sessions HTTP/1.1" 200 561 "-" "-"
127.0.0.1 - - [20/Jul/2016:15:43:50 +0000] "POST /v1/sessions HTTP/1.1" 200 561 "-" "-"
127.0.0.1 - - [20/Jul/2016:15:43:51 +0000] "POST /v1/sessions HTTP/1.1" 200 561 "-" "-"
(It also hosts an ExpressJS server on port 3000 for my NWJS application to communicate with the nodeJS server)
@dhillon2325 not really sure what is going on 馃槃. Log you send just tell me that you try to sing in serval times. I just try the example what I sent you put username and pwd in, and it is working :)
try to run script and do some inbox activity, write to someone or like something :)
@huttarichard start the server first?
the server is running, it can all in one script.
you can tell by this:
Socket port listening to 8888
Proxy APP started on 0.0.0.0:8080
session.create('some instagram username', 'somepass')
.then(function(session) {
console.log(session); // should log out session
ClientProxy.Thread.subscribeAll(session, function(thread){
console.log(thread, "thread change")
// this will show thread as soon as something is going to change in inbox, so you need to receive message or do some activity on your own.
})
});
It works! How do I get the message body and username from this JSON though?
inviter:
{ username: 'dhillon2325',
picture: 'http://scontent-lhr3-1.cdninstagram.com/t51.2885-19/s150x150/13725743_1648094448851005_393061985_a.jpg',
fullName: 'D搔ill謪占 馃樇馃槅 (15y)',
id: 3218755685,
isPrivate: false,
hasAnonymousProfilePicture: false,
isBusiness: false },
accounts: [ [Object] ],
items: [ [Object] ] },
items:
[ ThreadItem {
_session: [Object],
_params: [Object],
id: '27098876714266068818207929213648896' } ],
inviter:
Account {
_session:
Session {
server: [Object],
account: [Object],
_sessionKey: 'be51c23400ef4658b8fca2911c9ed1cf' },
_params:
{ username: 'dhillon2325',
picture: 'http://scontent-lhr3-1.cdninstagram.com/t51.2885-19/s150x150/13725743_1648094448851005_393061985_a.jpg',
fullName: 'D搔ill謪占 馃樇馃槅 (15y)',
id: 3218755685,
isPrivate: false,
hasAnonymousProfilePicture: false,
isBusiness: false },
id: 3218755685 },
accounts: [ Account { _session: [Object], _params: [Object], id: 3218755685 } ],
id: '340282366841710300949128128974947306896' }
@dhillon2325
checkout thread.items, every item has accountId (userId) and item type and in your case, text property
you need to first learn how to debug application in node.js how to use console.log() and inspect properties ;) it is necessary for any node.js development. Check this out it is a great start https://www.codeschool.com/learn/javascript
@huttarichard Is this not working any more ?
@sagardalal21 yes, not working!
@sagardalal21 @Epexa this is really old source and issue, I got rid of Proxy Server because it was nonsense... Make pooling your self it is not hard...