Avatar

@andreterron

27 likes149 public vals
Joined August 18, 2022
Dev Tools • Quantified Self Exploring Social Programming at Val Town (This platform!)

Actually, It's 𝕏

An annoying bot that corrects people about the new name of the app previously known as Twitter. Follow it on 𝕏!

Screenshot 2023-08-17 at 4.48.03 PM.png

The Twitter → 𝕏 transition is pretty painful, and this bot is here to make things even worse!

Want to create your own bot? Check out https://www.val.town/v/andreterron.twitter

Readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { correctPostAboutTwitter } from "https://esm.town/v/andreterron/correctPostAboutTwitter";
import { sortedPosts } from "https://esm.town/v/andreterron/sortedPosts";
import { searchPostsForTheActuallyBot } from "https://esm.town/v/andreterron/searchPostsForTheActuallyBot";
import { refreshActuallyBotToken } from "https://esm.town/v/andreterron/refreshActuallyBotToken";
export async function actuallyItsXBot() {
const token = await refreshActuallyBotToken();
//
// Search
const posts = await searchPostsForTheActuallyBot(
token.access_token,
);
const detailed = await sortedPosts({
posts,
accessToken: token.access_token,
});
//
const post = detailed[0];
if (!post) {
if (posts.length) {
throw new Error("Failed to get post users");
}
console.log("No valid posts returned");
return;
}
const reply = await correctPostAboutTwitter({
id: post.id,
accessToken: token.access_token,
});
//
return reply;
}

Use GPT to generate vals on your account!

Describe the val that you need, call this function, and you'll get a new val on your workspace generated by OpenAI's API!

First, ensure you have a Val Town API Token, then call @andreterron.createGeneratedVal({...}) like this example:

@andreterron.createGeneratedVal({
  valTownKey: @me.secrets.vt_token,
  description:
    "A val that given a text file position in `{line, col}` and the text contents, returns the index position",
});

This will create a val in your workspace, and here's the one created by the example above: https://www.val.town/v/andreterron.getFileIndexPosition

Readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
import { runVal } from "https://esm.town/v/std/runVal";
export async function createGeneratedVal({ description, valTownKey }: {
description: string;
valTownKey: string;
}) {
const code = await runVal(
"andreterron.generateValCodeAPI",
description,
);
const val = await fetchJSON(
`https://api.val.town/v1/vals`,
{
method: "POST",
headers: {
Authorization: `Bearer ${valTownKey}`,
},
body: JSON.stringify({
code,
}),
},
);
return val;
}

Email notifications when your vals are referenced

Screenshot 2023-08-18 at 1.32.34 PM.png

Setup

  1. Create a Val Town API Token
  2. Add it to your secrets as valtown
  3. Fork & Run this val
Readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { refHtml } from "https://esm.town/v/andreterron/refHtml";
import process from "node:process";
import { references } from "https://esm.town/v/andreterron/references";
export const referencesNotify = async (interval: Interval) => {
const refs = await references({
token: process.env.valtown,
since: interval.lastRunAt,
});
if (refs?.length) {
console.email({
html: refHtml(refs),
}, "Val Town References");
}
return refs;
};

Login with Twitter 𝕏

Use this val to login with 𝕏 and save your token into a twitter_token val

Usage

  1. Fork this val
  2. Change the 🔒 privacy to "Unlisted"
  3. Navigate to https://developer.twitter.com/en/portal/dashboard to create an OAuth app and save your client id and secret to your secrets page
  4. Navigate to https://{your_username}-twitter.web.val.run to authenticate with 𝕏

And then you'll have your tokens saved to a twitter_token val!

Other Twitter 𝕏 helper vals

Readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
let { twitter_token } = await import("https://esm.town/v/andreterron/twitter_token");
let { twitter_auth_storage } = await import("https://esm.town/v/andreterron/twitter_auth_storage");
import process from "node:process";
import { twitterAuthHandler } from "https://esm.town/v/andreterron/twitterAuthHandler";
export async function twitter(req: Request) {
// Constants
const username = "YOUR_VALTOWN_USERNAME";
//
const { res, token, storage } = await twitterAuthHandler(req, {
client_id: process.env.twitter_client_id,
client_secret: process.env.twitter_client_secret,
redirect_uri: `https://${username}-twitter.web.val.run/callback`,
scopes: ["tweet.read", "tweet.write", "users.read", "offline.access"],
storage: twitter_auth_storage,
});
//
if (storage) {
twitter_auth_storage = storage;
}
//
if (token) {
twitter_token = token;
}
//
return res;
}

An example of how to render an invoice using @vtdocs.generateInvoicePDF

Readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { generateInvoicePDF } from "https://esm.town/v/vtdocs/generateInvoicePDF";
// View me at https://andreterron-examplePDF.web.val.run!
export const examplePDF = async (req: Request): Promise<Response> => {
const invoicePDF = generateInvoicePDF({
invoiceNumber: "001",
date: new Date().toDateString(),
customerName: "Alice Bar",
customerEmail: "alice@bar.com",
items: [{ description: "Arabica Beans 500g", quantity: 2, price: 10 }, {
description: "Robusta Beans 500g",
quantity: 1,
price: 11,
}],
currencySymbol: "$",
});
return new Response(await invoicePDF, {
headers: {
"Content-Type": "application/pdf",
},
});
};
1
2
3
4
5
6
7
8
9
10
11
12
import { rootRef } from "https://esm.town/v/andreterron/rootRef?v=2";
import { ValRef } from "https://esm.town/v/andreterron/ValRef?v=1";
export function rootValRef(): ValRef | undefined {
const val = rootRef();
return val
? {
handle: val.userHandle,
name: val.valName,
}
: undefined;
}

Code on Val Town

Screenshot 2024-02-27 at 1.25.46 PM.png

Adds a "Code on Val Town" ribbon to your page. This lets your website visitors navigate to the code behind it.

This uses github-fork-ribbon-css under the hood.

Usage

Here are 2 different ways to add the "Code on Val Town" ribbon:

1. Wrap your fetch handler (recommended)

Create valimport { modifyFetchHandler } from "https://esm.town/v/andreterron/codeOnValTown?v=45"; import { html } from "https://esm.town/v/stevekrouse/html?v=5"; export default modifyFetchHandler(async (req: Request): Promise<Response> => { return html(`<h2>Hello world!</h2>`); });

Example: @andreterron/openable_handler

2. Wrap your HTML string

Create valimport { modifyHtmlString } from "https://esm.town/v/andreterron/codeOnValTown?v=45"; import { html } from "https://esm.town/v/stevekrouse/html?v=5"; export default async (req: Request): Promise<Response> => { return html(modifyHtmlString(`<h2>Hello world!</h2>`)); };

Example: @andreterron/openable_html

Other ways

We made sure this was very modular, so you can also add the ribbon using these methods:

Customization

Linking to the val

These functions infer the val using the call stack or the request URL. If the inference isn't working, or if you want to ensure it links to a specific val, pass the val argument:

  • modifyFetchHandler(handler, {val: { handle: "andre", name: "foo" }})
  • modifyHtmlString("<html>...", {val: { handle: "andre", name: "foo" }})

Styling

You can set the style parameter to a css string to customize the ribbon. Check out github-fork-ribbon-css to learn more about how to style the element.

  • modifyFetchHandler(handler, {style: ".github-fork-ribbon:before { background-color: #333; }"})
  • modifyHtmlString("<html>...", {style: ".github-fork-ribbon:before { background-color: #333; }"})

Here's how you can hide the ribbon on small screens:

Create valmodifyFetchHandler(handler, {style: `@media (max-width: 768px) { .github-fork-ribbon { display: none !important; } }`})

To-dos

  • Let users customize the ribbon. Some ideas are the text, color or placement.
Readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { modifyResponse } from "https://esm.town/v/andreterron/codeOnVT_modifyResponse?v=9";
import { ribbonElement } from "https://esm.town/v/andreterron/codeOnVT_ribbonElement?v=7";
import { inferRequestVal } from "https://esm.town/v/andreterron/inferRequestVal?v=2";
import { rootValRef } from "https://esm.town/v/andreterron/rootValRef?v=3";
import { ValRef } from "https://esm.town/v/andreterron/ValRef?v=1";
/**
* @param bodyText HTML string that will be used to inject the element.
* @param val Define which val should open. Defaults to the root reference.
*/
export function modifyHtmlString(
bodyText: string,
{ val, style }: { val?: ValRef; style?: string } = {},
) {
val = val?.handle && val?.name ? val : rootValRef();
if (!val) {
console.error("Failed to infer val. Please set the options.val parameter to the desired `{ handle, name }`");
return bodyText;
}
const element = ribbonElement({ val, style });
const bodyIndex = bodyText.lastIndexOf("</body>");
const htmlIndex = bodyText.lastIndexOf("</html>");
// Append the element before </body> or </html>. Get the last occurrence of each
// and use whichever one comes first.
if (bodyIndex !== -1 && (htmlIndex === -1 || bodyIndex < htmlIndex)) {
return bodyText.slice(0, bodyIndex) + element + bodyText.slice(bodyIndex);
} else if (htmlIndex !== -1) {
return bodyText.slice(0, htmlIndex) + element + bodyText.slice(htmlIndex);
} else {
return bodyText + element;
}
}
/**
* @param handler Fetch handler
* @param val Define which val should open
*/
export function modifyFetchHandler(
handler: (req: Request) => Response | Promise<Response>,
{ val, style }: { val?: ValRef; style?: string } = {},
): (req: Request) => Promise<Response> {
return async (req: Request): Promise<Response> => {
const res = await handler(req);
val = val?.handle && val?.name ? val : inferRequestVal(req);
return modifyResponse(res, { val, style });
};
}
export default modifyFetchHandler;

