Hi
In the API Readme, I see the following line:
Send direct messages or list direct messages in inbox
But I couldn't find example code or even the classes in the source
that I could use to send DMs.
Can someone help?
Thanks!
I've just thrown together this code by looking through IGDM (which uses this API):
const iClient = require("instagram-private-api").V1;
const device = new iClient.Device("myuser");
const storage = new iClient.CookieFileStorage(__dirname + "/myuser.json");
iClient.Session.create(device, storage, "myuser", "mypassword").then((session) => {
iClient.Thread.getById(session, "messageid").then((thread) => {
thread.broadcastText("Hello World!").then((threadWrapper) => {
console.log(threadWrapper);
});
});
});
It's not great by any means. This file seems to contain a lot of useful function calls.
Hi Jake,
Thanks for the example.
Can you also give me an example for getting the direct messages sent by the other person?
Thanks in advance
Sent from my Samsung Galaxy smartphone.
-------- Original message --------
From: Jake Walker notifications@github.com
Date: 19/09/2018 7:12 pm (GMT+03:00)
To: huttarichard/instagram-private-api instagram-private-api@noreply.github.com
Cc: Saziya Banu banu-saziya.consultant@gfi.in, Manual manual@noreply.github.com
Subject: Re: [huttarichard/instagram-private-api] Examples for sending direct messages? (#563)
I've just thrown together this code by looking through IGDMhttps://github.com/ifedapoolarewaju/igdm (which uses this API):
const iClient = require("instagram-private-api").V1;
const device = new iClient.Device("myuser");
const storage = new iClient.CookieFileStorage(__dirname + "/myuser.json");
iClient.Session.create(device, storage, "myuser", "mypassword").then((session) => {
iClient.Thread.getById(session, "messageid").then((thread) => {
thread.broadcastText("Hello World!").then((threadWrapper) => {
console.log(threadWrapper);
});
});
});
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHubhttps://github.com/huttarichard/instagram-private-api/issues/563#issuecomment-422863528, or mute the threadhttps://github.com/notifications/unsubscribe-auth/Aj6wwOQ3A529hSIqZQ2I4ly_N6kwS-aGks5ucmz7gaJpZM4WqGvX.
In the thread object you can do thread.items to get all of the chat message objects. I've done this so far:
thread.items.forEach((message => {
console.log(`${message._params.text} from ${message._params.userId}`);
});
Thanks much, Jake.
Can you tell me what is the 'messageid' parameter that you have mentioned in,
iClient.Thread.getById(session, "messageid").then((thread) => {
Thanks
Is there any way, I can get only the direct messages sent by the other person, not by me. I want to get only the last direct message of my friend.
Thanks in advance
You can get your 10 most recent chats by doing:
var chats = new Client.Feed.Inbox(session, 10);
chats.all().then((latestchats) => {
latestchats.forEach((chat) => {
console.log(chat.id);
});
});
And for your other question, each chat object has the ID of the user who has sent the message. I think it's something like message._params.userid you could then use an if to only select messages with your friend's ID.
Hope this helps.
After to create the session
Client.Thread.configureText(session, "InstagamID", "Hello world!").then(
threads => {
console.log(threads[0].params);
}
hello
i got this error below , i cannot figure out from where should i debug my code
Unhandled rejection NotFoundError: Page wasn't found!
i used the above example to test sending message to myself .
Regards
Ali Ahmed Nada
Most helpful comment
I've just thrown together this code by looking through IGDM (which uses this API):
It's not great by any means. This file seems to contain a lot of useful function calls.