diff --git a/README.md b/README.md index 82e6c67..7eab515 100644 --- a/README.md +++ b/README.md @@ -9,12 +9,8 @@ Contact BotFather on Telegram (https://telegram.me/botfather) via private messag This token is used to authenticate requests to the bot API. -### Run docker container -Automated build on Docker Hub is available. - -``` -docker run -i --name googleimagebot stanislavb/telegram-googleimagebot 110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw -``` +### Insert your API key and Custom Search ID +edit the api.py and copy your API key and Custom Search ID into the variables on top and build the container or start it without docker ### Build docker image by yourself Handy instructions are in the Makefile. @@ -23,3 +19,21 @@ Handy instructions are in the Makefile. make build TOKEN=110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw make run ``` + +### Without docker + +start the bot.py with your Token: + +``` +$ bot.py 110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw +``` + +### Usage + +Add the bot to a telegram group or write a message to him with + +``` +/img query +``` + +The Google Custom Search API is limited to 100 search queries per day. diff --git a/api.py b/api.py index f1ded9a..4bcf69b 100644 --- a/api.py +++ b/api.py @@ -4,6 +4,8 @@ import random logger = logging.getLogger() +ApiKey = '' # insert your Google-API key here +CustomSearch = '' #insert your cx ID here # Surpress warnings 'Unverified HTTPS request is being made" requests.packages.urllib3.disable_warnings() @@ -45,8 +47,12 @@ def request(self, method, endpoint, **kwargs): content = super(TelegramAPI, self).request(method, endpoint, **kwargs) if not content['ok']: logger.error('API returned error: {}'.format(content['description'])) - return content['result'] - + try: + return content['items'] + except KeyError as e: + logger.error('Invalid or no search result') + return None + def get_me(self): return self.get('getMe') @@ -63,15 +69,15 @@ def send_photo(self, chat_id, photo, **kwargs): class GoogleImagesAPI(API): def __init__(self): - self.url = 'https://ajax.googleapis.com/ajax/services/search/' + self.url = 'https://www.googleapis.com/customsearch/' def request(self, method, endpoint, **kwargs): content = super(GoogleImagesAPI, self).request(method, endpoint, **kwargs) - return content['responseData']['results'] + return content['items'] def images(self, query, results=8): assert 1 <= results <= 8 - return self.get('images', v='1.0', rsz=results, q=query) + return self.get('v1', key=ApiKey, cx=CustomSearch, num=results, q=query, alt='json', searchType='image') def random_image(self, query, results=8): results = self.images(query, results) diff --git a/bot.py b/bot.py index a021f94..8cb26fe 100755 --- a/bot.py +++ b/bot.py @@ -25,7 +25,7 @@ def __init__(self, token): # Define valid commands self.commands = { - 'image': self.image + 'img': self.image } def image(self, text): @@ -35,7 +35,7 @@ def image(self, text): if image is None: found = "nothing" else: - found = image['url'] + found = image['link'] returntext = ('Searched for {}, found {}'.format(text, found)) return returntext @@ -83,7 +83,7 @@ def respond(self, message): bot = TelegramBot(token=args.token) offset = args.offset if args.offset else 0 - wait = args.wait if args.wait else 15 + wait = args.wait if args.wait else 5 while True: logger.info('Waiting {} seconds'.format(wait)) time.sleep(wait)