-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.js
226 lines (188 loc) · 4.19 KB
/
main.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Import Module
const { Telegraf } = require("telegraf");
const Twit = require("twit");
const Credentials = require("./config.json");
// Set Credential
const TwitterCredential = {
consumer_key: Credentials.twitter.consumer_key,
consumer_secret: Credentials.twitter.consumer_secret,
access_token: Credentials.twitter.access_token,
access_token_secret: Credentials.twitter.access_token_secret,
};
const TelegramCredential = Credentials.telegram.access_token;
// Instantiate Object
const T = new Twit(TwitterCredential);
const BOT = new Telegraf(TelegramCredential);
const LISTENER = {
status: false,
};
// Temp Array to Store the Step
let STEP = [];
// Global Tweet Object
const TWEET = {
link: ``,
id: ``,
content: ``,
page: ``,
screenName: ``,
};
// User check
function check(ctx) {
if (ctx.from.username !== Credentials.telegram.username) {
BOT.telegram.sendMessage(
ctx.chat.id,
"You're not permitted to use this BOT",
{}
);
return false;
}
return true;
}
// Reset command after it finished
function resetCommand() {
STEP = [];
TWEET.link = "";
TWEET.id = "";
TWEET.content = "";
TWEET.page = "";
TWEET.screenName = "";
LISTENER.status = false;
return;
}
// Follow twitter target
function follow(username) {
return new Promise((resolve, reject) => {
let params = {
screen_name: username,
follow: false,
};
T.post("friendships/create", params, function (err, data) {
if (err) {
return reject(err);
}
return resolve();
});
});
}
// Retweet tweet target
function retweet(id) {
return new Promise((resolve, reject) => {
let params = {
id: id,
};
T.post("statuses/retweet/:id", params, function (err, data) {
if (err) {
if (err.code != 327) {
reject(err);
return;
}
resolve();
return;
}
resolve();
return;
});
});
}
// Quote Retweet tweet target
function quoteRetweet(obj) {
return new Promise((resolve, reject) => {
let params = {
status: `${obj.content}
${obj.link}`,
};
let returnData = {
link: "",
tweetID: obj.id,
screenName: obj.screenName,
};
T.post("statuses/update", params, (err, data) => {
if (err) {
reject(err);
return;
}
returnData.link = `https://twitter.com/${data.user.screen_name}/status/${data.id_str}`;
return resolve(returnData);
});
});
}
// Like tweet target
function like(id) {
return new Promise((resolve, reject) => {
let params = {
id: id,
};
T.post("favorites/create", params, function (err, data) {
if (err) {
if (err.code !== 139) {
reject(err);
return;
}
resolve();
return;
}
resolve();
return;
});
});
}
async function twitterTask(tw, ctx) {
try {
let retweeted = await quoteRetweet(tw);
await retweet(retweeted.tweetID);
await like(retweeted.tweetID);
await follow(retweeted.screenName);
ctx.reply(retweeted.link);
ctx.reply(`--SUCCESSFULY [LIKE, FOLLOW, RETWEET]--
===[] @snowfluke []===`);
resetCommand();
} catch (err) {
console.log(err.message);
ctx.reply("--ERROR: SOMETHINGS WRONG--");
}
}
BOT.start(async (ctx) => {
let verified = check(ctx);
if (!verified) return;
BOT.telegram.sendMessage(ctx.chat.id, "Bot ready to serve", {});
return;
});
BOT.command("init", async (ctx) => {
let verified = check(ctx);
if (!verified) return;
ctx.reply(`===[] Created by. Awal Ariansyah []===
--INSERT TWEET URL--`);
LISTENER.status = true;
return;
});
BOT.on("text", async (ctx) => {
if (!LISTENER.status) return;
const text = ctx.update.message.text;
if (STEP.length == 0) {
try {
let tweetID = text.split("/status/")[1].split("?")[0];
if (!tweetID) return ctx.reply("--ERROR: INVALID URL--");
TWEET.link = text;
TWEET.page = text.split("/status/")[0];
TWEET.id = tweetID;
TWEET.screenName = TWEET.page.split(".com/")[1];
STEP.push(1);
ctx.reply("--INSERT CAPTION (280 CHARS)--");
return;
} catch (err) {
ctx.reply("--ERROR: INVALID URL--");
console.log(err.message);
}
return;
}
TWEET.content = text.slice(0, 280);
ctx.reply("PROCESSING...");
try {
await twitterTask(TWEET, ctx);
} catch (err) {
ctx.reply("--ERROR: SOMETHINGS WRONG--");
console.log(err.message);
}
});
BOT.launch();
console.log("BOT is running");