sociova 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/client.d.ts ADDED
@@ -0,0 +1,91 @@
1
+
2
+ export type InstaClientConstructorOptions = {
3
+ auth: string;
4
+ };
5
+
6
+ export type GetUserPayload = {
7
+ fields: string | string[];
8
+ };
9
+
10
+ export type GetUserResponse = Record<string, any>;
11
+
12
+ export type SendMessagePayload = {
13
+ recepientId: string | number;
14
+ message: string;
15
+ };
16
+
17
+ export type SendMessageResponse = Record<string, any>;
18
+
19
+ export type AttachmentItem =
20
+ | {
21
+ type: string;
22
+ payload?: Record<string, any>;
23
+ [key: string]: any;
24
+ }
25
+ | Record<string, any>;
26
+
27
+ export type SendAttachmentPayload = {
28
+ recepientId: string | number;
29
+ attachments: AttachmentItem[];
30
+ };
31
+
32
+ export type SendAttachmentResponse = Record<string, any>;
33
+
34
+ export type SendStickerPayload = {
35
+ recepientId: string | number;
36
+ sticker: string;
37
+ };
38
+
39
+ export type SendStickerResponse = Record<string, any>;
40
+
41
+ export type SendReactionPayload = {
42
+ recepientId: string | number;
43
+ messageId: string;
44
+ reaction?: string;
45
+ action?: "react" | "unreact";
46
+ };
47
+
48
+ export type SendReactionResponse = Record<string, any>;
49
+
50
+ export type WebUrlButton = {
51
+ type: "web_url";
52
+ url: string;
53
+ title: string;
54
+ };
55
+
56
+ export type PostbackButton = {
57
+ type: "postback";
58
+ payload: string;
59
+ title: string;
60
+ };
61
+
62
+ export type ButtonTemplateButton = WebUrlButton | PostbackButton;
63
+
64
+ export type SendButtonTemplatePayload = {
65
+
66
+ recipientId: string | number;
67
+ text: string;
68
+ buttons: ButtonTemplateButton[];
69
+ };
70
+
71
+ export type SendButtonTemplateResponse = Record<string, any>;
72
+
73
+ export declare class InstaClient {
74
+ Config: {
75
+ auth: string;
76
+ };
77
+
78
+ constructor(options: InstaClientConstructorOptions);
79
+
80
+ getUser(payload: GetUserPayload): Promise<GetUserResponse>;
81
+
82
+ sendMessage(payload: SendMessagePayload): Promise<SendMessageResponse>;
83
+
84
+ sendAttachment(payload: SendAttachmentPayload): Promise<SendAttachmentResponse>;
85
+
86
+ sendSticker(payload: SendStickerPayload): Promise<SendStickerResponse>;
87
+
88
+ sendReaction(payload: SendReactionPayload): Promise<SendReactionResponse>;
89
+
90
+ sendButtonTemplate(payload: SendButtonTemplatePayload): Promise<SendButtonTemplateResponse>;
91
+ }
package/client.js ADDED
@@ -0,0 +1,138 @@
1
+ import { AUTHENTICATED_USER, INSTAGRAM_BASE_URL } from "./route";
2
+
3
+ export class InstaClient{
4
+ constructor({ auth}){
5
+ this.Config = {auth}
6
+ }
7
+ async getUser(payload) {
8
+ let url = new URL(`${INSTAGRAM_BASE_URL}${AUTHENTICATED_USER}`);
9
+
10
+ const fields = Array.isArray(payload.fields)
11
+ ? payload.fields.join(",")
12
+ : payload.fields;
13
+
14
+ if(fields){
15
+ url.searchParams.set("fields", fields);
16
+ url.searchParams.set("access_token", this.Config.auth)
17
+ }
18
+
19
+ const res = await fetch(url.href)
20
+ return await res.json()
21
+ }
22
+ async sendMessage(payload) {
23
+ let url = `${INSTAGRAM_BASE_URL}${AUTHENTICATED_USER}/messages`
24
+ const res = await fetch(url, {method:"POST",
25
+ headers:{
26
+ Authorization:`Bearer ${this.Config.auth}`,
27
+ "Content-Type": "application/json"
28
+ }, body:JSON.stringify({
29
+ "recipient":{
30
+ "id":payload.recepientId
31
+ },
32
+ "message":{
33
+ "text":payload.message
34
+ }
35
+ })
36
+
37
+ })
38
+
39
+ return await res.json()
40
+ }
41
+ async sendAttachment(payload) {
42
+ let url = `${INSTAGRAM_BASE_URL}${AUTHENTICATED_USER}/messages`
43
+ const res = await fetch(url, {method:"POST",
44
+ headers:{
45
+ Authorization:`Bearer ${this.Config.auth}`,
46
+ "Content-Type": "application/json"
47
+ }, body:JSON.stringify({
48
+ "recipient":{
49
+ "id":payload.recepientId
50
+ },
51
+ "message":{
52
+ "attachments":payload.attachments
53
+ }
54
+ })
55
+
56
+ })
57
+
58
+ return await res.json()
59
+ }
60
+ async sendSticker(payload) {
61
+ let url = `${INSTAGRAM_BASE_URL}${AUTHENTICATED_USER}/messages`
62
+ const res = await fetch(url, {method:"POST",
63
+ headers:{
64
+ Authorization:`Bearer ${this.Config.auth}`,
65
+ "Content-Type": "application/json"
66
+ }, body:JSON.stringify({
67
+ "recipient":{
68
+ "id":payload.recepientId
69
+ },
70
+ message: {
71
+ attachment: {
72
+ type: payload.sticker
73
+ }
74
+ }
75
+ })
76
+
77
+ })
78
+
79
+ return await res.json()
80
+ }
81
+ async sendReaction(payload) {
82
+
83
+ const url = `${INSTAGRAM_BASE_URL}${AUTHENTICATED_USER}/messages`;
84
+
85
+ const body = {
86
+ recipient: { id: String(payload.recepientId) },
87
+ sender_action: payload.action ?? "react", // "react" or "unreact"
88
+ payload: {
89
+ message_id: String(payload.messageId),
90
+ },
91
+ };
92
+
93
+ if ((payload.action ?? "react") !== "unreact") {
94
+ body.payload.reaction = payload.reaction;
95
+ }
96
+
97
+ const res = await fetch(url, {
98
+ method: "POST",
99
+ headers: {
100
+ Authorization: `Bearer ${this.Config.auth}`,
101
+ "Content-Type": "application/json",
102
+ },
103
+ body: JSON.stringify(body),
104
+ });
105
+
106
+ return await res.json();
107
+ }
108
+ async sendButtonTemplate(payload) {
109
+
110
+
111
+ const url = `${INSTAGRAM_BASE_URL}/${AUTHENTICATED_USER}/messages`;
112
+
113
+ const res = await fetch(url, {
114
+ method: "POST",
115
+ headers: {
116
+ Authorization: `Bearer ${this.Config.auth}`,
117
+ "Content-Type": "application/json",
118
+ },
119
+ body: JSON.stringify({
120
+ recipient: {
121
+ id: payload.recipientId
122
+ },
123
+ message: {
124
+ attachment: {
125
+ type: "template",
126
+ payload: {
127
+ template_type: "button",
128
+ text: payload.text,
129
+ buttons: payload.buttons
130
+ }
131
+ }
132
+ }
133
+ })
134
+ });
135
+
136
+ return await res.json();
137
+ }
138
+ }
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./client";
package/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from "./client"
package/package.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "sociova",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [],
10
+ "author": "",
11
+ "license": "ISC",
12
+ "type": "module",
13
+ "dependencies": {
14
+ "dotenv": "^17.3.1"
15
+ }
16
+ }
package/route.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export const AUTHENTICATED_USER = "/me"
2
+ export const INSTAGRAM_BASE_URL = "https://graph.instagram.com/v25.0"
package/route.js ADDED
@@ -0,0 +1,2 @@
1
+ export const AUTHENTICATED_USER = "/me"
2
+ export const INSTAGRAM_BASE_URL = "https://graph.instagram.com/v25.0"