Background
Recently I wrote a proof-of-concept vector search app using pyscript. It takes any detailed question or description of any conspiracy-related topic as input and outputs a list of the most relevant news article summaries from the WantToKnow.info news archive. There are about 12,970 valid entries in the archive. The app looks at vectors of every one of these news articles and compares these to a vector computed from a user query to find a list of the most relevant items.
The Bot Father
The other day I posted about hitting a roadblock with the OpenAI API. I wanted to use gpt to create a brief summary of the search results. Yet no matter what I did, I kept getting timeout errors when calling the API from my app. So I decided to set aside the gpt integration until someone on discord helps me figure out what exactly the problem is. The AI results brief might be a neat feature, but it's not by any means essential.
To add more interactivity to my search app, I decided to create a Telegram group called WantToKnow Unofficial, then I embedded a display of the group's 5 most recent messages into the search page under where search results appear. None of Telegram's out-of-the-box widgets were suited to this task. So I created a bot to fetch the messages with Telegram's API.
In order to do this, I had to chat with the Bot Father. The Bot Father gave me an API key and helped me create my own bot. In initial testing, I was able to post a message to the group using https://api.telegram.org/bot{api key}/sendMessage?chat_id={groupID}&text=Yes+I+am+here+to+assist
, but for reasons unknown I had to add a dash before the groupID or the request would fail. After some more fiddling around, I added the Telegram chat to my pyscript page like this:
async def fetch_telegram_messages():
telegram_token = decoded_token
url = f'https://api.telegram.org/bot{telegram_token}/getUpdates'
response = await fetch(url)
data = await response.json()
display_telegram_messages(data.to_py()['result'])
def display_telegram_messages(messages):
container = Element('telegram-messages')
container.clear()
for msg in messages:
message_text = msg.get('message', {}).get('text', '')
if message_text:
message_element = document.createElement('p')
message_element.setAttribute('id', 'chatstream')
message_element.textContent = message_text
container.element.appendChild(message_element)
# Fetch messages periodically
async def periodic_fetch():
while True:
await fetch_telegram_messages()
await asyncio.sleep(5)
# Start fetching messages
asyncio.ensure_future(periodic_fetch())
Minor Challenges
There were a couple of challenges in adding the chat to the page. First was the issue of API key exposure. The solution I implemented is operative, but it's hilarious from a security standpoint. It would be easy for a skilled adversary to decipher the key. Fortunately, the stakes are low. If someone out there does get the key, the worst they can do is cause the bot to misbehave. And when a bot misbehaves, it's trivial to shut it down.
Another challenge was aesthetic. I was getting messages, but they weren't being displayed correctly, and css seemed unable to style the message text. After trying many things, I eventually added message_element.setAttribute('id', 'chatstream')
to the incoming text objects, which allowed me to use chatstream as a css selector. This approach worked perfectly.
Overall, the little app is coming together nicely. It's just a single page, but it nods towards a variety of web decentralization that's now becoming possible. Anyone can load this page locally, querying the dataset and monitoring Telegram messages in complete privacy. Anyone who knows Pyscript can modify this tool to look at the data in any way they like, secure in the knowledge that what they're accessing hasn't been tampered with, because the dataset is stored on IPFS.
Soon I'll show my colleagues what I've been up to and hopefully they'll like it. This project paved the way for us to make our site searchable by publisher and I'd like to see that happen. If nothing else, I've made a novel research tool. And I'm learning all kinds of fun stuff.
Read Free Mind Gazette on Substack
Read my novels:
- Small Gods of Time Travel is available as a web book on IPFS and as a 41 piece Tezos NFT collection on Objkt.
- The Paradise Anomaly is available in print via Blurb and for Kindle on Amazon.
- Psychic Avalanche is available in print via Blurb and for Kindle on Amazon.
- One Man Embassy is available in print via Blurb and for Kindle on Amazon.
- Flying Saucer Shenanigans is available in print via Blurb and for Kindle on Amazon.
- Rainbow Lullaby is available in print via Blurb and for Kindle on Amazon.
- The Ostermann Method is available in print via Blurb and for Kindle on Amazon.
- Blue Dragon Mississippi is available in print via Blurb and for Kindle on Amazon.
See my NFTs:
- Small Gods of Time Travel is a 41 piece Tezos NFT collection on Objkt that goes with my book by the same name.
- History and the Machine is a 20 piece Tezos NFT collection on Objkt based on my series of oil paintings of interesting people from history.
- Artifacts of Mind Control is a 15 piece Tezos NFT collection on Objkt based on declassified CIA documents from the MKULTRA program.