Skip to content

Instantly share code, notes, and snippets.

@iAbadia
Created September 21, 2017 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iAbadia/2a00f27db90152a7f137994ef2d517c6 to your computer and use it in GitHub Desktop.
Save iAbadia/2a00f27db90152a7f137994ef2d517c6 to your computer and use it in GitHub Desktop.
query function for telegram bot
def query(self):
url = GIPHY_API + GIPHY_RAND_ENDP + '?'
# Check for API key
if not self.api_key:
logger.warn('GiphyRandomRequest<query()>: No API key provided')
return
# Add API key
url += 'api_key=' + self.api_key + '&'
# Add tag if provided
if self.tag:
url += 'tag=' + self.tag + '&'
# Add rating if provided
if self.rating:
url += 'rating=' + self.rating + '&'
# Indicate JSON
url += 'fmt=json'
# Do the query
try:
# Get GIF url
response = urllib.urlopen(url)
self.gif_json = json.loads(response.read())
except:
# Some error happended, can's send GIF
logger.error('GiphyRandomRequest<query>: Error ocurred while requesting GIF.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment