This directory contains Vercel serverless functions for bot management.
POST /api/bot/create— Create a new bot user and token. Requires a valid Firebase user ID token in the Authorization header.POST /api/bot/sendMessage— Send a message as a bot to a user or group chat. Requires a valid bot token in the Authorization header.POST /api/bot/readMessages— Read the latest messages from a user or group chat as a bot. Requires a valid bot token in the Authorization header. ...
FIREBASE_SERVICE_ACCOUNT_JSON: The contents of your Firebase service account JSON (as a single line, escaped string).FIREBASE_DATABASE_URL: Your Firebase Realtime Database URL.
- Deploy this directory to Vercel.
- Set the
FIREBASE_SERVICE_ACCOUNT_JSONandFIREBASE_DATABASE_URLenvironment variables in your Vercel project settings. - To create a bot, send a POST request to
/api/bot/createwith the following JSON body:And the header:{ "username": "botusername", "name": "Bot Display Name", "pic": "https://example.com/bot.png", "bio": "I'm a helpful bot!" }Authorization: Bearer <USER_ID_TOKEN> - The response will include the bot UID and bot token (show this to the user only once).
- To send a message as a bot, send a POST request to
/api/bot/sendMessagewith the following JSON body:And the header:{ "target": "<userUid or groupId>", "message": "Hello, world!" }Authorization: Bot <BOT_TOKEN> - The endpoint will automatically detect if the target is a group or a direct message and write the message to the appropriate location in the database.
- The response will be:
{ "success": true }
- To read messages as a bot, send a POST request to
/api/bot/readMessageswith the following JSON body:{ "target": "<userUid or groupId>", "limit": 20 }target: The user UID (for DMs) or group ID (for group chats).limit: (Optional) The maximum number of messages to return (default 20). And the header:
Authorization: Bot <BOT_TOKEN> - The endpoint will return the latest messages, sorted oldest to newest:
{ "messages": [ { "id": "msgid1", "sender": "uid", "text": "...", ... }, ... ] }
- The bot token is only shown once at creation. If lost, you must regenerate a new bot.
- Bots can only send/read messages to/from chats and group chats (not posts or comments).
- Only bots with the
chatpermission can use the sendMessage and readMessages endpoints.