Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

Emoji search bot

Replies to mentions on twitter with the emojis your photo evokes. Inspired by Devon Zuegel.

Screenshot 2023-11-07 at 07.46.43@2x.png

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
import { postTweet } from "https://esm.town/v/andreterron/postTweet";
import { msSecond } from "https://esm.town/v/stevekrouse/msSecond";
import { refreshEmojiSearchToken } from "https://esm.town/v/stevekrouse/refreshEmojiSearchToken";
import { twitterJSON } from "https://esm.town/v/stevekrouse/twitterJSON";
import process from "node:process";
import OpenAI from "npm:openai";
const openai = new OpenAI({ apiKey: process.env.openai });
export async function emojiSearchBot({ lastRunAt }: Interval) {
const token = await refreshEmojiSearchToken();
let query = `?start_time=${
new Date(lastRunAt.getTime() - 5 * msSecond).toISOString()
}&expansions=attachments.media_keys&media.fields=duration_ms,height,media_key,preview_image_url,public_metrics,type,url,width,alt_text`;
const result = await twitterJSON({
url: `https://api.twitter.com/2/users/1721850098575908864/mentions` + query,
bearerToken: process.env.twitter,
});
if (!result.data || !result?.includes?.media) return;
let latestTweet = result.data.filter(d => d.attachments)[0];
let attachment = result.includes.media.find(m => m.media_key === latestTweet.attachments.media_keys[0]);
if (attachment.type !== "photo") return;
const response = await openai.chat.completions.create({
model: "gpt-4-vision-preview",
messages: [
{
role: "user",
content: [
{
type: "text",
text:
"I am trying to find an emoji. I took a selfie that's trying to evoke this emoji. Give me a list of potential emojis this photo evokes. Reply ONLY with emoji. No other text explaining your choices.",
},
{
type: "image_url",
image_url: attachment.url,
},
],
},
],
max_tokens: 10,
});
return postTweet({
accessToken: token.access_token,
text: response.choices[0].message.content,
reply: { "in_reply_to_tweet_id": latestTweet.id },
});
}
November 7, 2023