To Bot or not: Automating boring stuff with Python and building a killer image classification app in the process!
Wouldn't it be great to have a digital assistant do all the boring stuff we don't like to do ourselves?
I have been experimenting with a lot of things and since building a WhatsApp Bot is only limited to big companies, I thought why not go for an something that is more developer friendly.
Getting Started.
Getting the App
To write your telegram bots you would need to install the telegram app on your phone or computer. Its fairly easy just google the platform you are on.
Registering your bot.
Once you have the app setup in ready it's time for you to register your bot. There is a bot called as Bot Father. Now all you have to do is to talk to bot father and tell it that you are going to create a bot. This Bot Father will ask you a few questions and will assign you an API token in the end.
Here are screenshots from my phone of my interaction with the Bot Father.
Note: Please note that I initially named the bot as Calculator but after adding the image classification functionality I renamed it to Genius.
Now that you have your complete brand new API token it's time to get coding.
Lets Code!
Getting all the repositories
To get all the necessary repositories go to command line and do:
pip install telepot
This will install the necessary repositories and now we are ready to write our own code! In a separate python file, say bot.py write this code to get started!
import telepot
import json
import time
BOT_TOKEN = "###### Your token here ######"
bot = telepot.Bot(BOT_TOKEN)
def handle(msg):
flavor = telepot.flavor(msg)
if flavor in ["chat", "normal"]:
content_type, chat_type, chat_id = telepot.glance(msg)
if content_type.lower().strip() == 'text':
bot.sendMessage(chat_id, text="Hi!")
bot.message_loop(handle)
print('Listening...')
while 1:
time.sleep(10)
Ready! Now you have a fully functional app that says, Hi!
But wait! Shouldn't this be a little more useful?
Agreed. The above app is just a proof of concept. Now lets write some serious code to get this app do something really useful. for now we will have this app do some calculations for us that a normal Calculator won't.
pip install simpleeval
Use the library mentioned above to do some serious calculations!
import telepot
import json
import time
from simpleeval import simple_eval
BOT_TOKEN = "###### Your token here ######"
bot = telepot.Bot(BOT_TOKEN)
def handle(msg):
flavor = telepot.flavor(msg)
if flavor in ["chat", "normal"]:
content_type, chat_type, chat_id = telepot.glance(msg)
try:
if content_type.lower().strip() == 'text':
input_text = msg["text"].lower()
if input_text in ['start', '/start']:
bot.sendMessage(chat_id, text="What do you need to calculate?")
else:
bot.sendMessage(chat_id, text=str(simple_eval(input_text)))
except:
bot.sendMessage(chat_id, text="Sorry I didnt get that!")
bot.message_loop(handle)
print('Listening...')
# Keep the program running.
while 1:
time.sleep(10)
Done! This simple calculator app is now ready. You can show this off to your buddies or couple this with a neat library to make something useful!
What next?
Lets build an image recognition app?
Its easier than it sounds. For the sake of demonstration purposes we will use a pre-processed model than to train our own.
For this you will need to get an account with any image recognition API services out there, I used clarifai. They are an incredible company check them out later.
You can setup a totally free account with 5000 searches per month.
Show me some code!
Lets create the supporting directory structure that will hold our image files by size.
mkdir SMALL
mkdir MEDIUM
mkdir BIG
mkdir LARGE
mkdir "EXTRA LARGE"
Now the code!
import json
import time
from os.path import join
from textwrap import dedent
from urllib.request import urlretrieve as download
import requests
import telepot
from clarifai.rest import ClarifaiApp
from simpleeval import simple_eval
CLARIFY_API_KEY = "##### YOUR CLARIFAI KEY HERE #####"
app = ClarifaiApp(api_key=CLARIFY_API_KEY)
model = app.public_models.general_model
BOT_TOKEN = "###### Your token here ######"
FILE_ID_URL = "https://api.telegram.org/bot{bot_token}/getFile?file_id={file_id}"
DOWNLOAD_FILE_URL = "https://api.telegram.org/file/bot{bot_token}/{file_path}"
small, medium, big, large = 9999, 99999, 999999, 9999999
bot = telepot.Bot(BOT_TOKEN)
# All the command and chat handlers
def start(chat_id):
bot.sendMessage(chat_id, text="Hi! I am a Telegram Bot\n""I am capable of doing some serious calculations!!")
def unknown(chat_id, custom_message="Sorry, I didn't understand that command."):
bot.sendMessage(chat_id, text=custom_message)
def help(chat_id):
bot.sendMessage(chat_id, text=dedent("""They call me a Telegram Bot. I can help you do stuff."""))
def settings(chat_id):
bot.sendMessage(chat_id, text="I cannot be configured via any settings yet. Check back soon!")
def calc(chat_id, text):
bot.sendMessage(chat_id, text=str(simple_eval(text)))
def process(chat_id, file_list, folder_name, analyze):# Ignore if list is emptyif not file_list:return
files = []
for file_id in file_list:
try:
resp = requests.get(FILE_ID_URL.format(bot_token=BOT_TOKEN, file_id=file_id))
js = resp.json()
file_path = js['result']['file_path']
download_url = DOWNLOAD_FILE_URL.format(bot_token=BOT_TOKEN, file_path=file_path)
extension = '.' + file_path.split('.')[-1]
file_path = join(folder_name, file_id + extension)
download(download_url, filename=file_path)
files.append(file_path)
except:
print(folder_name, file_id, "Could not be downloaded")
# At least analyze oneif analyze:for file_loc in files:
try:
if analyze:
response = model.predict_by_filename(file_loc)
for c in response['outputs'][0]['data']['concepts'][:5]:
bot.sendMessage(chat_id, text=f"{c['name']}:{c['value'] * 100}%")
return Trueexcept:
print(folder_name, file_loc, "Could not be processed")
return Falseelse:
return True
def handle(msg):
flavor = telepot.flavor(msg)
content_type, chat_type, chat_id = telepot.glance(msg)
print("Normal Message:", content_type, chat_type, chat_id)
if content_type.lower().strip() == 'text':
command = msg["text"].lower()
if command in ["/start", "start"]:
start(chat_id)
elif command in ["/help", 'help']:
help(chat_id)
elif command in ["/settings", "settings"]:
settings(chat_id)
elif command in ["author", "authors"]:
bot.sendMessage(chat_id, text="Hi!\n\nMy name is Vikram Bankar\n""I created this bot as an experiment\n""If you liked this message me @ https://t.me/viksto\n""It would certainly make my day!")
else:
try:
calc(chat_id, command)
except:
unknown(chat_id)
elif content_type.lower().strip() == 'photo':
try:
small_pics, medium_pics, big_pics, large_pics, extra_large = [], [], [], [], []
for item_ in msg['photo']:
item = item_['file_id']
dim = item_['width'] * item_['height']
if dim <= small:
small_pics.append(item)
elif dim <= medium:
medium_pics.append(item)
elif dim <= big:
big_pics.append(item)
elif dim <= large:
large_pics.append(item)
else:
extra_large.append(item)
analyze = True
x = [small_pics, medium_pics, big_pics, large_pics, extra_large][-1::-1]
y = ["SMALL", "MEDIUM", "BIG", "LARGE", "EXTRA LARGE"][-1::-1]
for lists, folders in zip(x, y):
resp = process(chat_id, lists, folders, analyze)
if analyze and resp:
analyze = False
bot.sendMessage(chat_id, text="Thanks for the picture!")
except:
unknown(chat_id, "Could not process that picture")
return ("Message sent")
else:
raise telepot.exception.BadFlavor(msg)
bot.message_loop(handle)
print("Listening ...")
# Keep the program from exiting
while 1:
time.sleep(10)
Identifying my lady friend's sunglasses
Trying to identify a small knife:
Identifying a really really old bronze dinner plate
Conclusion
The ability to talk to your phone like an assistant is incredible. Plus this communication doesn't have to be one way. You can have these apps setup to message you regarding important tasks that are running on your computer.
These tasks can be:
- Monitoring your websites constantly and letting you know in case a website is down
- Monitoring your servers to be reminded if you are running out of space or surge in user requests (possible hack)
- Creating endpoints where people can reach you without revealing sensitive contact information.
Contact
Guys, if you liked this post please follow me on LinkedIn. And please mention in the comments what post you'd like next! Please let me know of what you think of the content, I put a lot of time into it,
As I am a self employed software developer, I am always in a time crunch so I may not be available all the time but I read and try to reply to every comment.
Great article... Thanks for showing how to use API services in telegram bots. Tensorflow.js has also got similar image identifier . I saw one LinkedIn post creating a web app using tensorflow's api
It was a great article Vikram Bankar. I look forward for good stuff from you!