Public
Script
Readme

Message yourself on Telegram

This val lets you send yourself Telegram messages via ValTownBot. This ValTownBot saves you from creating your own Telegram Bot.

However if I'm being honest, it's really simple and fun to make your own Telegram bot. (You just message BotFather.) I'd recommend most folks going that route so you have an unmediated connection to Telegram. However if you want to have the simplest possible setup to just send yourself messages, read on...

Installation

It takes less than a minute to set up!

  1. Start a conversation with ValTownBot

  2. Copy the secret it gives you

  3. Save it in your Val Town Environment Variables under telegram

  4. Send a message!

Usage

telegramText

import { telegramText } from "https://esm.town/v/stevekrouse/telegram?v=14"; const statusResponse = await telegramText("Hello from Val.Town!!"); console.log(statusResponse);

telegramPhoto

import { telegramPhoto } from "https://esm.town/v/stevekrouse/telegram?v=14"; const statusResponse = await telegramPhoto({ photo: "https://placekitten.com/200/300", }); console.log(statusResponse);

ValTownBot Commands

  • /roll - Roll your secret in case you accidentally leak it.
  • /webhook - Set a webhook to receive messages you send to @ValTownBot

Receiving Messages

If you send /webhook to @ValTownBot, it will let you specify a webhook URL. It will then forward on any messages (that aren't recognized @ValTownBot commands) to that webhook. It's particularly useful for creating personal chatbots, like my telegram <-> DallE bot.

How it works

Telegram has a lovely API.

  1. I created a @ValTownBot via Bot Father.
  2. I created a webhook and registered it with telegram
  3. Whenever someone new messages @ValTownBot, I generate a secret and save it along with their Chat Id in @stevekrouse/telegramValTownBotSecrets (a private val), and message it back to them
  4. Now whenever you call this val, it calls telegramValTownAPI, which looks up your Chat Id via your secret and sends you a message

Telegram Resources

Credits

This val was originally made by pomdtr.

Todo

  • Store user data in Val Town SQLite
  • Parse user data on the API side using Zod
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
6
bkyerv avatar
stevekrouse avatar
robinmetral avatar

The message goes out for me, but my Val subsequently fails with a SyntaxError: Unexpected end of JSON input. Am I holding it wrong?

stevekrouse avatar

Sorry about that! This val is pretty stale. Will upgrade it now...

stevekrouse avatar

Ok! This val has been modernized. Can you give one of these examples a go? https://www.val.town/v/stevekrouse/telegram#usage

stevekrouse avatar

ps - the response of this API isn't terribly informative because I don't want to be careful not to leak any data. Is there any data you were looking for in particular or you just don't want an error message?

April 30, 2024