You can access search results via JSON API by adding format=json
to your query:
https://codesearch.val.run/image-url.jpg%20%22Image%20title%22?q=api&page=44&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 11775 results for "api"(470ms)
120async function getDiscussionPosts(discussionId: string): Promise<PostT[]> {
121// Used to get the list of post id's for the discussion.
122const discussionRes = await fetch(`${server}/api/discussions/${discussionId}`);
123const discussionResJson = await discussionRes.json();
124132133await Promise.all(chunks.map(async (c: string[]) => {
134const postRes = await fetch(`${server}/api/posts?filter[id]=${c.join(",")}`);
135const postJson = await postRes.json();
136
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}
234console.log(`[PROCESS] Initiating async for ${userPhoneNumber} (hash: ${queryHash || 'N/A'}).`);
235236if (WHATSAPP_GRAPH_API_TOKEN && business_phone_number_id) {
237await initiateChatWithMCPOrchestrator(userQuery, userPhoneNumber, originalMessageId, business_phone_number_id);
238try { // Mark as read
239console.log(`Marking read for ${userPhoneNumber} (msg_id: ${originalMessageId})`);
240const markReadResp = await fetch(`https://graph.facebook.com/v18.0/${business_phone_number_id}/messages`, {
241method: "POST", headers: {"Authorization": `Bearer ${WHATSAPP_GRAPH_API_TOKEN}`, "Content-Type": "application/json"},
242body: JSON.stringify({messaging_product: "whatsapp", status: "read", message_id: originalMessageId}),
243});
246} catch (e) { console.error(`Failed to mark read for ${userPhoneNumber}:`, e); }
247} else {
248console.error("WHATSAPP_GRAPH_API_TOKEN or business_phone_number_id missing.");
249}
250} else if (waMessage) {
283// This means for direct GET, you'd need to monitor this Val's `/cf-worker-updates` or provide one.
284285const userIdentifier = "direct_api_user_" + crypto.randomUUID();
286const clientRequestId = crypto.randomUUID(); // Not used by initiateChat directly, but good practice
287386// Handle sending the welcome message
387app.post("/send-welcome", async (c) => {
388if (!WHATSAPP_GRAPH_API_TOKEN || !WHATSAPP_BUSINESS_ID) {
389console.error("WhatsApp API token or Business ID is not configured.");
390return c.json({ error: "Server configuration error: WhatsApp API token or Business ID is missing." }, 500);
391}
392416method: "POST",
417headers: {
418"Authorization": `Bearer ${WHATSAPP_GRAPH_API_TOKEN}`,
419"Content-Type": "application/json",
420},
431);
432433const responseData = await response.json().catch(() => ({ error: { message: "Received non-JSON response from WhatsApp API" } })); // Ensure responseData is an object
434435if (!response.ok) {
436console.error(`Error sending template message to ${phoneNumber}: ${response.status} ${response.statusText}`, responseData);
437return c.json({ error: `WhatsApp API Error: ${responseData.error?.message || response.statusText}`, details: responseData }, response.status > 0 ? response.status : 500);
438}
439