|
import Koa from "koa"; |
|
import bodyParser from "koa-bodyparser"; |
|
import compression from "koa-compress"; |
|
import morgan from "koa-morgan"; |
|
import Router from "koa-router"; |
|
import { inspect } from "util"; |
|
import "dotenv/config"; |
|
|
|
const port = 7860; |
|
|
|
const app = new Koa(); |
|
|
|
app.use(morgan("dev")); |
|
app.use(compression()); |
|
app.use(bodyParser()); |
|
|
|
const { API_KEY, API_SECRET, BEARER_TOKEN, CLIENT_ID, CLIENT_SECRET } = process.env; |
|
|
|
const router = new Router(); |
|
|
|
app.use(router.routes()); |
|
app.use(router.allowedMethods()); |
|
|
|
|
|
import { Client, auth } from "twitter-api-sdk"; |
|
|
|
|
|
const authClient = new auth.OAuth2User({ |
|
client_id: CLIENT_ID as string, |
|
client_secret: CLIENT_SECRET as string, |
|
callback: "https://huggingface-projects-twitter-image-alt-bot.hf.space/callback", |
|
scopes: ["tweet.read", "users.read", "offline.access"], |
|
}); |
|
|
|
|
|
const twitterClient = new Client(authClient); |
|
|
|
const BOT_NAME = "coyotte508"; |
|
|
|
function debug(stuff: any) { |
|
console.log(inspect(stuff, { depth: 20 })); |
|
} |
|
|
|
async function lookupTweets() { |
|
const resp = await fetch(`https://api.twitter.com/2/users/${BOT_NAME}/mentions`, { |
|
headers: { Authorization: `Bearer ${BEARER_TOKEN}` }, |
|
}); |
|
|
|
debug(await resp.json()); |
|
} |
|
|
|
async function listen() { |
|
try { |
|
const promise = new Promise<void>((resolve, reject) => { |
|
app.listen(port, "localhost", () => resolve()); |
|
app.once("error", (err) => reject(err)); |
|
}); |
|
|
|
await promise; |
|
|
|
console.log("app started on port", port); |
|
|
|
process.send?.("ready"); |
|
} catch (err) { |
|
console.error(err); |
|
} |
|
} |
|
|
|
listen(); |
|
|
|
process.on("unhandledRejection", async (err) => { |
|
console.error("unhandled rejection", err); |
|
}); |
|
|
|
lookupTweets(); |
|
|