Skip to content

Cloud-based Markov chain bot for Telegram Messenger

License

Notifications You must be signed in to change notification settings

vtu-dog/markov_bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

markov_bot

Cloud-based Markov chain bot for Telegram Messenger

Overview

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).

Running the project

In order to run the program, you'll need to do some prep work:

  1. Generate a HTTP token to access Telegram API via @BotFather (detailed instructions here). Rememeber to keep it safe!
  2. Use BotFather's /setprivacy and change it to Disable, otherwise the bot will not be able to read non-command messages
  3. Make a Google Cloud Platform account and create a new project here
  4. Visit this link, navigate to Credentials and press + CREATE CREDENTIALS. Choose a Service account and give it a friendly name, then skip the permissions dialog via Continue. Now click + CREATE KEY and choose the JSON format to download your token. Remember to keep it safe!
  5. Copy the e-mail address associated with the service account you've just created
  6. Go to Dashboard and press + ENABLE APIS AND SERVICES, then select Google Drive API and click Enable
  7. Navigate to your Google Drive, create a folder with a unique name, then share it to the e-mail you copied
  8. Rename .env_example to .env in the GitHub project folder
  9. Replace the value of HTTP_TOKEN with the one provided by BotFather
  10. Copy the contents of your Google service account token and convert them to base64 (you can do that here)
  11. Replace the value of GDRIVE_CREDENTIALS with the base64 string
  12. Replace the value of CHAINDUMP_DIR with the name of the folder you created in Google Drive
  13. Replace the values of UPDATE_FREQUENCY and MAX_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.

Additional info

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!

About

Cloud-based Markov chain bot for Telegram Messenger

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages