-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.py
More file actions
69 lines (58 loc) · 2.59 KB
/
main.py
File metadata and controls
69 lines (58 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import requests
from pyrogram import Client, filters
from dotenv import load_dotenv
import os
# Load environment variables from .env file
load_dotenv()
# Get the API ID, API Hash, and Bot Token from the environment variables
api_id = int(os.getenv("API_ID"))
api_hash = os.getenv("API_HASH")
bot_token = os.getenv("BOT_TOKEN")
# Initialize the bot with the token, API ID, and API Hash
app = Client("joke_bot", api_id=api_id, api_hash=api_hash, bot_token=bot_token)
# Command handler for /start
@app.on_message(filters.command("start"))
def start_command(client, message):
message.reply_text("Hello! I am a Joke Bot 🤖. Use /joke to get a random joke, or /help for more options.")
# Command handler for /help
@app.on_message(filters.command("help"))
def help_command(client, message):
message.reply_text("Available Commands:\n"
"/start - Start the bot\n"
"/joke - Get a random joke\n"
"/help - Show this help message")
# Command handler for /joke
@app.on_message(filters.command("joke"))
def joke_command(client, message):
# Fetch a joke from JokeAPI
response = requests.get("https://v2.jokeapi.dev/joke/Any")
if response.status_code == 200:
joke_data = response.json()
if not joke_data.get("error"):
# Create the joke text formatted as a code block
if joke_data.get("type") == "single":
joke_text = f"```\n{joke_data.get('joke')}\n```"
else:
setup = joke_data.get("setup")
delivery = joke_data.get("delivery")
joke_text = f"```\n{setup}\n{delivery}\n```"
# Create a response message with the rest of the fields
details = (
f"Category: {joke_data.get('category')}\n"
f"Type: {joke_data.get('type')}\n"
f"ID: {joke_data.get('id')}\n"
f"Safe: {joke_data.get('safe')}\n"
f"Language: {joke_data.get('lang')}\n"
f"Flags: {', '.join([flag for flag, value in joke_data.get('flags', {}).items() if value]) or 'None'}"
)
# Combine the joke and the details
response_message = f"{joke_text}\n{details}"
# Send the combined response
message.reply_text(response_message)
else:
message.reply_text("Failed to fetch a joke. Please try again.")
else:
message.reply_text("Sorry, I couldn't fetch a joke at the moment. Please try again later!")
# Start the bot
if __name__ == "__main__":
app.run()