Skip to content

The moment it happens

Run a Telegram assistant that remembers each user

Your Telegram bot replies with AI and remembers each user's recent messages, stored in your Supabase tables, so conversations stay in context.

Runs
The moment it happens
Apps
Supabase, Telegram
AI
Yes, uses the built-in AI allowance
Access
Supabase read+write; bot token

Send this to your assistant

Create a ClickRuntime automation: when someone messages my Telegram bot, reply with AI using their last 20 messages from my Supabase messages table, and save both their message and the reply.

Paste it into your assistant. It tells you what it will send or change before it runs live.

First time? Adding ClickRuntime to your assistant takes about two minutes.

Why use it

Why teams use this automation

A coach or small community runs a Telegram bot for members. Without memory, every message starts from zero: the bot forgets the member's name, their goal and what it said yesterday, so the conversation feels robotic.

Keeping each user's recent messages in Supabase and passing them to the reply gives the bot continuity, while you keep full ownership of the conversation history.

What you get out of it

  • Conversations that make sense

    The bot remembers what each person said earlier, so replies follow on naturally.

  • Separate memory for every user

    Each person's history is kept apart, so nothing leaks between chats.

  • History you own

    Messages are stored in your Supabase project, where you can review, export or delete them.

  • Costs kept in check

    Only the last 20 messages are sent with each reply.

How it works

What happens, step by step

  1. Someone sends your Telegram bot a message.

  2. ClickRuntime records the user in your Supabase users table if they're new.

  3. It loads their last 20 messages from your messages table.

  4. AI writes a reply that takes that history into account.

  5. The bot sends the reply, and both messages are saved to Supabase.

Before it runs live

  • Connect Supabase
  • Connect Telegram
  • Paste your automation's link into the other app's webhook settings

What you get

RunsThe moment it happens
Run a Telegram assistant that remembers each userjust nowSucceeded
Telegram · Northwind Coach botNice work on the 8 km, Sam! Last week you said your knee was sore – how did it feel today?

Build your own

Need it to work differently?

This template is a starting point. Copy the prompt and change it, or describe a different automation from scratch, with your own conditions, more apps or an AI step. Your assistant writes it, tests it and runs it on ClickRuntime.
  • Your own rules

    Conditions, thresholds, filters and exceptions, written the way you'd explain them to a colleague.

  • Any number of apps

    One automation can read from one app and update several others. Over 248 apps are ready, and anything with an API works too.

  • AI where it helps

    Summaries, classification, extraction and drafts use ClickRuntime's built-in AI, with no extra key.

  • On a schedule or on each event

    Runs as often as every 5 minutes, or the moment an app sends an event, and keeps track of what it already handled.

Changes you can ask for

  • Act only on the items that match your own rules, and skip the rest
  • Change what the AI looks for, or how it writes the result
  • Add another app, for example also send it to Slack
  • Combine it with other Supabase and Telegram steps into one automation

Under the hood

The code that runs this automation

This is the Python script that runs each time the automation starts. Your AI assistant writes it for you when you send the prompt, and you allow each step in your assistant. To change how the automation works, describe the change to your assistant and it updates the script for you. For every function it can use, see the script reference.
telegram_ai_assistant_with_per_user_memory_in_supabase.pyexample
from clickruntime import on_event, credentials, http, ai

PERSONA = "You are the Northwind running club's friendly training assistant. Keep replies short."
SUPABASE = "https://your-project.supabase.co/rest/v1"

def sb(method, path, **kw):
    key = credentials.secret("SUPABASE_KEY")
    r = http.request(method, f"{SUPABASE}/{path}", **kw, headers={"apikey": key, "Authorization": f"Bearer {key}", "Prefer": "return=representation,resolution=merge-duplicates"})
    return r.raise_for_status() or (r.json() if r.content else None)

def tg(method, **params):
    return http.post(f"https://api.telegram.org/bot{credentials.secret('TELEGRAM_BOT_TOKEN')}/{method}", json=params).json()

@on_event
def handle(event):
    msg = (event.json or {}).get("message") or {}
    if not msg.get("text"):
        return {"skipped": True}
    user = msg["from"]
    sb("POST", "users?on_conflict=id", json={"id": user["id"], "name": user.get("first_name")})
    history = sb("GET", f"messages?user_id=eq.{user['id']}&order=created_at.desc&limit=20")[::-1]
    convo = "\n".join(f"{m['role']}: {m['text']}" for m in history)
    reply = ai.generate(f"{convo}\nuser: {msg['text']}\nassistant:", system=f"{PERSONA} The user's name is {user.get('first_name')}.", max_output_tokens=400).text
    tg("sendMessage", chat_id=msg["chat"]["id"], text=reply)
    sb("POST", "messages", json=[{"user_id": user["id"], "role": "user", "text": msg["text"]}, {"user_id": user["id"], "role": "assistant", "text": reply}])
    return {"user": user["id"], "history": len(history)}

FAQ

Common questions

Can I set the bot's personality?
Yes. Give your AI assistant a short description of the bot's role and tone, and it's used for every reply.
How do I delete someone's history?
Delete their rows from the messages table in Supabase. You can also add a /forget command.
What tables does it need?
A users table keyed by Telegram user ID and a messages table with user ID, role, text and time.
What permissions does it need?
Supabase read+write; bot token. Nothing else in your accounts is used.
Does it need AI?
Yes. It uses ClickRuntime's built-in AI, so you don't need an OpenAI or Anthropic key. Every plan includes a monthly AI allowance.
How many runs does it use?
One run for each event the app sends.
Can I change how it works?
Yes. Describe the change to your AI assistant, for example a different schedule, channel or filter. It updates the automation, tests it and tells you what changed.

Set up “Run a Telegram assistant that remembers each user” from your assistant

Copy the prompt above, or connect your assistant first. Free plan, no credit card.