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=22&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 11820 results for "api"(553ms)
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");
12const GROQ_API_KEY = Deno.env.get("GROQ_API_KEY"); // For the new quick ack
1314// URL of YOUR Cloudflare Worker that orchestrates MCP calls & has /initiate-async-chat
46// --- Helper Functions ---
47async function sendWhatsAppMessage(phoneNumber, businessId, text, originalMessageId) {
48if (!WHATSAPP_GRAPH_API_TOKEN || !businessId) {
49console.error("WhatsApp API token or business ID is missing. Cannot send message.");
50return;
51}
58method: "POST",
59headers: {
60"Authorization": `Bearer ${WHATSAPP_GRAPH_API_TOKEN}`,
61"Content-Type": "application/json",
62},
90let messageToSend = "Hmm, let me look into that for you!"; // Default deferral message
9192if (!GROQ_API_KEY) {
93console.warn("[SmartAck] GROQ_API_KEY missing. Sending static deferral ack.");
94await sendWhatsAppMessage(phoneNumber, businessId, messageToSend, originalMessageId);
95return { action: "defer_to_worker", messageSent: messageToSend };
177try {
178console.log(`[SmartAck] Attempting to generate smart ack for: "${userQuery.substring(0, 50)}..." with ${QUICK_ACK_MODEL}`);
179const groqResponse = await fetch("https://api.groq.com/openai/v1/chat/completions", {
180method: "POST",
181headers: {
182"Authorization": `Bearer ${GROQ_API_KEY}`,
183"Content-Type": "application/json",
184},
198if (!groqResponse.ok) {
199const errorText = await groqResponse.text().catch(() => "Could not retrieve error text");
200console.error(`[SmartAck] Error from Groq API: ${groqResponse.status} ${groqResponse.statusText}`, errorText);
201// Fallback to deferral message
202} else {
308console.log(`[initiateChat] Sending to CF Orchestrator (${CLOUDFLARE_WORKER_ORCHESTRATOR_URL}/initiate-async-chat) for ${userIdentifier}:`, JSON.stringify(mcpPayload, null, 2));
309310const mcpApiResponse = await fetch(`${CLOUDFLARE_WORKER_ORCHESTRATOR_URL}/initiate-async-chat`, {
311method: "POST",
312headers: { "Content-Type": "application/json" },
314});
315316if (!mcpApiResponse.ok) {
317const errorText = await mcpApiResponse.text();
318console.error(`[initiateChat] Error from CF Orchestrator for ${clientRequestId}: ${mcpApiResponse.status} ${mcpApiResponse.statusText}`, errorText);
319await sendWhatsAppMessage(userIdentifier, businessPhoneNumberId, "Sorry, I couldn't start a session with the assistant.", originalMessageId);
320return { error: true, details: errorText };
321} else {
322const mcpResponseData = await mcpApiResponse.json();
323console.log(`[initiateChat] Response from CF Orchestrator for ${clientRequestId} (Status ${mcpApiResponse.status}):`, mcpResponseData);
324return { error: false, data: mcpResponseData };
325}
458console.log(`[PROCESS] Smartly Acknowledging for ${userPhoneNumber} (hash: ${queryHash || 'N/A'}). Effective Query for Ack: "${userQueryForSmartAck.substring(0,50)}..."`);
459460if (WHATSAPP_GRAPH_API_TOKEN && business_phone_number_id) {
461// 1. Mark as read (sends blue check for the CURRENT incoming message)
462try {
463console.log(`Marking read for ${userPhoneNumber} (msg_id: ${currentMessageId})`);
464const markReadResp = await fetch(`https://graph.facebook.com/v22.0/${business_phone_number_id}/messages`, {
465method: "POST", headers: {"Authorization": `Bearer ${WHATSAPP_GRAPH_API_TOKEN}`, "Content-Type": "application/json"},
466body: JSON.stringify({messaging_product: "whatsapp", status: "read", message_id: currentMessageId}),
467});
494495} else {
496console.error("WHATSAPP_GRAPH_API_TOKEN or business_phone_number_id missing.");
497}
498} else if (waMessage) {
534// This means for direct GET, you'd need to monitor this Val's `/cf-worker-updates` or provide one.
535536const userIdentifier = "direct_api_user_" + crypto.randomUUID();
537const clientRequestId = crypto.randomUUID(); // Not used by initiateChat directly, but good practice
538653// Handle sending the welcome message
654app.post("/send-welcome", async (c) => {
655if (!WHATSAPP_GRAPH_API_TOKEN || !WHATSAPP_BUSINESS_ID) {
656console.error("WhatsApp API token or Business ID is not configured.");
657return c.json({ error: "Server configuration error: WhatsApp API token or Business ID is missing." }, 500);
658}
659686method: "POST",
687headers: {
688"Authorization": `Bearer ${WHATSAPP_GRAPH_API_TOKEN}`,
689"Content-Type": "application/json",
690},
697);
698699const responseData = await response.json().catch(() => ({ error: { message: "Received non-JSON response from WhatsApp API" } }));
700701if (!response.ok) {
702console.error(`Error sending custom message to ${phoneNumber}: ${response.status} ${response.statusText}`, responseData);
703return c.json({ error: `WhatsApp API Error: ${responseData.error?.message || response.statusText}`, details: responseData }, response.status > 0 ? response.status : 500);
704}
705
8const { isAuthenticated, authenticate, error } = useAuth();
9const [tokenValue, setTokenValue] = useState("");
10const [apiKey, setApiKey] = useState("");
11// const [invalid, setInvalid] = useState(""); // TODO
1213const handleSubmit = (e) => {
14e.preventDefault();
15authenticate(tokenValue, apiKey);
16};
1736>
37<div>
38<label htmlFor="valtown-token" className="label">Val Town API Token</label>
39<div style={{ fontSize: "0.8em", color: "#666" }}>
40<p>
41<a href="https://www.val.town/settings/api/new" target="_blank" rel="noreferrer">
42Create a Val Town token here
43</a>
58</div>
59<div>
60<label htmlFor="anthropic-api-key" className="label">Anthropic API Key (optional)</label>
61<input
62type="password"
63id="anthropic-api-key"
64name="anthropic-key"
65value={apiKey}
66onChange={e => {
67setApiKey(e.target.value);
68}}
69/>