anaf_bot.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. substr = 'Nu exist'
  18. substr2 = 'inca nedepusa'
  19. scope = ['https://spreadsheets.google.com/feeds',
  20. 'https://www.googleapis.com/auth/drive']
  21. creds = Credentials.from_service_account_file(json_, scopes=scope)
  22. gsheet = gspread.authorize(creds)
  23. sht = gsheet.open('Notif_Decl')
  24. config = configparser.ConfigParser()
  25. config.read('bot')
  26. bot = telegram.Bot(token=config['AUTH']['bank'])
  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 get_chat_id(update, context):
  35. chat_id = update.effective_chat.id
  36. context.bot.send_message(chat_id=update.effective_chat.id, text=chat_id)
  37. def D_300(update, context):
  38. sheet = sht.worksheet(f'300 {get_period()} 2021')
  39. messages = sheet.batch_get(['K3:M100'], major_dimension='COLUMNS')[0]
  40. D300 = messages[0]
  41. for msg in D300:
  42. if substr in msg:
  43. D300.insert(0, substr)
  44. D300.pop(D300.index(msg))
  45. c = Counter(D300)
  46. if c[substr2] > 0:
  47. text300 = f'You have to send {c[substr2]} more declarations of type D300'
  48. else:
  49. text300 = f'Congrats you have {c[substr2]} declarations to send of type D300'
  50. context.bot.send_message(chat_id=update.effective_chat.id, text=text300)
  51. def D_390(update, context):
  52. sheet = sht.worksheet(f'300 {get_period()} 2021')
  53. messages = sheet.batch_get(['K3:M100'], major_dimension='COLUMNS')[0]
  54. D390 = messages[1]
  55. for msg in D390:
  56. if substr in msg:
  57. D390.insert(0, substr)
  58. D390.pop(D390.index(msg))
  59. c = Counter(D390)
  60. if c[substr2] > 0:
  61. text390 = f'You have to send {c[substr2]} more declarations of type D390'
  62. else:
  63. text390 = f'Congrats you have {c[substr2]} declarations to send of type D390'
  64. context.bot.send_message(chat_id=update.effective_chat.id, text=text390)
  65. # print(update.effective_chat.id)
  66. def D_394(update, context):
  67. sheet = sht.worksheet(f'300 {get_period()} 2021')
  68. messages = sheet.batch_get(['K3:M100'], major_dimension='COLUMNS')[0]
  69. D394 = messages[2]
  70. for msg in D394:
  71. if substr in msg:
  72. D394.insert(0, substr)
  73. D394.pop(D394.index(msg))
  74. c = Counter(D394)
  75. if c[substr2] > 0:
  76. text394 = f'You have to send {c[substr2]} more declarations of type D394'
  77. else:
  78. text394 = f'Congrats you have {c[substr2]} declarations to send of type D394'
  79. context.bot.send_message(chat_id=update.effective_chat.id, text=text394)
  80. def D_100(update, context):
  81. sheet = sht.worksheet(f'100 {get_period()} 2021')
  82. messages = sheet.batch_get(['I3:I100'], major_dimension='COLUMNS')[0]
  83. D100 = messages[0]
  84. for msg in D100:
  85. if substr in msg:
  86. D100.insert(0, substr)
  87. D100.pop(D100.index(msg))
  88. c = Counter(D100)
  89. if c[substr2] > 0:
  90. text394 = f'You have to send {c[substr2]} more declarations of type D100'
  91. else:
  92. text394 = f'Congrats you have {c[substr2]} declarations to send of type D100'
  93. context.bot.send_message(chat_id=update.effective_chat.id, text=text394)
  94. def main():
  95. updater = Updater(token=config['AUTH']['bank'])
  96. dispatcher = updater.dispatcher
  97. # start_handler = CommandHandler('D390', D390)
  98. dispatcher.add_handler(CommandHandler('D100', D_100))
  99. dispatcher.add_handler(CommandHandler('D300', D_300))
  100. dispatcher.add_handler(CommandHandler('D390', D_390))
  101. dispatcher.add_handler(CommandHandler('D394', D_394))
  102. dispatcher.add_handler(CommandHandler('chatid', get_chat_id))
  103. updater.start_polling()
  104. updater.idle()
  105. if __name__ == '__main__':
  106. main()
  107. # print(get_period())