Commit
·
987f2db
1
Parent(s):
e04cf41
⚗️
Browse files
server.ts
CHANGED
|
@@ -5,6 +5,7 @@ import morgan from "koa-morgan";
|
|
| 5 |
import Router from "koa-router";
|
| 6 |
import { inspect } from "util";
|
| 7 |
import "dotenv/config";
|
|
|
|
| 8 |
|
| 9 |
const port = 7860;
|
| 10 |
|
|
@@ -14,16 +15,13 @@ app.use(morgan("dev"));
|
|
| 14 |
app.use(compression());
|
| 15 |
app.use(bodyParser());
|
| 16 |
|
| 17 |
-
const { API_KEY, API_SECRET, BEARER_TOKEN, CLIENT_ID, CLIENT_SECRET } = process.env;
|
| 18 |
|
| 19 |
const router = new Router();
|
| 20 |
|
| 21 |
app.use(router.routes());
|
| 22 |
app.use(router.allowedMethods());
|
| 23 |
|
| 24 |
-
//Import package
|
| 25 |
-
import { Client, auth } from "twitter-api-sdk";
|
| 26 |
-
|
| 27 |
// Initialize auth client first
|
| 28 |
const authClient = new auth.OAuth2User({
|
| 29 |
client_id: CLIENT_ID as string,
|
|
@@ -35,18 +33,55 @@ const authClient = new auth.OAuth2User({
|
|
| 35 |
// Pass auth credentials to the library client
|
| 36 |
const twitterClient = new Client(authClient);
|
| 37 |
|
| 38 |
-
const BOT_NAME = "
|
|
|
|
| 39 |
|
| 40 |
function debug(stuff: any) {
|
| 41 |
console.log(inspect(stuff, { depth: 20 }));
|
| 42 |
}
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
headers: { Authorization: `Bearer ${BEARER_TOKEN}` },
|
| 47 |
});
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
}
|
| 51 |
|
| 52 |
async function listen() {
|
|
|
|
| 5 |
import Router from "koa-router";
|
| 6 |
import { inspect } from "util";
|
| 7 |
import "dotenv/config";
|
| 8 |
+
import { Client, auth } from "twitter-api-sdk";
|
| 9 |
|
| 10 |
const port = 7860;
|
| 11 |
|
|
|
|
| 15 |
app.use(compression());
|
| 16 |
app.use(bodyParser());
|
| 17 |
|
| 18 |
+
const { API_KEY, API_SECRET, BEARER_TOKEN, CLIENT_ID, CLIENT_SECRET, COOKIE } = process.env;
|
| 19 |
|
| 20 |
const router = new Router();
|
| 21 |
|
| 22 |
app.use(router.routes());
|
| 23 |
app.use(router.allowedMethods());
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
// Initialize auth client first
|
| 26 |
const authClient = new auth.OAuth2User({
|
| 27 |
client_id: CLIENT_ID as string,
|
|
|
|
| 33 |
// Pass auth credentials to the library client
|
| 34 |
const twitterClient = new Client(authClient);
|
| 35 |
|
| 36 |
+
const BOT_NAME = "AltImageBot1";
|
| 37 |
+
const BOT_ID = "1612094318906417152";
|
| 38 |
|
| 39 |
function debug(stuff: any) {
|
| 40 |
console.log(inspect(stuff, { depth: 20 }));
|
| 41 |
}
|
| 42 |
|
| 43 |
+
interface TweetMentions {
|
| 44 |
+
data: Array<{ id: string; text: string }>;
|
| 45 |
+
meta: {
|
| 46 |
+
result_count: number;
|
| 47 |
+
newest_id: string;
|
| 48 |
+
oldest_id: string;
|
| 49 |
+
};
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
interface TweetLookups {
|
| 53 |
+
data: Array<{
|
| 54 |
+
id: string;
|
| 55 |
+
conversation_id: "string";
|
| 56 |
+
text: string;
|
| 57 |
+
}>;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
async function ff(url: string) {
|
| 61 |
+
const resp = await fetch(`https://api.twitter.com/2/${url}`, {
|
| 62 |
headers: { Authorization: `Bearer ${BEARER_TOKEN}` },
|
| 63 |
});
|
| 64 |
|
| 65 |
+
if (resp.status !== 200) {
|
| 66 |
+
throw new Error("invalid status: " + resp.status + "- " + (await resp.text()));
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
return await resp.json();
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
async function lookupTweets() {
|
| 73 |
+
const data: TweetMentions = await ff(`users/${BOT_ID}/mentions`);
|
| 74 |
+
|
| 75 |
+
const firstTweetId = data.data[0].id;
|
| 76 |
+
|
| 77 |
+
const conversation: TweetLookups = await ff(`tweets?ids=${firstTweetId}&tweet.fields=conversation_id`);
|
| 78 |
+
|
| 79 |
+
const conversation_id = conversation.data[0].conversation_id;
|
| 80 |
+
|
| 81 |
+
const tweets = await ff(
|
| 82 |
+
`tweets/search/recent?query=conversation_id:${conversation_id}&tweet.fields=in_reply_to_user_id,author_id,created_at,conversation_id&expansions=in_reply_to_user_id`
|
| 83 |
+
);
|
| 84 |
+
debug(tweets);
|
| 85 |
}
|
| 86 |
|
| 87 |
async function listen() {
|