The bot I mentioned on this blog is ready to use btw: https://t.me/cbk_forever_bot
I was planning on develop a simple tracking Telegram bot recently. When it comes to “HOW” to construct this bot, Shuttle.rs just poped up and it saved me so much time.
I’ve heard about it once before, but didn’t left many in my brain at that time. But it offers free project deployment, this is the main reason it becomes the new choice.
After picked it up and checked the Quick-Start Guide, I deployed a very simple http service online, everyone can access to it. I do not need to think about what vps provide to use, the IP/domain binding, service environment setting, and other painful job.
The whole project tooks me within 5 minutes. How amazing.
Shuttle does not limit how you program your Rust project. You can still use your favorite crates. It provides very good support for Axum, Rocket, sqlx, and many other popular crates: https://github.com/shuttle-hq/shuttle-examples
When you need a specific resource, for example, a Postgres db, you can just add a param in your main entry:
use sqlx::{PgPool};
#[shuttle_runtime::main]
async fn main(
// This is how you request and setup a pg resource
#[shuttle_shared_db::Postgres] pool: PgPool,
) -> Result<BotService, shuttle_runtime::Error>And it’s done. You do not need to care the db url/pass, config, deployment, blah blah. Just use it and RW your db. You can also pass it as you want.
Have docker installed first. You do not need to deploy a WIP project, use:
cargo-shuttle run --releaseShuttle will helps you setting up neccessary resources using docker. This is also helpful if you just do not want to deploy services in your own env outside shuttle.
So, I got my bot running, the response is fast, and I don’t need to care about maintainance stuffs.
Great, huh?
But something strange happened and confused me: the bot will die after a period. It won’t work, so I logged in to shuttle’s console, checking the project status, it says the project status is “stopped”. Okay, maybe it crashed? But still weird, the project is supposed to restart automatically.
What is more weird, when I clicked in the project detail page, it will turns into a normal “Running” status.
use cargo-shuttle project start will return an error says “project is already running”, and the bot is alive again. Why?
After searching the doc, this explains why: Idle Project - Shuttle
So Shuttle will “Sleep” your project instance, if it does not have incoming http request or in a low CPU load after 30 minutes. It’s reasonable, and mostly suitable for webhook services.
But our bot can’t do like that! We need it keeps polling message updates! Shuttle also kindly reminds you:
Important: If a Discord bot is sleeping due to inactivity, calling the bot commands will not wake it, only developer interaction through the CLI will (but traffic will keep it from going to sleep). See the below instructions for how to increase the idle limit.
Our Telegram bot is also the same. It does not have strict idle limits on free plan, so we can just:
cargo-shuttle restart --idle-minutes 0And our bot will live forever!
Use cargo shuttle project status to check current idle settings:
Project "cbk-forerver" is ready
Idle minutes: 0It’s good, and it’s free, which is also good. Consider it for your next personal tiny projects.
We need to know large-scale stragedy isn’t for anything.Keep It Simple, Stupid.