Extension_image_recognition/ExtensionBot.py

78 lines
2.8 KiB
Python
Executable File

from telegram import ReplyKeyboardMarkup, KeyboardButton
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ContextTypes
import requests
import json
import telegram
import re
endpoint = "http://bilioso.isti.cnr.it:8190/bcir/searchByURL"
endpoint_rescorer = "http://bilioso.isti.cnr.it:8190/bcir/setRescorer"
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
if (photo_id is None):
print(update.message)
query = context.bot.getFile(photo_id)
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)
params = {'url': query.file_path, 'securityID': 'FA6mMmeWZ8JjWKz5'}
print(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)
'''
def commands(update, context):
text = update.message.text
if text.lower() == 'commands':
chat_id = update.effective_chat.id
markup = ReplyKeyboardMarkup(keyboard=[['GEM', KeyboardButton(text='BEBLID')],["GEM_TH", "BEB_TH"]])
context.bot.sendMessage(chat_id=chat_id, text="available commands", reply_markup=markup, parse_mode=telegram.ParseMode.HTML)
if text == "BEBLID":
params = {'rescorer': 'true'}
elif text == "GEM":
params = {'rescorer': 'false'}
r = requests.get(endpoint_rescorer, params=params)
res = r.text
print(res)
update.message.reply_text(res)
'''
def call_service(query):
print(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(MessageHandler(Filters.text, commands))
updater.start_polling()
updater.idle()