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("http://api.giphy.com/v1/gifs/search?q="+query+"&api_key="+api_key+"&limit="+limit).read())
random_id = random.randint(0, int(limit) -1 )
id = data['data'][random_id]['id']
url_gif = "https://i.giphy.com/media/"+id+"/giphy.gif"
data = json.loads(urlopen("http://api.giphy.com/v1/"+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 = "https://i.giphy.com/media/"+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))
return
else:
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)
return
def sticker_callback(self, room, event):
self.generic_callback("stickers", room, event)
return
def reaction_callback(self, room, event):
self.generic_callback("stickers/packs/reactions", room, event)
return
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)
bot.add_handler(gif_handler)
sticker_handler = MCommandHandler("sticker", bot.sticker_callback)
bot.add_handler(sticker_handler)
reaction_handler = MCommandHandler("reaction", bot.reaction_callback)
bot.add_handler(reaction_handler)
# Start polling
bot.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