Infermatic output cleanup. Added dictionary and joke plugins. Updated Readme.
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
"""
|
||||
Plugin for fetching jokes from the Official Joke API.
|
||||
"""
|
||||
# plugins/joke.py
|
||||
|
||||
import logging
|
||||
import simplematrixbotlib as botlib
|
||||
import aiohttp
|
||||
|
||||
async def handle_command(room, message, bot, prefix, config):
|
||||
"""
|
||||
Function to handle the !joke command.
|
||||
|
||||
Args:
|
||||
room (Room): The Matrix room where the command was invoked.
|
||||
message (RoomMessage): The message object containing the command.
|
||||
bot (Bot): The bot object.
|
||||
prefix (str): The command prefix.
|
||||
config (dict): Configuration parameters.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
match = botlib.MessageMatch(room, message, bot, prefix)
|
||||
|
||||
# Handle !joke command
|
||||
if match.is_not_from_this_bot() and match.prefix() and match.command("joke"):
|
||||
args = match.args()
|
||||
|
||||
# Check if user wants a specific category
|
||||
category = "general"
|
||||
if args:
|
||||
category = args[0].lower()
|
||||
if category not in ["general", "programming"]:
|
||||
# If invalid category, use general
|
||||
category = "general"
|
||||
|
||||
logging.info(f"Fetching {category} joke")
|
||||
|
||||
try:
|
||||
# Fetch joke from API
|
||||
if category == "programming":
|
||||
url = "https://official-joke-api.appspot.com/jokes/programming/random"
|
||||
else:
|
||||
url = "https://official-joke-api.appspot.com/random_joke"
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url) as response:
|
||||
if response.status == 200:
|
||||
data = await response.json()
|
||||
|
||||
# Handle different response formats
|
||||
if isinstance(data, list) and len(data) > 0:
|
||||
joke = data[0]
|
||||
elif isinstance(data, dict):
|
||||
joke = data
|
||||
else:
|
||||
await bot.api.send_text_message(room.room_id, "Sorry, couldn't fetch a joke right now.")
|
||||
return
|
||||
|
||||
# Extract joke parts
|
||||
setup = joke.get("setup", "No setup available")
|
||||
punchline = joke.get("punchline", "No punchline available")
|
||||
|
||||
# Send the joke with a delay for better effect
|
||||
await bot.api.send_text_message(room.room_id, setup)
|
||||
# Add a small delay before the punchline for comedic timing
|
||||
import asyncio
|
||||
await asyncio.sleep(2)
|
||||
await bot.api.send_text_message(room.room_id, f"... {punchline}")
|
||||
else:
|
||||
await bot.api.send_text_message(room.room_id, "Sorry, couldn't fetch a joke right now. Try again later.")
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Error fetching joke: {e}")
|
||||
await bot.api.send_text_message(room.room_id, f"Error fetching joke: {str(e)}")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Plugin Metadata
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
__version__ = "1.0.0"
|
||||
__author__ = "Funguy Bot"
|
||||
__description__ = "Get random jokes from the Official Joke API"
|
||||
__help__ = """
|
||||
<details>
|
||||
<summary><strong>!joke</strong> – Random jokes</summary>
|
||||
<p>Get random jokes from the Official Joke API.<br>
|
||||
Usage: <code>!joke</code> for a general joke<br>
|
||||
Usage: <code>!joke programming</code> for a programming joke</p>
|
||||
</details>
|
||||
"""
|
||||
Reference in New Issue
Block a user