Skip to content

berkerol/guess-where-telegram-bot

Repository files navigation

Guess Where Telegram Bot

Sonarcloud Status CI contributions welcome license

Telegram bot that lets you guess the location of a random photo.

Overview

You can start by sending /random command to receive a random photo. Then you can enter your guess and send it as a message to receive feedback (true/false). You can also use /filter command to filter with prefixes for intermediate directories, for example for year it will be /filter 2022 2023. Made with python-telegram-bot. Inspired by another game of mine guess-where.

There are 2 scripts that use the Telegram API in 2 different ways.

  • guess_where_telegram_bot.py: This is using the polling method (fetching new messages from Telegram API periodically) with python-telegram-bot library (getUpdates). It needs to be run on a server (e.g. AWS EC2).
  • guess_where_telegram_bot_lambda_function.py: This is used as a webhook that will be called by Telegram to receive new messages. It needs to be run as a function (e.g. AWS Lambda).

Polling (guess_where_telegram_bot.py)

Installation & Usage

pipenv install
pipenv shell # without this you can also do: pipenv run python3 guess_where_telegram_bot.py
python3 guess_where_telegram_bot.py

Prerequisites

Following environment variables have to be set.

  • S3_BUCKET
  • S3_BUCKET_PREFIX
  • AWS_ACCESS_KEY
  • AWS_SECRET_ACCESS_KEY
  • TELEGRAM_BOT_TOKEN

Webhook (guess_where_telegram_bot_lambda_function.py)

Usage

It is using the following AWS services.

  • Lambda to run the Python script
  • DynamoDB to store the last location and current filter
  • S3 to store the current set of paths of photos in a JSON file
  • Systems Manager Parameter Store to store the TELEGRAM_BOT_TOKEN
  • CloudWatch to store the logs

Prerequisites

AWS services need to be set up with Terraform using main.tf.

Telegram webhook needs to be set as following.

POST https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/setWebhook?url={LAMBDA_FUNCTION_URL}

Photo Structure

Photos can only be read if they are structured in a specific way.

Directories by city as last directory

{PATH}/{COUNTRY}/{CITY}/**/{PHOTO}.jpg

City or country directory name should be guessed. {COUNTRY} DOESN'T have to be country, it can be anything. It is just my current setup. ** in between can contain any number of directories from 0 to N.

Directory by city as last element

{PATH}/{DATE} - {ORDER} - {COUNTRY} - {CITY}/**/{PHOTO}.jpg

City or country name should be guessed. {DATE} - {ORDER} - {COUNTRY} part DOESN'T have to be in that structure as long as elements are separated with - and city is the last element. It is just my current setup. ** in between can contain any number of directories from 0 to N.

Photo Source

Photos can be read from S3 bucket.

S3 Bucket

Use with or without prefix, for example s3://{BUCKET} or s3://{BUCKET}/{PREFIX}/

There must be at least one directory between bucket and photo directories in your structure, for example s3://{BUCKET}/Photos/{COUNTRY}/{CITY}/**/{PHOTO}.jpg or s3://{BUCKET}/Photos/2022/{DATE} - {ORDER} - {COUNTRY} - {CITY}/**/{PHOTO}.jpg

References

Continous Integration

It is setup using GitHub Actions in lint job in .github/workflows/lint_and_deploy.yml

Continuous Delivery

guess_where_telegram_bot_lambda_function.py is deployed to AWS Lambda automatically using GitHub Actions in deploy job in .github/workflows/lint_and_deploy.yml

Contribution

Feel free to contribute.

Distribution

You can distribute this software freely under GNU GPL v3.0.

About

Telegram bot that lets you guess the location of a random photo

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published