'''Created 19 Apr 2021 deeejas''' import os import configparser from collections import Counter from datetime import ( date, timedelta ) import telegram import gspread # from gspread.models import Worksheet as sheet # @UnusedImport from google.oauth2.service_account import Credentials from telegram.ext import CommandHandler from telegram.ext import Updater json_ = filter(lambda js: js.endswith('.json'), os.listdir()) json_ = list(json_)[0] substr = 'Nu exist' substr2 = 'inca nedepusa' scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive'] creds = Credentials.from_service_account_file(json_, scopes=scope) gsheet = gspread.authorize(creds) sht = gsheet.open('Notif_Decl') config = configparser.ConfigParser() config.read('bot') bot = telegram.Bot(token=config['AUTH']['bank']) # print(bot.getMe()) # bot.send_message(chat_id=config['AUTH']['chatid'], text=text) def get_period(): current_date = date.today() days_back = timedelta(days=30) period = current_date - days_back return period.strftime('%m') def get_chat_id(update, context): chat_id = update.effective_chat.id context.bot.send_message(chat_id=update.effective_chat.id, text=chat_id) def D_300(update, context): sheet = sht.worksheet(f'300 {get_period()} 2021') messages = sheet.batch_get(['K3:M100'], major_dimension='COLUMNS')[0] D300 = messages[0] for msg in D300: if substr in msg: D300.insert(0, substr) D300.pop(D300.index(msg)) c = Counter(D300) if c[substr2] > 0: text300 = f'You have to send {c[substr2]} more declarations of type D300' else: text300 = f'Congrats you have {c[substr2]} declarations to send of type D300' context.bot.send_message(chat_id=update.effective_chat.id, text=text300) def D_390(update, context): sheet = sht.worksheet(f'300 {get_period()} 2021') messages = sheet.batch_get(['K3:M100'], major_dimension='COLUMNS')[0] D390 = messages[1] for msg in D390: if substr in msg: D390.insert(0, substr) D390.pop(D390.index(msg)) c = Counter(D390) if c[substr2] > 0: text390 = f'You have to send {c[substr2]} more declarations of type D390' else: text390 = f'Congrats you have {c[substr2]} declarations to send of type D390' context.bot.send_message(chat_id=update.effective_chat.id, text=text390) # print(update.effective_chat.id) def D_394(update, context): sheet = sht.worksheet(f'300 {get_period()} 2021') messages = sheet.batch_get(['K3:M100'], major_dimension='COLUMNS')[0] D394 = messages[2] for msg in D394: if substr in msg: D394.insert(0, substr) D394.pop(D394.index(msg)) c = Counter(D394) if c[substr2] > 0: text394 = f'You have to send {c[substr2]} more declarations of type D394' else: text394 = f'Congrats you have {c[substr2]} declarations to send of type D394' context.bot.send_message(chat_id=update.effective_chat.id, text=text394) def D_100(update, context): sheet = sht.worksheet(f'100 {get_period()} 2021') messages = sheet.batch_get(['I3:I100'], major_dimension='COLUMNS')[0] D100 = messages[0] for msg in D100: if substr in msg: D100.insert(0, substr) D100.pop(D100.index(msg)) c = Counter(D100) if c[substr2] > 0: text394 = f'You have to send {c[substr2]} more declarations of type D100' else: text394 = f'Congrats you have {c[substr2]} declarations to send of type D100' context.bot.send_message(chat_id=update.effective_chat.id, text=text394) def main(): updater = Updater(token=config['AUTH']['bank']) dispatcher = updater.dispatcher # start_handler = CommandHandler('D390', D390) dispatcher.add_handler(CommandHandler('D100', D_100)) dispatcher.add_handler(CommandHandler('D300', D_300)) dispatcher.add_handler(CommandHandler('D390', D_390)) dispatcher.add_handler(CommandHandler('D394', D_394)) dispatcher.add_handler(CommandHandler('chatid', get_chat_id)) updater.start_polling() updater.idle() if __name__ == '__main__': main() # print(get_period())