I need to be funny all the time.

Hello everyone!

I am a mobile developer. And I’ve got a problem. I am a drinker. No worries it`s a joke 😄 So you`ve got the idea that today we`re gonna talk about funny stuff. Personally, I need some cool jokes to laugh at every 30 minutes.

So I decided to come up with the program which would send me a cool joke right when I need it. As a result, I created a small sh script which sends a new joke to the command line when I want it. It`s a piece of cake, because https://icanhazdadjoke.com has a public API, for random load joke. It`s not a secret that curl is the best and the most popular command-line program for tranfering data. So this brings us to my first version of SH script.

curl -s -X GET -H "Accept: application/json" https://icanhazdadjoke.com

I didn`t like the idea of executing a command in my terminal each time when I needed the new joke. So I started to look for another, more convenient, way to get some jokes. And I got lucky to come across Telegram. In case you haven`t used it yet, check it https://telegram.org/. It`s really well-organized messenger. So I decided to create a telegram bot which texts on my Telegram channel every 15 minutes. So if you happen to love jokes as much as I do, feel free to join it: https://t.me/hourJokes

It seems super weird because code which does this is easy and small. For instance:

TOKEN=593830940:AAEbr*************8zjwGFtA
CHAT_ID=”@hourJokes
URL=”https://api.telegram.org/bot$TOKEN/sendMessage"
while [[ true ]]; do
RESPONSE=$(curl -s -X GET -H “Accept: application/json” https://icanhazdadjoke.com)
JOKE=$(echo $RESPONSE | jq -r ‘.joke’)
 curl -s -X POST $URL -d chat_id=$CHAT_ID -d text=”$JOKE”
sleep 900
done

First curl call loads random joke. But the rest API returns joke in JSON format. Like following:

{
"id": "qjbF6E6wHlb",
"joke": “Why did the belt go to prison? He held up a pair of pants!",
"status": 200
}

We`re using jq for getting a joke from JSON. The second curl call posts a new joke in my telegram public channel. After this step program stops for 15 minutes. That`s it. Seriously, no kidding. All it takes to make this magic real is my code from this article and the telegram bot.
Oh, in case you don`t know how to create Telegram bot, don`t worry, cause Telegram has the bot for creating bots. Yeah, I`m not joking, we use the bot for creating another bot :). The bot is called @BotFather. To create a new bot use the following command `/newbot`. After that type the name for your bot. That`s it, you created a new bot for Telegram. BotFather`s gonna send you API token for your bot which may be used to send a message to Telegram.

That`s all for today. Don’t forget to join my telegram channel: https://t.me/hourJokes And get ready for some awesome jokes.

Thanks, Pavel Durov and your brother for Telegram and Telegram API it is very cool.

At the moment this bot host on the Digital Ocean. And you can read my article about VPN when I use DO too.

View at Medium.com

1 thought on “I need to be funny all the time.”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s