From 46cd105a237284c5e572b2b032b43eaaa0443e5f Mon Sep 17 00:00:00 2001 From: Winston Cahya Date: Sun, 17 Oct 2021 19:01:06 +0800 Subject: [PATCH 01/27] Delete and unique Key --- repo_list.txt | 1 + telebot_main.py | 99 +++++++++++++++++++++++++++++++++++++------------ 2 files changed, 77 insertions(+), 23 deletions(-) diff --git a/repo_list.txt b/repo_list.txt index fe14f3f..b0afd02 100644 --- a/repo_list.txt +++ b/repo_list.txt @@ -3,3 +3,4 @@ chat_id,owner_name,repo_url 176037276,Albertsutz,https://github.com/indocom/lumina 176037276,Albertsutz,https://github.com/indocom/pinus-telebot 176037276,Albertsutz,https://github.com/indocom/pinus-client +1141961290,Cwd324,https://github.com/indocom/pinus-telebot diff --git a/telebot_main.py b/telebot_main.py index 910970d..3e82253 100644 --- a/telebot_main.py +++ b/telebot_main.py @@ -14,7 +14,8 @@ from google.oauth2.credentials import Credentials from csv_handler import * -BOT_API_TOKEN = "" +BOT_API_TOKEN = os.environ.get('BOT_API_TOKEN') +GITHUB_API_TOKEN = os.environ.get('GITHUB_API_TOKEN') PORT = int(os.environ.get('PORT', 8443)) # chat_ids = [] @@ -56,14 +57,18 @@ def unknown(update, context): def repo_list(update, context): url = "https://api.github.com/users/indocom/repos" - response = requests.get(url) - json = response.json() - - reply_text = ("Hi, here is the list of PINUS Repositories: \n") - for i in json: - reply_text += ("- " + i["name"] + " : " + i["svn_url"] + "\n") + repo_data = readCSVfromFile("repo_list.txt") + length = len(repo_data) + id = str(update.message.chat_id) + text = "Here is your list of repo registered in the bot: " - context.bot.send_message(chat_id=update.effective_chat.id, text=reply_text) + for key, value in repo_data.items(): + if(value[0] != id): + continue + github_url = value[2] + text += github_url + text += "\n" + context.bot.send_message(chat_id=id, text=text) def new_pull_request(update, context): repo_data = readCSVfromFile("repo_list.txt") @@ -74,13 +79,15 @@ def new_pull_request(update, context): for key, value in repo_data.items(): if(value[0] != id): continue - - url = "https://api.github.com/repos/" + value[2][19:] + "/pulls" - print(url) - response = requests.get(url) - json = response.json() - - print(json) + + github_url = value[2][19:] + url = "https://api.github.com/repos/" + github_url + "/pulls" + try: + response = requests.get(url, auth=('user',GITHUB_API_TOKEN)) + json = response.json() + except requests.exceptions.RequestException as e: + context.bot.send_message(chat_id = id, text = "An error occured, Pull request may be Incomplete") + top_5 = json[0:5] new_pulls = [] @@ -127,8 +134,11 @@ def broadcast_pull_request(context): url = "https://api.github.com/repos/" + value[2][19:] + "/pulls" print(url) - response = requests.get(url) - json = response.json() + try: + response = requests.get(url, auth=('user',GITHUB_API_TOKEN)) + json = response.json() + except requests.exceptions.RequestException as e: + context.bot.send_message(chat_id = id, text = "An error occured, Pull request may be Incomplete") top_5 = json[0:5] new_pulls = [] @@ -258,20 +268,61 @@ def status(update, context): def add_repo(update, context): repo_data = readCSVfromFile("repo_list.txt") length = len(repo_data) + + try: # args[0] should contain the time for the timer in seconds new_github_url = context.args[0] new_chat_id = str(update.message.chat_id) - new_owner_name = update.message.from_user.username - repo_data[length] = [new_chat_id, new_owner_name, new_github_url] - - text = 'Successfully added new repo' - writeToCSV("repo_list.txt", repo_data) - update.message.reply_text(text + str(repo_data)) except (IndexError, ValueError): update.message.reply_text('Usage: /add_repo ') + #Catch duplicate entries + dupes = False + id = str(update.message.chat_id) + text = "" + for key, value in repo_data.items(): + if(value[0] == id and value[2] == new_github_url): + dupes = True + + if(dupes) : + update.message.reply_text("The repo has already been registered !") + return + + new_owner_name = update.message.from_user.username + repo_data[length] = [new_chat_id, new_owner_name, new_github_url] + + text = 'Successfully added new repo' + writeToCSV("repo_list.txt", repo_data) + update.message.reply_text(text + str(repo_data)) + +def remove_repo(update, context): + repo_data = readCSVfromFile("repo_list.txt") + length = len(repo_data) + id = str(update.message.chat_id) + try: + # args[0] should contain the time for the timer in seconds + to_be_deleted_github_url = context.args[0] + + except (IndexError, ValueError): + update.message.reply_text('Usage: /remove_repo ') + return + delete = False + new_repo_data = dict(repo_data) + for key, value in repo_data.items(): + if(value[0] == id and value[2] == to_be_deleted_github_url): + delete = True + new_repo_data.pop(key) + print(new_repo_data) + writeToCSV('repo_list.txt', new_repo_data) + print("kawaii") + print(to_be_deleted_github_url) + if(delete) : + update.message.reply_text('Deleted Successfully !') + else : + update.message.reply_text('Repo not found') + #job queues job = updater.job_queue.run_repeating(broadcast_pull_request, interval=300, first=1) @@ -282,6 +333,7 @@ def add_repo(update, context): repo_list_handler = CommandHandler('repo', repo_list) new_pull_request_handler = CommandHandler('new_pull_request', new_pull_request) add_repo_handler = CommandHandler('add_repo', add_repo) +remove_repo_handler = CommandHandler('remove_repo', remove_repo) status_handler = CommandHandler('status', status) @@ -297,6 +349,7 @@ def add_repo(update, context): dispatcher.add_handler(new_pull_request_handler) dispatcher.add_handler(add_repo_handler) dispatcher.add_handler(status_handler) +dispatcher.add_handler(remove_repo_handler) dispatcher.add_handler(unknown_handler) From 71b6c46421268028022fa7bd5e4c1cabc411732a Mon Sep 17 00:00:00 2001 From: Winston Cahya Date: Sun, 17 Oct 2021 19:12:42 +0800 Subject: [PATCH 02/27] Deploying --- requirements.txt | 2 + telebot_main.py | 170 +++++++++++++++++++++++------------------------ 2 files changed, 87 insertions(+), 85 deletions(-) diff --git a/requirements.txt b/requirements.txt index c2f4e8a..9a90324 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ python-telegram-bot==12.7 requests +googleapiclient +google_auth_oauthlib diff --git a/telebot_main.py b/telebot_main.py index 3e82253..e93d617 100644 --- a/telebot_main.py +++ b/telebot_main.py @@ -8,10 +8,10 @@ import datetime import os import os.path -from googleapiclient.discovery import build -from google_auth_oauthlib.flow import InstalledAppFlow -from google.auth.transport.requests import Request -from google.oauth2.credentials import Credentials +# from googleapiclient.discovery import build +# from google_auth_oauthlib.flow import InstalledAppFlow +# from google.auth.transport.requests import Request +# from google.oauth2.credentials import Credentials from csv_handler import * BOT_API_TOKEN = os.environ.get('BOT_API_TOKEN') @@ -165,87 +165,87 @@ def broadcast_pull_request(context): # If modifying these scopes, delete the file token.json. SCOPES = ['https://www.googleapis.com/auth/calendar'] -def main(): - """Shows basic usage of the Google Calendar API. - Prints the start and name of the next 10 events on the user's calendar. - """ - creds = None - # The file token.json stores the user's access and refresh tokens, and is - # created automatically when the authorization flow completes for the first - # time. - if os.path.exists('token.json'): - creds = Credentials.from_authorized_user_file('token.json', SCOPES) - # If there are no (valid) credentials available, let the user log in. - if not creds or not creds.valid: - if creds and creds.expired and creds.refresh_token: - creds.refresh(Request()) - else: - flow = InstalledAppFlow.from_client_secrets_file( - 'credentials.json', SCOPES) - creds = flow.run_local_server(port=0) - # Save the credentials for the next run - with open('token.json', 'w') as token: - token.write(creds.to_json()) - - service = build('calendar', 'v3', credentials=creds) - - # Call the Calendar API - now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time - print('Getting the upcoming 10 events:') - f = open("events.txt", "w") - events_result = service.events().list(calendarId='tech.pinusonline@gmail.com', timeMin=now, - maxResults=10, singleEvents=True, - orderBy='startTime').execute() - events = events_result.get('items', []) - - if not events: - print('No upcoming events found.') - f.write("No upcoming events found."+"\n") - for event in events: - timestart = event['start'].get('dateTime', event['start'].get('date')) - print(timestart, event['summary']) - f.write(timestart + event['summary'] +"\n") - print() - print('Telebot Events:') - for event in events: - timestart = event['start'].get('dateTime', event['start'].get('date')) - if '[Telebot]' in event['summary']: - print(timestart, event['summary']) - f.close() - -def getevents(update, context): - if __name__ == '__main__': - main() - f = open("events.txt", "r") - reply_text = ("Getting the upcoming 10 events: \n") - for i in f: - reply_text += (i) - context.bot.send_message(chat_id=update.effective_chat.id, text=reply_text) -getevents_handler = CommandHandler('getevents', getevents) -dispatcher.add_handler(getevents_handler) - -def reminder(context): - if __name__ == '__main__': - main() - f = open("events.txt", "r") - text = '' - for i in f: - startime = datetime.datetime.strptime(i[0:19], "%Y-%m-%dT%H:%M:%S") - minute = (startime - datetime.datetime.now()).total_seconds()/60 - if 59 <= minute < 61: - a = i - text = "Reminder: You have an event in 1 hour. \n" - text += a - break - if len(text) > 2: - context.bot.send_message(chat_id=context.job.context, text=text) - -def remindme(update, context): - reply = 'Reminder is on.' - context.bot.send_message(chat_id=update.message.chat_id, text=reply) - context.job_queue.run_repeating(reminder, interval = 120, first = 1, context=update.message.chat_id) -remindme_handler = CommandHandler('remindme', remindme) -dispatcher.add_handler(remindme_handler) +# def main(): +# """Shows basic usage of the Google Calendar API. +# Prints the start and name of the next 10 events on the user's calendar. +# """ +# creds = None +# # The file token.json stores the user's access and refresh tokens, and is +# # created automatically when the authorization flow completes for the first +# # time. +# if os.path.exists('token.json'): +# creds = Credentials.from_authorized_user_file('token.json', SCOPES) +# # If there are no (valid) credentials available, let the user log in. +# if not creds or not creds.valid: +# if creds and creds.expired and creds.refresh_token: +# creds.refresh(Request()) +# else: +# flow = InstalledAppFlow.from_client_secrets_file( +# 'credentials.json', SCOPES) +# creds = flow.run_local_server(port=0) +# # Save the credentials for the next run +# with open('token.json', 'w') as token: +# token.write(creds.to_json()) + +# service = build('calendar', 'v3', credentials=creds) + +# # Call the Calendar API +# now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time +# print('Getting the upcoming 10 events:') +# f = open("events.txt", "w") +# events_result = service.events().list(calendarId='tech.pinusonline@gmail.com', timeMin=now, +# maxResults=10, singleEvents=True, +# orderBy='startTime').execute() +# events = events_result.get('items', []) + +# if not events: +# print('No upcoming events found.') +# f.write("No upcoming events found."+"\n") +# for event in events: +# timestart = event['start'].get('dateTime', event['start'].get('date')) +# print(timestart, event['summary']) +# f.write(timestart + event['summary'] +"\n") +# print() +# print('Telebot Events:') +# for event in events: +# timestart = event['start'].get('dateTime', event['start'].get('date')) +# if '[Telebot]' in event['summary']: +# print(timestart, event['summary']) +# f.close() + +# def getevents(update, context): +# if __name__ == '__main__': +# main() +# f = open("events.txt", "r") +# reply_text = ("Getting the upcoming 10 events: \n") +# for i in f: +# reply_text += (i) +# context.bot.send_message(chat_id=update.effective_chat.id, text=reply_text) +# getevents_handler = CommandHandler('getevents', getevents) +# dispatcher.add_handler(getevents_handler) + +# def reminder(context): +# if __name__ == '__main__': +# main() +# f = open("events.txt", "r") +# text = '' +# for i in f: +# startime = datetime.datetime.strptime(i[0:19], "%Y-%m-%dT%H:%M:%S") +# minute = (startime - datetime.datetime.now()).total_seconds()/60 +# if 59 <= minute < 61: +# a = i +# text = "Reminder: You have an event in 1 hour. \n" +# text += a +# break +# if len(text) > 2: +# context.bot.send_message(chat_id=context.job.context, text=text) + +# def remindme(update, context): +# reply = 'Reminder is on.' +# context.bot.send_message(chat_id=update.message.chat_id, text=reply) +# context.job_queue.run_repeating(reminder, interval = 120, first = 1, context=update.message.chat_id) +# remindme_handler = CommandHandler('remindme', remindme) +# dispatcher.add_handler(remindme_handler) def status(update, context): repo_data = readCSVfromFile("repo_list.txt") From 0e4176bd4da57d0ef540acb856c01dcc7ac5be20 Mon Sep 17 00:00:00 2001 From: Winston Cahya Date: Sun, 17 Oct 2021 19:15:58 +0800 Subject: [PATCH 03/27] Updated Requirements.txt --- requirements.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 9a90324..7655668 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ python-telegram-bot==12.7 requests -googleapiclient -google_auth_oauthlib + From 0094436f3a87c58fea59ce47a0a51ab7a8d13c23 Mon Sep 17 00:00:00 2001 From: Winston Cahya Date: Sun, 17 Oct 2021 19:24:19 +0800 Subject: [PATCH 04/27] Change to http webhook --- telebot_main.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/telebot_main.py b/telebot_main.py index e93d617..1750375 100644 --- a/telebot_main.py +++ b/telebot_main.py @@ -353,12 +353,12 @@ def remove_repo(update, context): dispatcher.add_handler(unknown_handler) -# updater.start_webhook(listen="0.0.0.0", -# port=int(PORT), -# url_path=BOT_API_TOKEN -# ) -# updater.bot.set_webhook('https://enigmatic-sands-16778.herokuapp.com/' + BOT_API_TOKEN) -updater.start_polling() -print("Server Bot is up and running !") -updater.idle() +updater.start_webhook(listen="0.0.0.0", + port=int(PORT), + url_path=BOT_API_TOKEN + ) +updater.bot.set_webhook('https://enigmatic-sands-16778.herokuapp.com/' + BOT_API_TOKEN) +# updater.start_polling() +# print("Server Bot is up and running !") +# updater.idle() print("Listening .... ") From a3c7707bca092152b33c41b18ce4671fde417dc5 Mon Sep 17 00:00:00 2001 From: Winston Cahya Date: Wed, 20 Oct 2021 13:17:51 +0800 Subject: [PATCH 05/27] Reminder Calendar --- .gitignore | 3 +- csv_handler.py | 13 ++- events.txt | 10 ++ events_subscription.txt | 1 + telebot_main.py | 196 ++++++++++++++++++++-------------------- 5 files changed, 118 insertions(+), 105 deletions(-) create mode 100644 events.txt create mode 100644 events_subscription.txt diff --git a/.gitignore b/.gitignore index ee79715..578a4ae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .env -*.pyc \ No newline at end of file +*.pyc +.json diff --git a/csv_handler.py b/csv_handler.py index 1725a1b..be2a9a6 100644 --- a/csv_handler.py +++ b/csv_handler.py @@ -1,25 +1,24 @@ import csv def readCSVfromFile(csv_file_path) : - repo_data = dict() + storage = dict() with open(csv_file_path) as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') line_count = 0 for row in csv_reader: if line_count > 0 : - repo_data[line_count - 1] = row; + storage[line_count - 1] = row; line_count += 1 print(f'Processed {line_count} lines.') - return repo_data + return storage -def writeToCSV(csv_file_path, repo_data) : +def writeToCSV(csv_file_path, storage, fieldname) : with open(csv_file_path, mode='w', newline='') as csv_file: writer = csv.writer(csv_file, delimiter=',') - length = len(repo_data) - fieldname = ['chat_id', 'owner_name', 'repo_url'] + length = len(storage) writer.writerow(fieldname) for i in range(length): - writer.writerow(repo_data[i]); + writer.writerow(storage[i]); diff --git a/events.txt b/events.txt new file mode 100644 index 0000000..755d712 --- /dev/null +++ b/events.txt @@ -0,0 +1,10 @@ +2021-10-21T21:00:00+08:00[Blog] PINUS Tech Sprint Meeting +2021-10-23T17:00:00+08:00[Pinus Client] Tech Meeting +2021-10-24T21:00:00+08:00[Telebot] Pinus Tech Sprint Meeting +2021-10-28T21:00:00+08:00[Blog] PINUS Tech Sprint Meeting +2021-10-30T17:00:00+08:00[Pinus Client] Tech Meeting +2021-10-31T21:00:00+08:00[Telebot] Pinus Tech Sprint Meeting +2021-11-04T21:00:00+08:00[Blog] PINUS Tech Sprint Meeting +2021-11-06T17:00:00+08:00[Pinus Client] Tech Meeting +2021-11-07T21:00:00+08:00[Telebot] Pinus Tech Sprint Meeting +2021-11-11T21:00:00+08:00[Blog] PINUS Tech Sprint Meeting diff --git a/events_subscription.txt b/events_subscription.txt new file mode 100644 index 0000000..27dccaa --- /dev/null +++ b/events_subscription.txt @@ -0,0 +1 @@ +id, header \ No newline at end of file diff --git a/telebot_main.py b/telebot_main.py index 1750375..dced2f4 100644 --- a/telebot_main.py +++ b/telebot_main.py @@ -8,10 +8,10 @@ import datetime import os import os.path -# from googleapiclient.discovery import build -# from google_auth_oauthlib.flow import InstalledAppFlow -# from google.auth.transport.requests import Request -# from google.oauth2.credentials import Credentials +from googleapiclient.discovery import build +from google_auth_oauthlib.flow import InstalledAppFlow +from google.auth.transport.requests import Request +from google.oauth2.credentials import Credentials from csv_handler import * BOT_API_TOKEN = os.environ.get('BOT_API_TOKEN') @@ -165,87 +165,84 @@ def broadcast_pull_request(context): # If modifying these scopes, delete the file token.json. SCOPES = ['https://www.googleapis.com/auth/calendar'] -# def main(): -# """Shows basic usage of the Google Calendar API. -# Prints the start and name of the next 10 events on the user's calendar. -# """ -# creds = None -# # The file token.json stores the user's access and refresh tokens, and is -# # created automatically when the authorization flow completes for the first -# # time. -# if os.path.exists('token.json'): -# creds = Credentials.from_authorized_user_file('token.json', SCOPES) -# # If there are no (valid) credentials available, let the user log in. -# if not creds or not creds.valid: -# if creds and creds.expired and creds.refresh_token: -# creds.refresh(Request()) -# else: -# flow = InstalledAppFlow.from_client_secrets_file( -# 'credentials.json', SCOPES) -# creds = flow.run_local_server(port=0) -# # Save the credentials for the next run -# with open('token.json', 'w') as token: -# token.write(creds.to_json()) - -# service = build('calendar', 'v3', credentials=creds) - -# # Call the Calendar API -# now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time -# print('Getting the upcoming 10 events:') -# f = open("events.txt", "w") -# events_result = service.events().list(calendarId='tech.pinusonline@gmail.com', timeMin=now, -# maxResults=10, singleEvents=True, -# orderBy='startTime').execute() -# events = events_result.get('items', []) - -# if not events: -# print('No upcoming events found.') -# f.write("No upcoming events found."+"\n") -# for event in events: -# timestart = event['start'].get('dateTime', event['start'].get('date')) -# print(timestart, event['summary']) -# f.write(timestart + event['summary'] +"\n") -# print() -# print('Telebot Events:') -# for event in events: -# timestart = event['start'].get('dateTime', event['start'].get('date')) -# if '[Telebot]' in event['summary']: -# print(timestart, event['summary']) -# f.close() - -# def getevents(update, context): -# if __name__ == '__main__': -# main() -# f = open("events.txt", "r") -# reply_text = ("Getting the upcoming 10 events: \n") -# for i in f: -# reply_text += (i) -# context.bot.send_message(chat_id=update.effective_chat.id, text=reply_text) -# getevents_handler = CommandHandler('getevents', getevents) -# dispatcher.add_handler(getevents_handler) - -# def reminder(context): -# if __name__ == '__main__': -# main() -# f = open("events.txt", "r") -# text = '' -# for i in f: -# startime = datetime.datetime.strptime(i[0:19], "%Y-%m-%dT%H:%M:%S") -# minute = (startime - datetime.datetime.now()).total_seconds()/60 -# if 59 <= minute < 61: -# a = i -# text = "Reminder: You have an event in 1 hour. \n" -# text += a -# break -# if len(text) > 2: -# context.bot.send_message(chat_id=context.job.context, text=text) - -# def remindme(update, context): -# reply = 'Reminder is on.' -# context.bot.send_message(chat_id=update.message.chat_id, text=reply) -# context.job_queue.run_repeating(reminder, interval = 120, first = 1, context=update.message.chat_id) -# remindme_handler = CommandHandler('remindme', remindme) -# dispatcher.add_handler(remindme_handler) +def connectCalendar(): + """Shows basic usage of the Google Calendar API. + Prints the start and name of the next 10 events on the user's calendar. + """ + creds = None + # The file token.json stores the user's access and refresh tokens, and is + # created automatically when the authorization flow completes for the first + # time. + if os.path.exists('token.json'): + creds = Credentials.from_authorized_user_file('token.json', SCOPES) + # If there are no (valid) credentials available, let the user log in. + if not creds or not creds.valid: + if creds and creds.expired and creds.refresh_token: + creds.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file( + 'credentials.json', SCOPES) + creds = flow.run_local_server(port=0) + # Save the credentials for the next run + with open('token.json', 'w') as token: + token.write(creds.to_json()) + + service = build('calendar', 'v3', credentials=creds) + + # Call the Calendar API + now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time + print('Getting the upcoming 10 events:') + f = open("events.txt", "w") + events_result = service.events().list(calendarId='tech.pinusonline@gmail.com', timeMin=now, + maxResults=10, singleEvents=True, + orderBy='startTime').execute() + events = events_result.get('items', []) + + if not events: + print('No upcoming events found.') + f.write("No upcoming events found."+"\n") + for event in events: + timestart = event['start'].get('dateTime', event['start'].get('date')) + print(timestart, event['summary']) + f.write(timestart + event['summary'] +"\n") + print() + print('Telebot Events:') + for event in events: + timestart = event['start'].get('dateTime', event['start'].get('date')) + if '[Telebot]' in event['summary']: + print(timestart, event['summary']) + f.close() + +def getevents(update, context): + connectCalendar() + f = open("events.txt", "r") + reply_text = ("Getting the upcoming 10 events: \n") + for i in f: + reply_text += (i) + context.bot.send_message(chat_id=update.effective_chat.id, text=reply_text) + + +def reminder(context): + connectCalendar() + f = open("events.txt", "r") + text = '' + for i in f: + startime = datetime.datetime.strptime(i[0:19], "%Y-%m-%dT%H:%M:%S") + minute = (startime - datetime.datetime.now()).total_seconds()/60 + if 59 <= minute < 61: + a = i + text = "Reminder: You have an event in 1 hour. \n" + text += a + break + if len(text) > 2: + context.bot.send_message(chat_id=context.job.context, text=text) + print("Hallo") + +def remindme(update, context): + reply = 'Reminder is on.' + context.bot.send_message(chat_id=update.message.chat_id, text=reply) + context.job_queue.run_repeating(reminder, interval = 120, first = 1, context=update.message.chat_id) + def status(update, context): repo_data = readCSVfromFile("repo_list.txt") @@ -269,7 +266,6 @@ def add_repo(update, context): repo_data = readCSVfromFile("repo_list.txt") length = len(repo_data) - try: # args[0] should contain the time for the timer in seconds new_github_url = context.args[0] @@ -294,7 +290,8 @@ def add_repo(update, context): repo_data[length] = [new_chat_id, new_owner_name, new_github_url] text = 'Successfully added new repo' - writeToCSV("repo_list.txt", repo_data) + fieldname = ['chat_id', 'owner_name', 'repo_url'] + writeToCSV("repo_list.txt", repo_data, fieldname) update.message.reply_text(text + str(repo_data)) def remove_repo(update, context): @@ -315,7 +312,8 @@ def remove_repo(update, context): delete = True new_repo_data.pop(key) print(new_repo_data) - writeToCSV('repo_list.txt', new_repo_data) + fieldname = ['chat_id', 'owner_name', 'repo_url'] + writeToCSV('repo_list.txt', new_repo_data, fieldname) print("kawaii") print(to_be_deleted_github_url) if(delete) : @@ -334,6 +332,9 @@ def remove_repo(update, context): new_pull_request_handler = CommandHandler('new_pull_request', new_pull_request) add_repo_handler = CommandHandler('add_repo', add_repo) remove_repo_handler = CommandHandler('remove_repo', remove_repo) +getevents_handler = CommandHandler('getevents', getevents) +remindme_handler = CommandHandler('remindme', remindme) + status_handler = CommandHandler('status', status) @@ -350,15 +351,16 @@ def remove_repo(update, context): dispatcher.add_handler(add_repo_handler) dispatcher.add_handler(status_handler) dispatcher.add_handler(remove_repo_handler) - +dispatcher.add_handler(getevents_handler) +dispatcher.add_handler(remindme_handler) dispatcher.add_handler(unknown_handler) -updater.start_webhook(listen="0.0.0.0", - port=int(PORT), - url_path=BOT_API_TOKEN - ) -updater.bot.set_webhook('https://enigmatic-sands-16778.herokuapp.com/' + BOT_API_TOKEN) -# updater.start_polling() -# print("Server Bot is up and running !") -# updater.idle() +# updater.start_webhook(listen="0.0.0.0", +# port=int(PORT), +# url_path=BOT_API_TOKEN +# ) +# updater.bot.set_webhook('https://enigmatic-sands-16778.herokuapp.com/' + BOT_API_TOKEN) +updater.start_polling() +print("Server Bot is up and running !") +updater.idle() print("Listening .... ") From fd1df2af7d9b91100823fc01f3d0a2de05a2c60c Mon Sep 17 00:00:00 2001 From: nixonwidjaja <69686938+nixonwidjaja@users.noreply.github.com> Date: Sun, 24 Oct 2021 19:59:59 +0800 Subject: [PATCH 06/27] Add event --- telebot_main.py | 189 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 152 insertions(+), 37 deletions(-) diff --git a/telebot_main.py b/telebot_main.py index 910970d..1fe6470 100644 --- a/telebot_main.py +++ b/telebot_main.py @@ -12,9 +12,10 @@ from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials -from csv_handler import * +#from csv_handler import * -BOT_API_TOKEN = "" +BOT_API_TOKEN = os.environ.get('BOT_API_TOKEN') +GITHUB_API_TOKEN = os.environ.get('GITHUB_API_TOKEN') PORT = int(os.environ.get('PORT', 8443)) # chat_ids = [] @@ -26,17 +27,16 @@ #Starting our bot #Initialize updator and dispatcher -updater = Updater(token= BOT_API_TOKEN, use_context=True) +updater = Updater(token=BOT_API_TOKEN, use_context=True) dispatcher = updater.dispatcher #List of all of our functions def start(update, context): - context.bot.send_message(chat_id=update.effective_chat.id, text="I'm KawaiiBot, use /list to show list of available commands and /help to know informations about the bot") def list(update, context): - keyboard = keyboard = [['/help'], ['/status', '/repo'], ['/new_pull_request']] + keyboard = keyboard = [['/help'], ['/status', '/repo'], ['/new_pull_request'], ['/getevents', '/addevent']] reply_markup = ReplyKeyboardMarkup(keyboard, one_time_keyboard=True, @@ -49,6 +49,8 @@ def help_func(update, context): text += ("/repo: Get the list of all repositories inside github.com/indocom\n\n") text += ("/status: Get the list of all repositories that you have subscribed\n\n") text += ("/add_repo : Subscribe to a particular repo\n\n") + text += ("/getevents: Get the upcoming 10 events\n\n") + text += ("/addevent