banner

Imagebot

A Telegram bot for searching image

Demo

Register a bot on Telegram

  1. Open Telegram
  2. Search @ botfather
  3. Type /newbot .
  4. It will show “Alright, a new bot. How are we going to call it? Please choose a name for your bot.”
  5. Type the name of your bot.
  6. you can access your botusing such url https://telegram.me/YOUR_BOT_USERNAME
  7. your token should seems like this: 752713328:AAFQYLO6CJJnrwKnfo9MSqZUjCiue5XYYrw

Requirements

  • requestsfor handlying api
  • python-telegram-bot library to acccess telegram apis

Importing required libraries

from telegram.ext import Updater, InlineQueryHandler, CommandHandler,MessageHandler,Filters
import requests

Get Tokens

token = 'xxxxxxxxxxxxxxxxxxxxxx'
YOUR_ACCESS_KEY = "xxxxxxxxxxxxxxxxxxxxxxx"

There are multiple image search apis available, I will be using unsplash api here

search_api = "https://api.unsplash.com/search/photos"

Write a function that with take search keyword as input and will return image url

def get_img_url(userinput):
    result = requests.get(search_api+"/?query="+userinput+"&client_id=" + YOUR_ACCESS_KEY).json()
    print("url",result['results'][0]['urls']['small'])
    return result['results'][0]['urls']['small']

write a function that will respond to user’s message or search keyword

def reply(update, context):
    # get user's message string
    user_input = update.message.text
    
    # get user's chat id
    chat_id = update.message.chat_id
    print(user_input)
    
    # get image url from api
    url = get_img_url(user_input)
    print(url)
    
    # Reply or send  msg / image url to user
    update.message.reply_text("search keyword: "+user_input+"\nURL:"+url)

Write main function

def main():
    # pass the bot token
    updater = Updater(token, use_context=True)
    dp = updater.dispatcher
    dp.add_handler(MessageHandler(Filters.text, reply))
    updater.start_polling()
    updater.idle()

Execution

python imagebot.py

References