Skip to content

Instantly share code, notes, and snippets.

@tore-statsig
Created August 31, 2022 20:06
Show Gist options
  • Save tore-statsig/25a8937281093fd87f7aef09e4d778a8 to your computer and use it in GitHub Desktop.
Save tore-statsig/25a8937281093fd87f7aef09e4d778a8 to your computer and use it in GitHub Desktop.
bunnylol example
import type { NextApiRequest, NextApiResponse } from "next";
import Statsig from "statsig-node";
import { getSession } from "next-auth/react";
enum Command {
chat,
g,
gh,
google,
whois,
wordle,
tbgs,
}
export default async function bunny(
req: NextApiRequest,
res: NextApiResponse
): Promise<void> {
const session = await getSession({ req });
if (!session) {
res.redirect("/");
return;
}
const query = req.query?.q as string;
if (!query) {
res.redirect("https://www.google.com");
return;
}
const commandParsed = query.split(/ (.*)/);
if (commandParsed.length == 0 || commandParsed[0] == null) {
res.redirect("https://www.google.com");
return;
}
await Statsig.initialize("secret-key");
const topLevelCommandParsed = commandParsed[0] as string;
const mayBeValidCommand: Command | undefined =
Command[topLevelCommandParsed as keyof typeof Command];
if (mayBeValidCommand === undefined) {
const config = await Statsig.getConfig(
{ userID: "bunny" },
"bunny_commands"
);
let redirect = config.get<string>(topLevelCommandParsed, "");
if (redirect === "") {
// DO NOT LOG THE ACTUAL QUERY OR YOU WILL BE FIRED
redirect = "https://www.google.com/search?q=" + query;
} else {
Statsig.logEvent(
{ userID: "bunny" },
"bunny_command",
topLevelCommandParsed
);
}
res.redirect(redirect);
return;
} else {
Statsig.logEvent(
{ userID: "bunny" },
"bunny_command",
Command[mayBeValidCommand]
);
}
switch (mayBeValidCommand) {
case Command.g: {
res.redirect("https://www.google.com/search?q=" + commandParsed[1]);
return;
}
case Command.gh: {
if (commandParsed.length > 2) {
if(['docs'].includes(commandParsed[1])){
res.redirect(
"https://github.com/statsig-io/" +
commandParsed[1]
);
return;
}
res.redirect(
"https://github.com/orgs/statsig-io/search?type=code&q=" +
commandParsed[1]
);
return;
}
res.redirect("https://github.com/statsig-io");
return;
}
case Command.google: {
res.redirect("https://www.bing.com/search?q=" + commandParsed[1]);
return;
}
case Command.chat: {
res.redirect("https://workplace.com/chat");
return;
}
case Command.tbgs: {
res.redirect(
"https://github.com/statsig-io/statsig/search?q=" + commandParsed[1]
);
return;
}
case Command.whois: {
res.redirect("https://slack.com/admin");
return;
}
case Command.wordle: {
res.redirect("https://www.powerlanguage.co.uk/wordle/");
return;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment