Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
16 changes: 11 additions & 5 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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')

Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, token):

# Define valid commands
self.commands = {
'image': self.image
'img': self.image
}

def image(self, text):
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down