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

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

telegramPhoto

Create valimport { 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
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
import { runVal } from "https://esm.town/v/std/runVal";
import type { TelegramSendMessageOptions } from "https://esm.town/v/stevekrouse/telegramSendMessage";
import type { TelegramSendPhotoOptions } from "https://esm.town/v/stevekrouse/telegramSendPhoto";
/**
* Send a text message to yourself from the ValTownBot Telegram bot
* Message https://t.me/ValTownBot to get started
* @param text
* @param options
* @param authorization
*/
export async function telegramText(text: string, options?: TextOptions, authorization?: string) {
return telegramRequest("text", { text, options }, authorization);
}
/**
* Send a photo to yourself from the ValTownBot Telegram bot
* Message https://t.me/ValTownBot to get started
* @param options
* @param authorization
*/
export async function telegramPhoto(options: PhotoOptions, authorization?: string) {
return telegramRequest("photo", { options }, authorization);
}
async function telegramRequest(path, body, authorization?: string) {
const response = await fetch("https://stevekrouse-telegramValTownAPI.web.val.run/" + path, {
method: "POST",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${authorization ?? Deno.env.get("telegram")}`,
},
});
if (!response.ok) {
const error = await response.text();
throw new Error(error);
} else {
return "SUCCESS";
}
}
type TextOptions = Omit<TelegramSendMessageOptions, "chat_id">;
type PhotoOptions = Omit<TelegramSendPhotoOptions, "chat_id">;
/**
* @deprecated since 4/20/2024
*/
export async function telegram(secret: string, text: string, options?: MergedOptions) {
return runVal("stevekrouse.telegramValTownBot", secret, text, options);
}
export type MergedOptions = TextOptions & PhotoOptions;
šŸ‘† This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data ā€” all from the browser.