Commit 1576906e authored by Otthorn's avatar Otthorn 🤔

added stickers and reaction command to gif

parent 46ce12b3
......@@ -9,14 +9,23 @@ from matrix_bot_api.matrix_bot_api import MatrixBotAPI
from matrix_bot_api.mcommand_handler import MCommandHandler
def gif_search(query, api_key, limit):
def gif_search(query, query_type, api_key, limit):
query = quote_plus(query) # make the query browser compliant
data = json.loads(urlopen(""+query+"&api_key="+api_key+"&limit="+limit).read())
random_id = random.randint(0, int(limit) -1 )
id = data['data'][random_id]['id']
url_gif = ""+id+"/giphy.gif"
data = json.loads(urlopen(""+query_type+"/search?q="+query+"&api_key="+api_key+"&limit="+limit).read())
maximum_id = len(data['data']) - 1
if maximum_id == -1:
return query
random_id = random.randint(0, maximum_id)
print("Search id: {}/{}".format(random_id, maximum_id))
id_url = data['data'][random_id]['id']
url_gif = ""+id_url+"/giphy.gif"
mime = "image/gif" # mime type of the image
......@@ -34,18 +43,35 @@ class MyMatrixBotAPI(MatrixBotAPI):
self.limit = limit
def gif_callback(self, room, event):
def generic_callback(self, query_type, room, event):
args = event['content']['body'].split()
query = " ".join(args[1:])
print("[{}] GIF called with query={} in room={}".format(log_time(), query, room.display_name))
print("[{}] {} called with query={} in room={}".format(log_time(), query_type, query, room.display_name))
data, mime = gif_search(query, self.api_key, self.limit)
result = gif_search(query, "gifs", self.api_key, self.limit)
if type(result) == str:
room.send_text("Désolé, aucun résultat pour {}".format(query))
data, mime = result
mxc_url = self.client.upload(data, mime)
room.send_image(mxc_url, query, mimetype=mime)
def gif_callback(self, room, event):
self.generic_callback("gifs", room, event)
def sticker_callback(self, room, event):
self.generic_callback("stickers", room, event)
def reaction_callback(self, room, event):
self.generic_callback("stickers/packs/reactions", room, event)
def main():
# Load the configuration
......@@ -61,9 +87,15 @@ def main():
bot = MyMatrixBotAPI(username, password, server, api_key, limit)
# Created handler
gif_handler = MCommandHandler("gif", bot.gif_callback)
gif_handler = MCommandHandler("gif", bot.gif_callback)
sticker_handler = MCommandHandler("sticker", bot.sticker_callback)
reaction_handler = MCommandHandler("reaction", bot.reaction_callback)
# Start polling
print("gif_bot started")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment