Introduction

Being mainly a Java programmer, I decided however to expand my area of knowledge and the fates fell on the Go language. And the best way to learn is to apply, to practice what you learn! I usually prefer to do this by implementing a game or a tool that has some use.
So this time it’s a game - Wordle, but implemented as a Telegram Bot.

Environment

To run Go code, you need to install Go on your local computer. In my case, I’m using Windows 10, so I installed the latest version of Go, which is currently 1.20.5, from their website. For development, I use Visual Studio Code as my IDE.

Telegram Bot

tbotfather.jpg
I created my Telegram Bot using The Botfather. For more information check the official documentation available here. To create a bot, you need to communicate to The Botfather and request a new bot with the command

/newbot

Wordle Game

The Wordle Game involves guessing a five-letter word chosen by the computer. You have six attempts to guess the word, with feedback given for each guess in the form of colored tiles indicating when letters match or occupy the correct position. These tiles indicate whether the letters match or occupy the correct position. However, due to the nature of Telegram, we can only provide feedback to the user using text characters, such as letters and symbols:

  • Bold letter - indicates the letter is in the correct position.
  • Italic letter - indicates the letter is present but not in the correct position.
  • # - indicates the letter is not present in the word.

Implementation

You can find the game implementation in this GitHub repository https://github.com/prutonis/wordlebot. Some key points to mention are that I used yanzay’s Bot library to communicate with Telegram Bot API. Also I used zap library for logging, the viper for storing and reading configuration files, and GoDotEnv library for reading .env file where the Bot Token is stored.

The entry point of the program is the WordleBot() function in pkg/wordlebot/wbot.go which is called from main.go:

func WordleBot() {
   Chats = make(chats)
   Bot = tbot.New(Token)
   App.client = Bot.Client()
   Bot.HandleMessage("/help", App.helpHandler)
   Bot.HandleMessage("/start", App.startHandler)
   Bot.HandleMessage("/giveup", App.giveUpHandler)
   Bot.HandleMessage("/lang", App.languageHandler)
   Bot.HandleMessage("^.{5}$", App.messagesHandler)
   Bot.HandleCallback(App.callbackHandler)
   log.Fatal(Bot.Start())
}

Play with Docker

If you have access to a Docker instance, you can run the bot within seconds by following these instructions:

  1. Clone the repository
$ git clone https://github.com/prutonis/wordlebot
  1. Navigate to the wordlebot directory and set your Bot token in the .env file:
$ cd wordlebot
$ echo TBOT_TOKEN=4839574812:AAFD39kkdpWt3ywyRZergyOLMaJhac60qc > .env
  1. Build the Docker image
$ docker build -t wordlebot .
  1. Start a Docker container from the created image:
$ docker run --name wordle_game --rm -d -t wordlebot

Now, you can enjoy the game. Simply open your bot in Telegram and type /start or /help to begin.

Features

The help menu of the game:
help.png

You can choose between English and Romanian languages: pick-language.png

An example of gameplay: gamplay.png

You cand try the game with the Wordle bot instance: Wordle Telegram Bot.