Extension_image_recognition/ExtensionBotTest.py

88 lines
3.2 KiB
Python
Executable File

from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import requests
import json
import telegram
import re
endpoint = "http://bilioso.isti.cnr.it:8290/bcirtest/searchByURL"
def start(update, context):
chat_id = update.effective_chat.id
# custom_keyboard = [['top-left', 'top-right'], ['bottom-left', 'bottom-right']]
# reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard)
# context.bot.send_message(chat_id=chat_id, text = "Custom Keyboard Test", reply_markup = reply_markup)
# context.bot.send_message(chat_id=chat_id, text = "Custom Keyboard Test")
photo_id = update.message.photo[-1].file_id
query = context.bot.getFile(photo_id)
params = {'url': query.file_path}
print(query.file_path)
# r = requests.get(endpoint, params=params)
# json_res = json.loads(r.text)
response = "Sorry, I can't recognize it :-("
# if json_res is not None:
# title = re.sub("\\d", "", json_res['title'])
# response = "*" + json_res['author'] + "*, _" + title + "_\n\n"
# response += json_res['artworkPage']
# update.message.reply_text(response)
text = endpoint + '?tohtml=true&url=' + query.file_path
print(text)
context.bot.send_message(chat_id=chat_id, text=text, parse_mode=telegram.ParseMode.HTML)
#-GEM-#
params = {'url': query.file_path}
r = requests.get(endpoint, params=params)
text = json.loads(r.text)
print(text)
context.bot.send_message(chat_id=chat_id, text='GEM', parse_mode=telegram.ParseMode.HTML)
context.bot.send_message(chat_id=chat_id, text=text, parse_mode=telegram.ParseMode.HTML)
#-ORB-#
params = {'url': query.file_path, 'rescorer': 'true', 'lf_impl': 'orb'}
r = requests.get(endpoint, params=params)
text = json.loads(r.text)
print(text)
context.bot.send_message(chat_id=chat_id, text='ORB', parse_mode=telegram.ParseMode.HTML)
context.bot.send_message(chat_id=chat_id, text=text, parse_mode=telegram.ParseMode.HTML)
#-BEBLID-#
params = {'url': query.file_path, 'rescorer': 'true', 'lf_impl': 'beblid'}
r = requests.get(endpoint, params=params)
text = json.loads(r.text)
print(text)
context.bot.send_message(chat_id=chat_id, text='BEBLID', parse_mode=telegram.ParseMode.HTML)
context.bot.send_message(chat_id=chat_id, text=text, parse_mode=telegram.ParseMode.HTML)
# -LATCH-#
params = {'url': query.file_path, 'rescorer': 'true', 'lf_impl': 'latch'}
r = requests.get(endpoint, params=params)
text = json.loads(r.text)
print(text)
context.bot.send_message(chat_id=chat_id, text='LATCH', parse_mode=telegram.ParseMode.HTML)
context.bot.send_message(chat_id=chat_id, text=text, parse_mode=telegram.ParseMode.HTML)
def hello(bot, update):
update.message.reply_text(
'Hello {}'.format(update.message.from_user.first_name))
def call_service(query):
files = {'img': query}
r = requests.post(endpoint, files=files)
return r.text
updater = Updater('2140606085:AAHEBR3a_O9b8E8tbJFaWopDUvT4ptAUkP4')
updater.dispatcher.add_handler(MessageHandler(Filters.photo, start))
updater.dispatcher.add_handler(CommandHandler('hello', hello))
updater.start_polling()
updater.idle()