Не даёт ввести пользователю значение, а сразу обновляет и заносит команду, которую юзер использует для активации функции в качестве введенного значение.
import json
import requests
TOKEN = “” # Replace with your bot token
points = 0
task_list =
def add_task(chat_id):
message = “Type the task you want to add to your list:”
send_message(chat_id, message)
# Wait for response
while True:
updates = get_updates()
if len(updates) > 0:
task = updates
task_list.append({“task”: task, “completed”: False})
message = “Task added: {}”.format(task)
send_message(chat_id, message)
break
def print_tasks(chat_id):
if len(task_list) == 0:
message = “No tasks added yet!”
send_message(chat_id, message)
else:
for i, task in enumerate(task_list):
status = “Incomplete”
if task:
status = “Complete (+1 point)”
message = “{}) {} - {}”.format(i+1, task, status)
send_message(chat_id, message)
def mark_complete(chat_id):
message = “Enter task number to mark as complete:”
send_message(chat_id, message)
# Wait for response
while True:
updates = get_updates()
if len(updates) > 0:
task_num = int(updates)
task_list = True
global points
points += 1
message = “Task marked as complete, you now have {} points!”.format(points)
send_message(chat_id, message)
break
def view_outstanding(chat_id):
incomplete_tasks = [task for task in task_list if not task]
if len(incomplete_tasks) == 0:
message = “All tasks completed!”
else:
message = “Outstanding tasks:\n”
for i, task in enumerate(incomplete_tasks):
message += “{}) {}\n”.format(i+1, task)
send_message(chat_id, message)
def view_points(chat_id):
message = “You have earned {} points!”.format(points)
send_message(chat_id, message)
def save_data():
with open(“task_list.json”, “w”) as f:
data = {“points”: points, “tasks”: task_list}
json.dump(data, f)
print(“Data saved successfully!”)
def get_updates():
url = "https://api.telegram.org/bot{}/getUpdates".format(TOKEN)
response = requests.get(url)
data = response.json()
updates = data
return updates
def send_message(chat_id, message):
url = "https://api.telegram.org/bot{}/sendMessage“.format(TOKEN)
params = {”chat_id“: chat_id, ”text“: message}
response = requests.post(url, data=params)
print(response.status_code, response.content)
# Try to load saved data, if it exists
try:
with open(”task_list.json“, ”r") as f:
data = json.load(f
)
points = data
task_list = data
print(“Data loaded successfully!”)
except:
print(“No saved data found.”)
# Main loop
while True:
updates = get_updates()
if len(updates) > 0:
last_update_id = updates
# Process each message since last update
for update in updates:
message = update
chat_id = update
if message == “/start”:
send_message(chat_id, “Welcome to the Task Manager bot!”)
elif message == “/add”:
add_task(chat_id)
elif message == “/view”:
print_tasks(chat_id)
elif message == “/complete”:
mark_complete(chat_id)
elif message == “/points”:
view_points(chat_id)
elif message == “/outstanding”:
view_outstanding(chat_id)
# Mark last update as read
url = "https://api.telegram.org/bot{}/getUpdates?offset={}".format(TOKEN, last_update_id+1)
requests.get(url)