-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
dm-thrad.broadast-media.example.ts
81 lines (71 loc) · 2 KB
/
dm-thrad.broadast-media.example.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* tslint:disable:no-console */
import { DirectThreadEntity, IgApiClient } from '../src';
import { promisify } from 'util';
import { readFile } from 'fs';
// use node v8 or later or the util npm package
const readFileAsync = promisify(readFile);
const ig = new IgApiClient();
async function login() {
// basic login-procedure
ig.state.generateDevice(process.env.IG_USERNAME);
ig.state.proxyUrl = process.env.IG_PROXY;
await ig.account.login(process.env.IG_USERNAME, process.env.IG_PASSWORD);
}
(async () => {
await login();
// get the first thread
const [thread] = await ig.feed.directInbox().records();
await sendPhoto(thread);
await sendVideo(thread);
await sendPhotoStory(thread);
await sendVideoStory(thread);
})();
/**
* Sends a regular photo to the thread
* @param thread
*/
async function sendPhoto(thread: DirectThreadEntity) {
const photo: Buffer = await readFileAsync('PATH_TO_PHOTO.jpg');
console.log(await thread.broadcastPhoto({
file: photo,
}));
}
/**
* Sends a regular video to the thread
* @param thread
*/
async function sendVideo(thread: DirectThreadEntity) {
const video: Buffer = await readFileAsync('PATH_TO_VIDEO.mp4');
console.log(await thread.broadcastVideo({
video,
// optional if you get a 202 transcode error
// delay in ms
transcodeDelay: 5 * 1000, // 5ms * 1000ms = 5s
}));
}
/**
* Sends a story with a video
* The story is replayable
* @param thread
*/
async function sendVideoStory(thread: DirectThreadEntity) {
const video = await readFileAsync('PATH_TO_VIDEO.mp4');
const cover = await readFileAsync('PATH_TO_COVER.jpg');
console.log(await thread.broadcastStory({
video,
coverImage: cover,
viewMode: 'replayable',
}));
}
/**
* Sends a story with a photo
* The story can only be seen once
* @param thread
*/
async function sendPhotoStory(thread: DirectThreadEntity) {
const photo = await readFileAsync('PATH_TO_PHOTO.jpg');
console.log(await thread.broadcastStory({
file: photo,
viewMode: 'once',
}));
}