Author

Alejandro Alcalde

Data Scientist and Computer Scientist. Creator of this blog.

Alejandro Alcalde's posts | Porfolio

I have been wanting for a while to build a telegram bot who did something useful. For example, I created some time ago a telegram channel called CompTrain Individuals to post the crossfit WORKOUT from comptrain.co. but it was somewhat painful since I had to check its website every day, copy the workout and paste it on the telegram channel. So I though, that is a great use case for a telegram bot! In this post I will explain very quickly how I did it, from development to deployment.

Botfather, your bot and your token

Firsts thing first, you need to create your bot and obtain your token via the @botfather bot on telegram. Once you have it, keep it secret.

Intro to the python api

There is a python library that makes it extremely easy and painless to write a telegram bot, it is called python-telegram-bot. Although its very complete, I have only used a tiny part of the library to achieve my requeriments, send a message to a telegram channel at a given time, every day. This is the key function to do it:

  def main():
      token = os.environ["TOKEN"]
      me = os.environ["ME"]

      # Download page
      headers = {
          "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"
      }

      getPage = requests.get("https://comptrain.co/free-programming/", headers=headers)
      getPage.raise_for_status()  # if error it will stop the program

      # Parse text for foods
      soup = bs4.BeautifulSoup(getPage.text, "html.parser")
      mydivs = soup.findAll("div", {"class": "vc_gitem-zone-mini"}, limit=2)[1]
      date = mydivs.h4.get_text()  # .find('h4').getText()
      date = "<strong>{}</strong>".format(date.upper())

      a = mydivs.find_all(["p", "h2"])[2:]
      buff = "{}".format(date)
      for item in a:
          if not item.has_attr("style") or item.name == "h2":
              buff = "%s%s" % (buff, clean_html(item))

      bot = telegram.Bot(token=token)

      logging.info("Sending {}\n\n".format(buff))

      bot.send_message(chat_id=me, text=buff, parse_mode="html")

      logging.info("%s" % buff)

if __name__ == '__main__':
    logging.info('Starting at %s' % datetime.datetime.now())
    schedule.every().day.at('03:00:00').do(main)
    while True:
        # logging.info('Time %s' % datetime.datetime.now())
        schedule.run_pending()
        sleep(30)

Explain a little bit the code

Basically the bot does the following:

Easy!

Deployment

Now the bot is working, we need to deploy it somewhere in order to execute it continuously. After a few hours of searching and testing environments, I found two free hostings, OpenShift and Google Cloud Platform, both of them work like a charm. Right now I am using Google Cloud.

Create a Kubernetes Engine

Final thoughs

That’s all!, hope you enjoy it, if you have any question do not hesitate to comment below.

All the code its on Github, in the repository elbaulp/comptrain-bot

Spot a typo?: Help me fix it by contacting me or commenting below!

Categories: