3 * Requires a Telegram Bot token as an environment variable
4 */
5export async function sendTelegramMessage(chatId: string, message: string) {
6 const botToken = Deno.env.get('TELEGRAM_BOT_TOKEN');
7
4
5// Basic Auth middleware
6async function basicAuthMiddleware(req: Request): Promise<Response | null> {
7 const realm = "Usage Dashboard";
8 const unauthorizedResponse = new Response("Unauthorized", {
50}
51
52export default async function(req: Request) {
53 // Check authentication first
54 const authResponse = await basicAuthMiddleware(req);
286
287 <script>
288 document.addEventListener('DOMContentLoaded', function() {
289 // Add click event listeners to all tabs
290 document.querySelectorAll('.tab').forEach(tab => {
291 tab.addEventListener('click', function() {
292 const tabId = this.getAttribute('data-tab');
293
20}
21
22async function createTables() {
23 await sqlite.execute(`
24 CREATE TABLE IF NOT EXISTS ${USAGE_TABLE} (
42}
43
44async function deleteTables() {
45 await sqlite.execute(`DROP TABLE IF EXISTS ${USAGE_TABLE}`);
46}
7// but in the meantime, we can cache user info in memory
8const userIdCache: { [key: string]: any } = {};
9export async function getUser(bearerToken: string) {
10 if (userIdCache[bearerToken]) return userIdCache[bearerToken];
11
16}
17
18async function last24Hours(userId: string) {
19 const usage = await sqlite.execute(
20 `SELECT
37const DAILY_PRO_LIMIT = 5; // $5 per day
38
39export async function overLimit(bearerToken: string) {
40 const user = await getUser(bearerToken);
41 const last24HourUsage = await last24Hours(user.id);
44}
45
46export async function trackUsage({
47 bearerToken,
48 val_id,
5const CACHE_WRITE_RATE = 3.75; // $3.75 per M cache-write tokens
6
7export function calculateCost({
8 input_tokens,
9 output_tokens,
2 * Get information about the webhook status
3 */
4async function getWebhookInfo() {
5 try {
6 const botToken = process.env.TELEGRAM_BOT_TOKEN;
1import { OpenAI } from "https://esm.town/v/std/openai";
2
3export default async function(req: Request): Promise<Response> {
4 if (req.method === "OPTIONS") {
5 return new Response(null, {
1import { OpenAI } from "https://esm.town/v/std/openai";
2
3export default async function(req: Request): Promise<Response> {
4 if (req.method === "OPTIONS") {
5 return new Response(null, {
1import { OpenAI } from "https://esm.town/v/std/openai";
2
3export default async function(req: Request): Promise<Response> {
4 if (req.method === "OPTIONS") {
5 return new Response(null, {
1import { email } from "https://esm.town/v/std/email";
2
3export default async function(req: Request): Promise<Response> {
4 let body;
5 try {
A helper function to build a file's email
Simple functional CSS library for Val Town
LangChain (https://langchain.com) Ambassador, KubeSphere (https://kubesphere.io) Ambassador, CNCF OpenFunction (https://openfunction.dev) TOC Member.
import { OpenAI } from "https://esm.town/v/std/openai";
export default async function(req: Request): Promise<Response> {
if (req.method === "OPTIONS") {
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "*",