Skip to content

Instantly share code, notes, and snippets.

@zachlim98
Created October 25, 2020 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachlim98/f76cfb08d26e18d4858629d6570ad2de to your computer and use it in GitHub Desktop.
Save zachlim98/f76cfb08d26e18d4858629d6570ad2de to your computer and use it in GitHub Desktop.
title categories comments
Asking a girl out with a telegram bot
coding
true

Introduction

So, when I first mooted this idea to my colleagues, they were not exactly the most impressed. "Zach," these wise, married 30-year olds said, "she's not going to be impressed with a telegram bot... that's literally the lamest, most geeky idea in the world...if you write the bot in R" And with that challenge, I HAD to attempt to use Python to write it.

Having been working with R for the entire summer and not having touched Python in ages, it felt nasty using = instead of the ever so beautiful <-. The loss of %>% was also a pretty sad thing. (what I did not realize at this juncture, was that I wouldn't even have to use these because my bot would truly be one of the lamest bots around).

Steps:

  1. Request a new bot to be birthed from the BotFather
  2. Plan out my bot
  3. Write some code to get the bot running
    1. Send and receive messages
    2. Send stickers
    3. Send a location
    4. Send a random number generator (RNG) dartboard
  4. Cry because I get rejected

The Bot Begins

Step 1: The birth of a new bot

This step was relatively simple. All you had to do was create a telegram account and then initiate a chat with @BotFather. Then, send him a /newbot, give your bot a name and a username (that has to end in _bot) and he'll give birth to your bot and give your bot a token (not of appreciation; just a token)

Botfather

Take care of your token because, as the BotFather says, anyone with the token can access your bot!

Step 2: Planning out the bot

This turned out to be the actual hardest part of the entire project (and there was absolutely no coding involved in this). I had to spend time thinking about what I wanted the bot to do and how I was to incorporate it into the process of asking her out. This really wasn't easy so I went to sketch out a diagram (the only reason I'm including this here because I wanted to show off typora, this amazing markdown editor which allows you to create diagrams super elegantly and simply)

https://gist.github.com/ad09a59ed2458039cfa8b29ce3c9932d

diag1

Was basically what I had in my head. But then I realized that I also needed to design the telegram bot (I mean, it couldn't just be a literal:

opens up telegram bot

"Will you get together with me?"

right..?)

So back to more flowcharts...

https://gist.github.com/5d137b50da37ac70dec82db3e99f2817

diag2

I obviously needed the flowchart to help me craft out such an intricate plan. But at least I was clear now! I needed to make the bot send her our dinner location, send her a cute telegram sticker, and then send her a (RIGGED) RNG to trick her into agreeing to get together.

Step 3: Coding the bot

This was honestly the simplest part of the entire project. There's a fantastic telegram wrapper that has fantastic documentation and code examples which allowed me to more or less copy borrow what I needed for this bot.

The first step was to import the telegram library and enable logging on the bot. This turned out to be super useful later on (when I struggled to send the stupid sticker and couldn't figure out what the issue was)

https://gist.github.com/ee1156fc93d03c2f1549b21e02aa4c85

After the standard (template) headers, it was time to start defining some functions for our bot. The first two standard ones that one has to define is /start and /help. Which is exactly what I did.

https://gist.github.com/15b5c081f71748e24f042304f8531880

I added the action=telegram.ChatAction.TYPING to create more realism. With this, the bot would pretend to type (wow so cool) before sending the message.

I then added the standard template bits below to get the bot running. We needed a dispatcher to send and receive from the telegram API and then needed to add CommandHandlers to respond to the incoming telegram commands.

https://gist.github.com/1976f7f2ac479f0af67747387ae06d74

And it was live.

alivebot

Now came the tricky bit, sending her a sticker! I really struggled with this one for awhile because I could not for the life of me figure out how to identify the sticker - each telegram sticker has a unique ID. I tried using the "Sticker ID bots" on telegram but they kept giving me incorrect IDs. I eventually figured out that the easiest way to do it was to run the bot, send yourself a sticker and then check the log for the sticker ID.

In order to check the log, I stopped the bot and then headed over to: https://api.telegram.org/botYOURTOKENHERE/getUpdates (so to clarify, it's /bot[yourBotToken]/getUpdates) and sent myself a sticker.

stickid

(yes, the sticker pack I sent it from was indeed beymax kmn). The file_id is (incidentally) the id of the sticker, and to send the sticker, we just had to create the sticker function:

https://gist.github.com/e2fd8efe60459bb01cfba4dcdb3dde80

and add it in as a command:

https://gist.github.com/a6a07d38e9d9017b33fb45073d61a098

image

Next, we needed to send her our dinner location, which was easily done with reply_location. Note that the location has to be given in lat, long (which you can easily get from Google Maps)

https://gist.github.com/dcf7bcf44cf70f04fefdf42384084c78

And finally, the RNG (which I rigged to force her to agree to getting together with me). You could choose between three different emojis (dice, dartboard, and basketball) and by setting the "value", it would pre-determine what the outcome was. For dartboards, setting it to '6' meant that it would hit the BULLSEYE

https://gist.github.com/8399f561a3e3efadb094a9b2799db64f

Throw all these into CommandHandlers... (its '24' for one of the commands because I made her do some sort of quiz before finding out the dinner place and '24' was the correct answer to the quiz)

https://gist.github.com/91763561aa0e506602ade27928ed2e58

And presto~ the bot is DONE!

image

Conclusion: Getting to the BOTtom of it

So, the clear question on everyone's mind is... did it work? Can I impress potential partners by doing nerdy things like coding a telegram bot? Does lying to a potential partner BUT then sending them a telegram bot make up for the lying? The answer, I guess, is for me to know and for you to (now that you have the rough skeleton of how to code a telegram bot) find out :p

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment