-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
106 lines (80 loc) · 2.73 KB
/
Copy pathscript.py
File metadata and controls
106 lines (80 loc) · 2.73 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import time
import requests
import telebot
import threading
import os
with open('bot_token.txt', 'r') as file:
token = file.read().strip()
BOT_TOKEN = token
DEFAULT_INTERVAL = 3600 # in seconds
interval = DEFAULT_INTERVAL
is_running = False
timer = None # Add this line to define timer globally
bot = telebot.TeleBot(BOT_TOKEN)
def get_random_meme():
try:
url = 'https://meme-api.com/gimme/ProgrammerHumor'
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return data['url']
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
return None
def set_interval(message, minutes: int, firstCall=False):
global interval, timer
interval = minutes * 60
chat_id = message.chat.id # Store the chat_id from the message
if not firstCall:
bot.reply_to(message,
f'Successfully set the interval to {minutes} minutes.')
# Reset the timer with the new interval
if timer is not None and timer.is_alive():
timer.cancel()
timer = threading.Timer(interval, send_meme,
args=(chat_id, )) # Pass the chat_id as an argument
timer.start()
def send_meme(chat_id):
meme_url = get_random_meme()
if meme_url:
bot.send_photo(chat_id=chat_id, photo=meme_url)
else:
bot.send_message(chat_id=chat_id,
text='Failed to fetch a meme. Try again later.')
def meme_sending_loop(message):
global is_running, interval
while is_running:
send_meme(message.chat.id)
time.sleep(interval)
@bot.message_handler(commands=['stop'])
def handle_stop_command(message):
global is_running
if is_running:
is_running = False
bot.reply_to(message, 'Stopping send memes')
else:
bot.reply_to(message, 'Bot is not running!')
@bot.message_handler(commands=['meme'])
def handle_meme_command(message):
send_meme(message.chat.id)
@bot.message_handler(commands=['set_interval'])
def handle_set_interval_command(message):
try:
minutes = int(message.text.split()[1])
set_interval(message, minutes) # Pass 'message' to the function
except (IndexError, ValueError):
bot.reply_to(message, 'Usage: /setinterval <minutes>')
@bot.message_handler(commands=['start'])
def handle_start_command(message):
global is_running
if not is_running:
is_running = True
bot.reply_to(message, 'Bot started! Sending memes every hour.')
set_interval(message, interval // 60, True) # Set the initial interval
# Start the meme_sending_loop in a new thread
meme_loop_thread = threading.Thread(target=meme_sending_loop,
args=(message, ))
meme_loop_thread.start()
else:
bot.reply_to(message, 'Bot is already running!')
bot.polling(none_stop=True)