Home > Reflections | โฎ๏ธ โญ๏ธ

2025-06-07 | ๐Ÿšœ Farm | ๐Ÿ’พ Software | ๐Ÿค• Trauma | ๐Ÿค–๐Ÿฆ AutoTweet โŒจ๏ธ

๐Ÿ“š Books

๐Ÿ“ฐ News

๐Ÿค–๐Ÿฆ Semiautomatic Tweets โŒจ๏ธ

โœจ Obsidian Templater code for opening a ๐Ÿ’ป browser window with an ๐Ÿค– AI-generated ๐Ÿฆ tweet, ready to ๐Ÿš€ post.

โ“ What this code does:

  1. ๐Ÿ“– Read the content of the current note ๐Ÿ“
  2. ๐Ÿ—ฃ๏ธ Ask Gemini ๐Ÿค– to identify a list of ๐Ÿท๏ธ topics from the note ๐Ÿ“
  3. โœ๏ธ Format a tweet ๐Ÿฆ with:
    1. ๐Ÿ“ƒ the title of the current note ๐Ÿ“
    2. ๐Ÿท๏ธ The formatted topics identified by Gemini ๐Ÿค–
    3. ๐ŸŒ The URL to the web page this note is published to ๐Ÿš€
  4. ๐Ÿ’ป Open a browser window ๐ŸŒ to the ๐Ÿฆ twitter post form, prefilled with the generated tweet ๐Ÿค–โœ๏ธ ๐ŸŽ‰.
<%*  
const GEMINI_API_KEY = "๐Ÿคซ Secret ๐Ÿ—๏ธ key";  
  
const tweetPrompt = (body) => `Identify the main topics in the following.  
Return a pipe separated list of emoji-topic pairs.  
Prefer specific topics (e.g. don't include "Books" - that's too generic).  
Don't use terms that are already present in the title.  
  
2 Examples:  
๐Ÿ“œ Ancient Wisdom | โš”๏ธ Military Strategy | ๐Ÿ’ก Timeless Principles | ๐Ÿง  Strategic Thinking | ๐ŸŽฏ Path to Victory  
๐Ÿ—๏ธ Principles | ๐Ÿช– Leadership | ๐Ÿค” Decisions  
  
---  
  
${body}`  
  
const file = tp.file.find_tfile(tp.file.path(true));  
  
async function generateTextFromGemini(prompt, apiKey, model = "gemini-2.0-flash", search = false) {  
  const apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent?key=${apiKey}`;  
  
  const body = {  
      "contents": [  
          {  
              "parts": [  
                  {"text": prompt }  
              ]  
          }  
      ],  
      "tools": [  
          {  
              "google_search": {}  
          }  
      ]  
  };  
  if (!search) delete body.tools  
  
  
    const response = await fetch(apiUrl, {  
      method: "POST",  
      headers: {  
        "Content-Type": "application/json",  
      },  
      body: JSON.stringify(body),  
    });  
  
    if (!response.ok) {  
      throw new Error(`HTTP error! status: (${response.status}) ${await response.text()}`);  
    }  
  
    const data = await response.json();  
    const { parts } = data.candidates[0].content;  
    if (!parts) {  
      throw new Error(`No parts from Gemini! url='${apiUrl}'! unexpected response: data=(${JSON.stringify(data)})`)  
    }  
    return (parts[1] || parts[0]).text;  
}  
  
const model = 'gemini-2.5-flash-preview-04-17'  
  
try {  
const originalContent = await app.vault.read(file);  
  
const tweetBody = await generateTextFromGemini(tweetPrompt(originalContent), GEMINI_API_KEY)  
      .then(t => t.trim());  
  
const tweet = `${tp.frontmatter.title}  
  
${tweetBody}  
  
${tp.frontmatter.URL}`  
  
const URL = `https://twitter.com/intent/tweet?text=${encodeURIComponent(tweet.trim())}`  
window.open(URL);  
  
} catch (error) {  
    tR += `Error: ${error.message}`;  
}  
%>  

๐Ÿ‘€๐Ÿ› Bug Report

๐Ÿฆ Tweet