This project demonstrates how to build a Telegram bot using Solana Agent Kit and Cloudflare Workers.
- Serverless deployment using Cloudflare Workers
- Blockchain integration with Solana Agent Kit
- Interactive user experience through Telegram bot interface
- Node.js (v18 or higher)
- pnpm package manager
- Cloudflare account
- Telegram account and bot token
- OpenAI API key
- Connect to @BotFather on Telegram
- Send the
/newbot
command to create a new bot - Follow the instructions to set a name and username
- Save the generated token (e.g.,
123456789:ABCDefGhIJKlmNoPQRsTUVwxyZ
)
For more details, check here.
-
Clone the repository
git clone https://github.com/yourusername/sendai-cf-tgbot-sample.git
cd sendai-cf-tgbot-sample
-
Install dependencies
pnpm install
-
Set up environment variables
- Copy
.dev.vars.example
to.dev.vars
- Configure required variables (
TELEGRAM_BOT_TOKEN
,OPENAI_API_KEY
,SOLANA_PRIVATE_KEY
, etc.)
- Copy
-
Start the local development server
pnpm run dev
-
In a separate terminal, expose your local server using ngrok
npx ngrok http 8787
-
Set up the webhook using the URL provided by ngrok
curl https://api.telegram.org/bot<TELEGRAM_BOT_TOKEN>/setWebhook?url=https://<NGROK_URL>/
-
If successful, you'll see this response
{ "ok": true, "result": true, "description": "Webhook was set" }
-
Log in to Cloudflare and access Workers & Pages
-
Set environment variables as Cloudflare secrets
npx wrangler secret put TELEGRAM_BOT_TOKEN npx wrangler secret put OPENAI_API_KEY # Set other required environment variables similarly
-
Deploy your project
pnpm run deploy
-
Set up the webhook using your deployed URL
curl https://api.telegram.org/bot<TELEGRAM_BOT_TOKEN>/setWebhook?url=https://<YOUR_WORKER_URL>/
src/index.ts
: Main application codewrangler.jsonc
: Cloudflare Workers configuration
This project uses Biome for linting and formatting. Biome is a fast formatter and linter for JavaScript and TypeScript.
Available scripts:
pnpm lint
: Run Biome linter on source filespnpm format
: Format source files with Biomepnpm check
: Check and automatically fix issues in source files
VS Code users can install the Biome extension for in-editor linting and formatting.
- Webhook setup fails: Verify that your URL is correct and your Telegram bot token is valid
- Bot doesn't respond: Check logs and ensure environment variables are properly configured
- When you run image generation tasks using an agent developed by Solana Agent Kit, sometimes it becomes a zombie process just keeping timeout and sending image generation tasks to API endlessly, so please be careful.
MIT License
🎉 Congratulations! You now have a Telegram bot running with Solana Agent Kit on Cloudflare Workers.