Skip to content

Commit

Permalink
added a simple telegram bot
Browse files Browse the repository at this point in the history
  • Loading branch information
wolterlw committed Apr 21, 2018
1 parent d91d156 commit 74e906d
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions telegram_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import telegram
from telegram.ext import Updater
from telegram.ext import CommandHandler
from telegram.ext import MessageHandler, Filters

from PIL import Image
import requests as r

import io
import logging
logging.basicConfig(level=logging.INFO)

credentials = {
"token": open('tg_bot_token','r').read().strip()
}

updater = Updater(token = credentials["token"])
dispatcher = updater.dispatcher

def start(bot, update):
bot.send_message(chat_id=update.message.chat_id,
text="""
Hello, I am a bill recognition bot (WIP)
So far I can only cut bills from images and detect lines of text
!!!DO NOT CROP PHOTOS!!!
The whole idea is to automate it
""")

start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)

def get_photo(bot, update):

photo = max(update.message.photo, key=lambda x: x.height)
print("received: " + str(photo.file_id))


ph_file = bot.get_file(photo.file_id)
ph_file.download("./tmp/unlabelled_ones/" + str(photo.file_id) + ".jpg")
image = open("./tmp/unlabelled_ones/" + str(photo.file_id) + ".jpg",mode='rb').read()

payload = {"image": image}

resp = r.post('http://127.0.0.1:5000/predict', files=payload)

bot.send_photo(chat_id=update.message.chat_id, photo=io.BytesIO(resp.content))


photo_handler = MessageHandler(Filters.photo, get_photo)
dispatcher.add_handler(photo_handler)

updater.start_polling()

0 comments on commit 74e906d

Please sign in to comment.