Advertisement
pasholnahuy

Untitled

Nov 11th, 2023
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.92 KB | None | 0 0
  1. import telebot
  2. from telebot import types
  3. import requests
  4. import json
  5. import os
  6.  
  7. api_token = '6899437684:AAHuhona7h1r4kPmQTGe-SRULunPkWEqbg0'
  8. bot = telebot.TeleBot(api_token)
  9.  
  10. d = dict()
  11. @bot.message_handler()
  12. def info(message):
  13.     if (message.text.lower() == 'id'):
  14.         bot.reply_to(message, f'Your ID is {message.from_user.id}')
  15.  
  16. @bot.message_handler(commands = ["start"])
  17. def start(message):
  18.     bot.send_message(message.chat.id, f"Hello, {message.from_user.first_name} {message.from_user.last_name}!")
  19.  
  20. @bot.message_handler(commands=["help"])
  21. # @bot.message_handler(content_types=["photo"])
  22. def help(message):
  23.     markup = types.InlineKeyboardMarkup()
  24.     markup.add(types.InlineKeyboardButton("Посмотреть id проекта", callback_data= 'get_project_id'))
  25.     markup.add(types.InlineKeyboardButton("Удалить проект", callback_data='delete_project'))
  26.     markup.add(types.InlineKeyboardButton("Добавить проект", callback_data='add_project'))
  27.     markup.add(types.InlineKeyboardButton("Изменить имя проекта", callback_data='rename_project'))
  28.  
  29.  
  30.     bot.reply_to(message, "Help information", reply_markup = markup)
  31. @bot.callback_query_handler(func = lambda callback: True)
  32. def callback_message(callback):
  33.     if callback.data == 'get_project_id':
  34.         mesg = bot.send_message(callback.message.chat.id, 'Введите название проекта')
  35.         bot.register_next_step_handler(mesg, get_project_id)
  36.     elif callback.data == 'delete_project':
  37.         mesg = bot.send_message(callback.message.chat.id, 'Введите название проекта')
  38.         bot.register_next_step_handler(mesg, del_proj_id)
  39.     elif callback.data == 'add_project':
  40.         mesg = bot.send_message(callback.message.chat.id, 'Введите название проекта')
  41.         bot.register_next_step_handler(mesg, add_proj)
  42.     elif callback.data == 'rename_project':
  43.         mesg = bot.send_message(callback.message.chat.id, 'Введите старое название проекта')
  44.         bot.register_next_step_handler(mesg, add_proj)
  45. def get_proj_id(message):
  46.     bot.send_message(message.chat.id, f'id проекта {get_project_id(message)}')
  47. def del_proj_id(message):
  48.     delete_project(message)
  49.     bot.send_message(message.chat.id, 'Проект удален')
  50. def add_proj(message):
  51.     add_project(message)
  52.     bot.send_message(message.chat.id, 'Проект добавлен')
  53. def rename_proj(message):
  54.     d[message.chat.id] = message
  55.     mesg = bot.send_message(message.chat.id, 'Введите новое название проекта')
  56.     bot.register_next_step_handler(mesg, add_proj_set_new)
  57. def add_proj_set_new(message):
  58.     rename_project(d[message.chat.id], message)
  59.     bot.send_message(message.chat.id, 'Проект переименован')
  60.  
  61.  
  62. from todoist_api_python.api import TodoistAPI
  63. import json
  64. from datetime import date
  65. import datetime
  66.  
  67. CLIENT_ID = '01fd238f1dca45c0b6a2ffdb3ff9d601'
  68. CLIENT_SECRET = '51376de7dfe946418eff3c149f11cc54'
  69. TEST_TOKEN = '38847922db0efe60a38e91fe73ec190f19f2ecfb'
  70.  
  71. api = TodoistAPI(TEST_TOKEN)
  72. def get_project_id(project_name):
  73.     projects = api.get_projects()
  74.     myprojects = []
  75.     for project in projects:
  76.         if (project.name == project_name):
  77.             myprojects.append(project.id)
  78.     if len(myprojects) <= 0:
  79.         # Not Found
  80.         return None
  81.     if len(myprojects) > 1:
  82.         # too many objects
  83.         return None
  84.     return myprojects[0]
  85.  
  86. def delete_project(name):
  87.     id = get_project_id(name)
  88.     try:
  89.         s = api.delete_project(project_id=id)
  90.     except Exception as error:
  91.         return error
  92.  
  93. def add_project(name, parent_name=None, view_style='list', color= "charcoal"):
  94.     try:
  95.         res = get_projects_names()
  96.         if len(res) >= 7:
  97.             return 'Переполнение. Купите тариф Про.'
  98.         parent_id = get_project_id(parent_name)
  99.         project = api.add_project(name=name, parent_id=parent_id, view_style=view_style, color=color)
  100.         return project.id
  101.     except Exception as error:
  102.         return(error)
  103.  
  104. def get_projects_names(url=False)->list:
  105.     try:
  106.         projects = api.get_projects()
  107.         if (url):
  108.             projects_names = [[project.name, project.url] for project in projects]
  109.         else:
  110.             projects_names = [project.name for project in projects]
  111.         #print(projects_names)
  112.         return projects_names
  113.     except Exception as error:
  114.         return error
  115.  
  116. def rename_project(old_name, new_name):
  117.     try:
  118.         project_id = get_project_id(old_name)
  119.         project = api.update_project(project_id=project_id, name=new_name)
  120.         return project.url
  121.     except Exception as error:
  122.         return error
  123. def get_tasks(project_name=None):
  124.     project_id = get_project_id(project_name)
  125.     tasks = api.get_tasks(project_id=project_id)
  126.     return tasks
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement