Skip to content

Instantly share code, notes, and snippets.

@heejune
Created January 26, 2016 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save heejune/c3626f5430d94f5450ad to your computer and use it in GitHub Desktop.
Save heejune/c3626f5430d94f5450ad to your computer and use it in GitHub Desktop.
telegram bot example uses the tesseract to process OCR request
import sys
import asyncio
import random
import telepot
from telepot.delegate import per_chat_id
from telepot.async.delegate import create_open
import os
import traceback
try:
import Image
except ImportError:
from PIL import Image
import pytesseract
class OCRBot(telepot.helper.ChatHandler):
def __init__(self, seed_tuple, timeout):
super(OCRBot, self).__init__(seed_tuple, timeout)
self._lang = 'eng'
@asyncio.coroutine
def on_message(self, msg):
content_type, chat_type, chat_id = telepot.glance2(msg)
if content_type == 'photo':
photo_id = msg['photo'][-1:][0]['file_id']
width = msg['photo'][-1:][0]['width']
height = msg['photo'][-1:][0]['height']
to_save = './downloads/' + photo_id
yield from self.sender.sendMessage('Trying to download the file({}*{}) you sent...'.format(width, height))
yield from bot.downloadFile(photo_id, to_save)
yield from self.sender.sendMessage('Download complete. Start OCR...')
try:
text = pytesseract.image_to_string(Image.open(to_save), lang=self._lang)
except:
yield from self.sender.sendMessage('Failed to process OCR')
traceback.print_exc()
os.remove(to_save)
return;
os.remove(to_save)
yield from self.sender.sendMessage('Done. Removed the temp file. Here is the result:')
yield from self.sender.sendMessage(text)
return
elif content_type == 'text':
commands = msg['text'].strip().lower().split()
if commands[0] == '/getlang' or commands[0] == 'getlang':
yield from self.sender.sendMessage(self._lang)
return;
if commands[0] == '/setlang' or commands[0] == 'setlang':
self._lang = commands[1]
yield from self.sender.sendMessage('current lang setting: %s' % self._lang )
if commands[0] == '/?' or commands[0] == '?':
show_keyboard = {'keyboard': [['getlang', 'setlang kor','setlang eng', 'hidekbd']]}
yield from self.sender.sendMessage('Get help!', reply_markup=show_keyboard)
elif commands[0] == '/hidekbd' or commands[0] == 'hidekbd':
hide_keyboard = {'hide_keyboard': True}
yield from self.sender.sendMessage('Hide helps', reply_markup=hide_keyboard)
TOKEN = '**YOUR TOKEN**'
bot = telepot.async.DelegatorBot(TOKEN, [
(per_chat_id(), create_open(OCRBot, timeout=60)),
])
loop = asyncio.get_event_loop()
loop.create_task(bot.messageLoop())
print('Listening ...')
loop.run_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment