Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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