pasholnahuy

Untitled

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