Upload 3 files
Browse files- Dockerfile +13 -0
- README.md +57 -0
- server.js +120 -0
Dockerfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM node:22-bookworm-slim
|
| 2 |
+
|
| 3 |
+
RUN apt-get update \
|
| 4 |
+
&& apt-get install -y --no-install-recommends curl ca-certificates \
|
| 5 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 6 |
+
|
| 7 |
+
WORKDIR /app
|
| 8 |
+
COPY server.js /app/server.js
|
| 9 |
+
|
| 10 |
+
ENV PORT=7860
|
| 11 |
+
EXPOSE 7860
|
| 12 |
+
|
| 13 |
+
CMD ["node", "server.js"]
|
README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Facebook Hello World Test
|
| 3 |
+
emoji: 🧪
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# Facebook Hello World — Hugging Face test
|
| 11 |
+
|
| 12 |
+
This Space does only one thing:
|
| 13 |
+
|
| 14 |
+
1. Starts on port `7860`.
|
| 15 |
+
2. Waits 5 seconds.
|
| 16 |
+
3. Sends a Facebook Page post with exactly:
|
| 17 |
+
|
| 18 |
+
```text
|
| 19 |
+
Hello World
|
| 20 |
+
```
|
| 21 |
+
|
| 22 |
+
It uses `curl -4` so the test forces IPv4.
|
| 23 |
+
|
| 24 |
+
## Required Hugging Face Secrets
|
| 25 |
+
|
| 26 |
+
Add these under **Settings → Variables and secrets → Secrets**:
|
| 27 |
+
|
| 28 |
+
- `FB_PAGE_ID`
|
| 29 |
+
- `FB_PAGE_ACCESS_TOKEN`
|
| 30 |
+
|
| 31 |
+
Optional variable:
|
| 32 |
+
|
| 33 |
+
- `FB_GRAPH_API_VERSION` — default: `v25.0`
|
| 34 |
+
|
| 35 |
+
## What to check
|
| 36 |
+
|
| 37 |
+
After deploy, open **Logs**.
|
| 38 |
+
|
| 39 |
+
Success looks like:
|
| 40 |
+
|
| 41 |
+
```text
|
| 42 |
+
[TEST] SUCCESS ✅
|
| 43 |
+
[TEST] HTTP STATUS: 200
|
| 44 |
+
[TEST] META RESPONSE: {"id":"..."}
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
+
Failure prints the HTTP status and the full Meta error response.
|
| 48 |
+
|
| 49 |
+
You can also open:
|
| 50 |
+
|
| 51 |
+
```text
|
| 52 |
+
/status
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
to see the latest result.
|
| 56 |
+
|
| 57 |
+
> This is intentionally a test project. It attempts one `Hello World` post every time the container starts.
|
server.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const http = require("http");
|
| 2 |
+
const { execFile } = require("child_process");
|
| 3 |
+
|
| 4 |
+
const PORT = Number(process.env.PORT || 7860);
|
| 5 |
+
const PAGE_ID = String(process.env.FB_PAGE_ID || "").trim();
|
| 6 |
+
const TOKEN = String(process.env.FB_PAGE_ACCESS_TOKEN || "").trim();
|
| 7 |
+
const API_VERSION = String(process.env.FB_GRAPH_API_VERSION || "v25.0").trim();
|
| 8 |
+
const MESSAGE = "Hello World";
|
| 9 |
+
|
| 10 |
+
let lastResult = {
|
| 11 |
+
attempted: false,
|
| 12 |
+
ok: false,
|
| 13 |
+
status: null,
|
| 14 |
+
response: null,
|
| 15 |
+
error: null,
|
| 16 |
+
at: null
|
| 17 |
+
};
|
| 18 |
+
|
| 19 |
+
function maskToken(token) {
|
| 20 |
+
if (!token) return "(missing)";
|
| 21 |
+
if (token.length < 10) return "***";
|
| 22 |
+
return token.slice(0, 4) + "..." + token.slice(-4);
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
function postHelloWorld() {
|
| 26 |
+
lastResult = {
|
| 27 |
+
attempted: true,
|
| 28 |
+
ok: false,
|
| 29 |
+
status: null,
|
| 30 |
+
response: null,
|
| 31 |
+
error: null,
|
| 32 |
+
at: new Date().toISOString()
|
| 33 |
+
};
|
| 34 |
+
|
| 35 |
+
console.log("--------------------------------------------------");
|
| 36 |
+
console.log("[TEST] Facebook Hello World test starting");
|
| 37 |
+
console.log("[TEST] PAGE_ID:", PAGE_ID || "(missing)");
|
| 38 |
+
console.log("[TEST] TOKEN:", maskToken(TOKEN));
|
| 39 |
+
console.log("[TEST] API_VERSION:", API_VERSION);
|
| 40 |
+
console.log("[TEST] MESSAGE:", MESSAGE);
|
| 41 |
+
|
| 42 |
+
if (!PAGE_ID || !TOKEN) {
|
| 43 |
+
lastResult.error = "Missing FB_PAGE_ID or FB_PAGE_ACCESS_TOKEN";
|
| 44 |
+
console.error("[TEST] ERROR:", lastResult.error);
|
| 45 |
+
console.log("--------------------------------------------------");
|
| 46 |
+
return;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
const url = `https://graph.facebook.com/${API_VERSION}/${encodeURIComponent(PAGE_ID)}/feed`;
|
| 50 |
+
|
| 51 |
+
const args = [
|
| 52 |
+
"-4",
|
| 53 |
+
"-sS",
|
| 54 |
+
"--connect-timeout", "15",
|
| 55 |
+
"--max-time", "30",
|
| 56 |
+
"-w", "\n__HTTP_STATUS__:%{http_code}\n",
|
| 57 |
+
"-X", "POST",
|
| 58 |
+
url,
|
| 59 |
+
"--data-urlencode", `message=${MESSAGE}`,
|
| 60 |
+
"--data-urlencode", `access_token=${TOKEN}`
|
| 61 |
+
];
|
| 62 |
+
|
| 63 |
+
execFile("curl", args, { timeout: 35000, maxBuffer: 1024 * 1024 }, (err, stdout, stderr) => {
|
| 64 |
+
const out = String(stdout || "");
|
| 65 |
+
const errOut = String(stderr || "").trim();
|
| 66 |
+
|
| 67 |
+
const match = out.match(/\n__HTTP_STATUS__:(\d{3})\s*$/);
|
| 68 |
+
const status = match ? Number(match[1]) : null;
|
| 69 |
+
const body = match ? out.slice(0, match.index).trim() : out.trim();
|
| 70 |
+
|
| 71 |
+
lastResult.status = status;
|
| 72 |
+
lastResult.response = body || null;
|
| 73 |
+
|
| 74 |
+
if (err) {
|
| 75 |
+
lastResult.error = `${err.message}${errOut ? " | " + errOut : ""}`;
|
| 76 |
+
console.error("[TEST] CURL ERROR:", lastResult.error);
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
if (status && status >= 200 && status < 300) {
|
| 80 |
+
lastResult.ok = true;
|
| 81 |
+
console.log("[TEST] SUCCESS ✅");
|
| 82 |
+
console.log("[TEST] HTTP STATUS:", status);
|
| 83 |
+
console.log("[TEST] META RESPONSE:", body);
|
| 84 |
+
} else {
|
| 85 |
+
console.error("[TEST] FAILED ❌");
|
| 86 |
+
console.error("[TEST] HTTP STATUS:", status);
|
| 87 |
+
console.error("[TEST] META RESPONSE:", body || "(empty)");
|
| 88 |
+
if (errOut) console.error("[TEST] CURL STDERR:", errOut);
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
console.log("--------------------------------------------------");
|
| 92 |
+
});
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
const server = http.createServer((req, res) => {
|
| 96 |
+
if (req.url === "/status") {
|
| 97 |
+
res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
|
| 98 |
+
res.end(JSON.stringify({
|
| 99 |
+
service: "facebook-hello-world-hf-test",
|
| 100 |
+
pageConfigured: Boolean(PAGE_ID),
|
| 101 |
+
tokenConfigured: Boolean(TOKEN),
|
| 102 |
+
apiVersion: API_VERSION,
|
| 103 |
+
lastResult
|
| 104 |
+
}, null, 2));
|
| 105 |
+
return;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" });
|
| 109 |
+
res.end(
|
| 110 |
+
"Facebook Hello World HF Test\n\n" +
|
| 111 |
+
"The container attempts one Facebook post on startup.\n" +
|
| 112 |
+
"Open /status for the latest result.\n"
|
| 113 |
+
);
|
| 114 |
+
});
|
| 115 |
+
|
| 116 |
+
server.listen(PORT, "0.0.0.0", () => {
|
| 117 |
+
console.log(`[SERVER] Listening on 0.0.0.0:${PORT}`);
|
| 118 |
+
console.log("[SERVER] Waiting 5 seconds, then sending Facebook test post...");
|
| 119 |
+
setTimeout(postHelloWorld, 5000);
|
| 120 |
+
});
|