"Code on Val Town" Ribbon HTML Element

Ribbon element used by @andreterron/codeOnValTown

Usage

  • ribbonElement({val: { handle: "andre", name: "foo" }}) - define which val to link to;
  • ribbonElement() - infer the val from the call stack.

Example: @andreterron/openable_element

Create valimport { ribbonElement } from "https://esm.town/v/andreterron/codeOnVT_ribbonElement?v=3"; import { html } from "https://esm.town/v/stevekrouse/html?v=5"; export default async (req: Request): Promise<Response> => { return html(` <h2>Hello world!</h2> <style>* { font-family: sans-serif }</style> ${ribbonElement()} `); };
Readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { rootValRef } from "https://esm.town/v/andreterron/rootValRef?v=3";
import { ValRef } from "https://esm.town/v/andreterron/ValRef?v=1";
export function ribbonElement({ val, style }: { val?: ValRef; style?: string } = {}) {
const valRef = val?.handle && val?.name ? val : rootValRef();
if (!valRef) {
console.error("Failed to infer val. Please set the val parameter to the desired `{ handle, name }`");
return "";
}
const valSlug = `${valRef.handle}/${valRef.name}`;
return `
<div id="code-on-vt-host">
<template shadowrootmode="open">
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/github-fork-ribbon-css/0.2.3/gh-fork-ribbon.min.css"
/>
${style ? `<style>${style}</style>` : ""}
<a
href="https://www.val.town/v/${valSlug}"
rel="source"
target="_blank"
class="github-fork-ribbon"
data-ribbon="Code on Val Town"
title="Code on Val Town"
>
Code on Val Town
</a>
</template>
</div>`;
}

Inject HTML Element Stream

Use InjectHTMLElementStream to inject an HTML element inside the <body /> or <html /> tag of a streamed HTML string.

Readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const bodyRegex = /(<\/body[>\s])/;
const htmlRegex = /(<\/html[>\s])/;
const bodySplitTag = "</body";
const htmlSplitTag = "</html";
export class InjectHTMLElementStream extends TransformStream<string, string> {
elementSent = false;
buffered = "";
constructor(element: string) {
super({
transform: (chunk, controller) => {
// If something is buffered, add to this transform step
let bufferedChunk = this.buffered + chunk;
this.buffered = "";
// Check if the string contains the closing tags for body or html
if (!this.elementSent) {
if (bufferedChunk.match(bodyRegex)) {
controller.enqueue(bufferedChunk.replace(bodyRegex, `${element}$1`));
this.elementSent = true;
return;
}
if (bufferedChunk.match(htmlRegex)) {
controller.enqueue(bufferedChunk.replace(htmlRegex, `${element}$1`));
this.elementSent = true;
return;
}
// If the chunk ends with a partial match to the body or html string,
// buffer that to be used next chunk. Remove it from the current chunk
const end = bufferedChunk.slice(-7);
for (let i = 1; i <= 6; i++) {
const sliced = bufferedChunk.slice(-i);
if (sliced === bodySplitTag.slice(0, i) || sliced === htmlSplitTag.slice(0, i)) {
this.buffered = sliced;
bufferedChunk = bufferedChunk.slice(0, -i);
break;
}
}
}
// Base case
controller.enqueue(bufferedChunk);
},
flush: (controller) => {
// flush any buffered string
if (this.buffered) {
controller.enqueue(this.buffered);
this.buffered = "";
}
// enqueue element
if (!this.elementSent) {
controller.enqueue(element);
this.elementSent = true;
}
controller.terminate();
},
});
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { basicAuthorization } from "https://esm.town/v/stevekrouse/basicAuthorization";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON";
export async function refreshTwitterToken(
{ refresh_token, client_id, client_secret }: {
refresh_token: string;
client_id: string;
client_secret: string;
},
) {
const url = "https://api.twitter.com/2/oauth2/token";
const body = new URLSearchParams({
grant_type: "refresh_token",
refresh_token,
client_id,
});
return fetchJSON(url, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
...basicAuthorization(client_id, client_secret),
},
body: body.toString(),
});
}