You can access search results via JSON API by adding format=json
to your query:
https://codesearch.val.run/image-url.jpg%20%22Optional%20title%22?q=api&page=19&format=json
For typeahead suggestions, use the /typeahead
endpoint:
https://codesearch.val.run/typeahead?q=api
Returns an array of strings in format "username" or "username/projectName"
Found 11798 results for "api"(431ms)
8// Environment variables should be set in your Val Town val's settings
9const WHATSAPP_WEBHOOK_VERIFY_TOKEN = Deno.env.get("WHATSAPP_WEBHOOK_VERIFY_TOKEN");
10const WHATSAPP_GRAPH_API_TOKEN = Deno.env.get("WHATSAPP_GRAPH_API_TOKEN");
11const WHATSAPP_BUSINESS_ID = Deno.env.get("WHATSAPP_BUSINESS_ID");
1242// --- Helper Functions ---
43async function sendWhatsAppMessage(phoneNumber, businessId, text, originalMessageId) {
44if (!WHATSAPP_GRAPH_API_TOKEN || !businessId) {
45console.error("WhatsApp API token or business ID is missing. Cannot send message.");
46return;
47}
54method: "POST",
55headers: {
56"Authorization": `Bearer ${WHATSAPP_GRAPH_API_TOKEN}`,
57"Content-Type": "application/json",
58},
120console.log(`[initiateChat] Sending to CF Orchestrator (${CLOUDFLARE_WORKER_ORCHESTRATOR_URL}/initiate-async-chat) for ${userIdentifier}:`, JSON.stringify(mcpPayload, null, 2));
121122const mcpApiResponse = await fetch(`${CLOUDFLARE_WORKER_ORCHESTRATOR_URL}/initiate-async-chat`, {
123method: "POST",
124headers: { "Content-Type": "application/json" },
126});
127128if (!mcpApiResponse.ok) {
129const errorText = await mcpApiResponse.text();
130console.error(`[initiateChat] Error from CF Orchestrator for ${clientRequestId}: ${mcpApiResponse.status} ${mcpApiResponse.statusText}`, errorText);
131await sendWhatsAppMessage(userIdentifier, businessPhoneNumberId, "Sorry, I couldn't start a session with the assistant.", originalMessageId);
132return { error: true, details: errorText };
133} else {
134const mcpResponseData = await mcpApiResponse.json();
135console.log(`[initiateChat] Response from CF Orchestrator for ${clientRequestId} (Status ${mcpApiResponse.status}):`, mcpResponseData);
136return { error: false, data: mcpResponseData };
137}
232console.log(`[PROCESS] Initiating async for ${userPhoneNumber} (hash: ${queryHash || 'N/A'}).`);
233234if (WHATSAPP_GRAPH_API_TOKEN && business_phone_number_id) {
235await initiateChatWithMCPOrchestrator(userQuery, userPhoneNumber, originalMessageId, business_phone_number_id);
236try { // Mark as read
237console.log(`Marking read for ${userPhoneNumber} (msg_id: ${originalMessageId})`);
238const markReadResp = await fetch(`https://graph.facebook.com/v22.0/${business_phone_number_id}/messages`, {
239method: "POST", headers: {"Authorization": `Bearer ${WHATSAPP_GRAPH_API_TOKEN}`, "Content-Type": "application/json"},
240body: JSON.stringify({messaging_product: "whatsapp", status: "read", message_id: originalMessageId}),
241});
244} catch (e) { console.error(`Failed to mark read for ${userPhoneNumber}:`, e); }
245} else {
246console.error("WHATSAPP_GRAPH_API_TOKEN or business_phone_number_id missing.");
247}
248} else if (waMessage) {
281// This means for direct GET, you'd need to monitor this Val's `/cf-worker-updates` or provide one.
282283const userIdentifier = "direct_api_user_" + crypto.randomUUID();
284const clientRequestId = crypto.randomUUID(); // Not used by initiateChat directly, but good practice
285400// Handle sending the welcome message
401app.post("/send-welcome", async (c) => {
402if (!WHATSAPP_GRAPH_API_TOKEN || !WHATSAPP_BUSINESS_ID) {
403console.error("WhatsApp API token or Business ID is not configured.");
404return c.json({ error: "Server configuration error: WhatsApp API token or Business ID is missing." }, 500);
405}
406433method: "POST",
434headers: {
435"Authorization": `Bearer ${WHATSAPP_GRAPH_API_TOKEN}`,
436"Content-Type": "application/json",
437},
444);
445446const responseData = await response.json().catch(() => ({ error: { message: "Received non-JSON response from WhatsApp API" } }));
447448if (!response.ok) {
449console.error(`Error sending custom message to ${phoneNumber}: ${response.status} ${response.statusText}`, responseData);
450return c.json({ error: `WhatsApp API Error: ${responseData.error?.message || response.statusText}`, details: responseData }, response.status > 0 ? response.status : 500);
451}
452
124async function getDiscussionPosts(discussionId: string): Promise<PostT[]> {
125// Used to get the list of post id's for the discussion.
126const discussionRes = await fetch(`${server}/api/discussions/${discussionId}`);
127const discussionResJson = await discussionRes.json();
128136137await Promise.all(chunks.map(async (c: string[]) => {
138const postRes = await fetch(`${server}/api/posts?filter[id]=${c.join(",")}`);
139const postJson = await postRes.json();
140