anaf_bot2.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. '''Created 19 Apr 2021 deeejas'''
  2. import os
  3. import configparser
  4. from collections import Counter
  5. from datetime import (
  6. date,
  7. timedelta
  8. )
  9. import telegram
  10. import gspread
  11. from gspread.models import Worksheet as sheet # @UnusedImport
  12. from google.oauth2.service_account import Credentials
  13. from telegram.ext import CommandHandler
  14. from telegram.ext import Updater
  15. json_ = filter(lambda js: js.endswith('.json'), os.listdir())
  16. json_ = list(json_)[0]
  17. config = configparser.ConfigParser()
  18. config.read('bot')
  19. substr = 'Nu exist'
  20. substr2 = 'inca nedepusa'
  21. bot = telegram.Bot(token=config['AUTH']['token2'])
  22. scope = ['https://spreadsheets.google.com/feeds',
  23. 'https://www.googleapis.com/auth/drive']
  24. creds = Credentials.from_service_account_file(json_, scopes=scope)
  25. gsheet = gspread.authorize(creds)
  26. sht = gsheet.open('Contabilitate')
  27. # print(bot.getMe())
  28. # bot.send_message(chat_id=config['AUTH']['chatid'], text=text)
  29. def get_period():
  30. current_date = date.today()
  31. days_back = timedelta(days=30)
  32. period = current_date - days_back
  33. return period.strftime('%m')
  34. def D_300(update, context):
  35. sheet = sht.worksheet(f'300 {get_period()} 2021')
  36. messages = sheet.batch_get(['J3:L100'], major_dimension='COLUMNS')[0]
  37. D300 = messages[0]
  38. for msg in D300:
  39. if substr in msg:
  40. D300.insert(0, substr)
  41. D300.pop(D300.index(msg))
  42. c = Counter(D300)
  43. if c[substr2] > 0:
  44. text300 = f'You have to send {c[substr2]} more declarations of type D300'
  45. else:
  46. text300 = f'Congrats you have {c[substr2]} declarations to send of type D300'
  47. context.bot.send_message(chat_id=update.effective_chat.id, text=text300)
  48. def D_390(update, context):
  49. sheet = sht.worksheet(f'300 {get_period()} 2021')
  50. messages = sheet.batch_get(['J3:L100'], major_dimension='COLUMNS')[0]
  51. D390 = messages[1]
  52. for msg in D390:
  53. if substr in msg:
  54. D390.insert(0, substr)
  55. D390.pop(D390.index(msg))
  56. c = Counter(D390)
  57. if c[substr2] > 0:
  58. text390 = f'You have to send {c[substr2]} more declarations of type D390'
  59. else:
  60. text390 = f'Congrats you have {c[substr2]} declarations to send of type D390'
  61. context.bot.send_message(chat_id=update.effective_chat.id, text=text390)
  62. # print(update.effective_chat.id)
  63. def D_394(update, context):
  64. sheet = sht.worksheet(f'300 {get_period()} 2021')
  65. messages = sheet.batch_get(['J3:L100'], major_dimension='COLUMNS')[0]
  66. D394 = messages[2]
  67. for msg in D394:
  68. if substr in msg:
  69. D394.insert(0, substr)
  70. D394.pop(D394.index(msg))
  71. c = Counter(D394)
  72. if c[substr2] > 0:
  73. text394 = f'You have to send {c[substr2]} more declarations of type D394'
  74. else:
  75. text394 = f'Congrats you have {c[substr2]} declarations to send of type D394'
  76. context.bot.send_message(chat_id=update.effective_chat.id, text=text394)
  77. def D_100(update, context):
  78. sheet = sht.worksheet(f'100 {get_period()} 2021')
  79. messages = sheet.batch_get(['J3:J100'], major_dimension='COLUMNS')[0]
  80. D100 = messages[0]
  81. for msg in D100:
  82. if substr in msg:
  83. D100.insert(0, substr)
  84. D100.pop(D100.index(msg))
  85. c = Counter(D100)
  86. if c[substr2] > 0:
  87. text394 = f'You have to send {c[substr2]} more declarations of type D100'
  88. else:
  89. text394 = f'Congrats you have {c[substr2]} declarations to send of type D100'
  90. context.bot.send_message(chat_id=update.effective_chat.id, text=text394)
  91. def main():
  92. updater = Updater(token=config['AUTH']['token2'])
  93. dispatcher = updater.dispatcher
  94. # start_handler = CommandHandler('D390', D390)
  95. dispatcher.add_handler(CommandHandler('D100', D_100))
  96. dispatcher.add_handler(CommandHandler('D300', D_300))
  97. dispatcher.add_handler(CommandHandler('D390', D_390))
  98. dispatcher.add_handler(CommandHandler('D394', D_394))
  99. updater.start_polling()
  100. updater.idle()
  101. if __name__ == '__main__':
  102. main()
  103. # print(get_period())