Giphy
A bot that displays a gif from the Giphy API depending on a search term
https://github.com/meya-ai/community/tree/master/examples/giphybot
Notable concepts:
- Custom
component
with an API call
states:
get_tag:
component: meya.input_string
properties:
text: "What sort of gif?"
output: tag
output_gif:
component: giphy
This flow
asks the user what type of gif they'd like to see and outputs a Giphy gif using the Giphy API.
The giphy component
reads the datastore to get the type of gif the user specified and creates a message with the Giphy-supplied search result.
from meya import Component
import requests
API_KEY = "dc6zaTOxFJmzC"
API_URL = "http://api.giphy.com/v1/gifs/random?tag={tag}&api_key={key}"
class Giphy(Component):
"""Outputs a random giphy url based on passed in tag as either
a property or flow db"""
def start(self):
tag = self.properties.get('tag') or \
self.db.flow.get('tag') or "funny"
response = requests.get(API_URL.format(tag=tag, key=API_KEY))
gif = response.json()['data']['url']
message = self.create_message(text=gif)
return self.respond(message=message, action="next")
Why did you import requests?
Requests is a python dependency that is available to every component. See the full list of dependencies here.
Updated over 6 years ago