Cloud-based Markov chain bot for Telegram Messenger
markov_bot
is a Telegram bot that builds Markov chains based on user input and uses them to generate pseudorandom messages. While these rarely make sense, their grammar is surprisingly sound, which makes for a lot of fun when the bot is fed enough sentences. Add it to your group and let the Markov chaos reign!
Chains are generated per-chat. Cross-group message generation is not supported, as it quickly leads to completely nonsensical messages. Which is unfun.
Every UPDATE_FREQUENCY
minutes, chains that weren't interacted with for MAX_TIMEDELTA
minutes or more will be serialized, uploaded to Google Drive and subsequently freed from memory. The file will be fetched when needed.
Killing the bot via Ctrl-C will cause it to shut down without saving any changes. If you'd rather keep the chain files, you'll need to send it a SIGTERM signal (example: $ pkill -SIGTERM markov_bot
).
In order to run the program, you'll need to do some prep work:
- Generate a HTTP token to access Telegram API via @BotFather (detailed instructions here). Rememeber to keep it safe!
- Use BotFather's
/setprivacy
and change it toDisable
, otherwise the bot will not be able to read non-command messages - Make a Google Cloud Platform account and create a new project here
- Visit this link, navigate to Credentials and press
+ CREATE CREDENTIALS
. Choose aService account
and give it a friendly name, then skip the permissions dialog viaContinue
. Now click+ CREATE KEY
and choose the JSON format to download your token. Remember to keep it safe! - Copy the e-mail address associated with the service account you've just created
- Go to Dashboard and press
+ ENABLE APIS AND SERVICES
, then select Google Drive API and clickEnable
- Navigate to your Google Drive, create a folder with a unique name, then share it to the e-mail you copied
- Rename
.env_example
to.env
in the GitHub project folder - Replace the value of
HTTP_TOKEN
with the one provided by BotFather - Copy the contents of your Google service account token and convert them to base64 (you can do that here)
- Replace the value of
GDRIVE_CREDENTIALS
with the base64 string - Replace the value of
CHAINDUMP_DIR
with the name of the folder you created in Google Drive - Replace the values of
UPDATE_FREQUENCY
andMAX_TIMEDELTA
with desired values
You're all set! Now the chains generated by the bot will appear in your Google Drive directory.
You might be wondering - why not just store files locally? The answer is: I wanted to host the app on Heroku, but the chain files would be lost every few hours due to ephemeral storage. And why would I want to convert the token to base64 instead of putting the file next to the binary? I didn't want to upload a file that contained a private key to Heroku, I'd rather set it as an environment variable.
The project was tested using Rust 1.40.0 (Stable) on macOS 10.15.2 Catalina.
Any more questions? Feature suggestions? Contact me on Telegram! Pull requests / GitHub issues are greatly appreciated as well!