-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelebot.py
234 lines (192 loc) · 8.65 KB
/
telebot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import logging
import requests
import datefinder
from datetime import datetime, timedelta
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
import pickle
from oauth2client import client
from oauth2client import tools
from google.auth.transport.requests import Request
import os.path
import random
import redis
import string
import telegram
import json
import os
from sutime import SUTime
from nltk.corpus import stopwords
import logging
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
redis_client = redis.StrictRedis(host='localhost', port=6379, db=0, decode_responses=True)
redis_pickle_client = redis.StrictRedis(host='localhost', port=6379, db=0, decode_responses=False)
INPUT_NAME = range(0)
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar']
#create_event(start_time_str= '23 Jan 12.30pm', summary="Test Meeting using CreateFunction Method",
#description="Test Description",location="Mentone, VIC, Australia") #callfunction
### To Begin OAuth ###
#Utility function to begin OAuth process and gain access to the calendar
def get_service(bot, update, code=None):
user_id = get_user_id(bot,update)
creds = None
service = None
if redis_client.hexists(user_id, "credentials"):
creds = pickle.loads(redis_pickle_client.hget(user_id, "credentials"))
service = build("calendar", "v3", credentials=creds, cache_discovery=False)
return service
# If there are no (valid) credentials available, let the user log in.
if (creds == None) or (not creds.valid):
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
service = build("calendar", "v3", credentials=creds, cache_discovery=False)
return service
else:
flow = InstalledAppFlow.from_client_secrets_file(
"client_secret.json", SCOPES)
# Setup authorization URL
flow.redirect_uri = InstalledAppFlow._OOB_REDIRECT_URI
auth_url, _ = flow.authorization_url()
if not code:
instructions = """Please follow the instructions to link your Google Account:
1. Click on the authorization URL
2. Copy the authentication code
3. Use '/permissions <authentication_code>' to finish!"""
keyboard = [[telegram.InlineKeyboardButton("Open URL", url=auth_url)]]
reply_markup = telegram.InlineKeyboardMarkup(keyboard)
update.message.reply_text(instructions, reply_markup=reply_markup)
else:
print("WITHIN GET SERVICE")
print(code)
# Fetch access token if args present
flow.fetch_token(code=code)
creds = flow.credentials
print("Obtain credentials")
# Save the credentials for the next run
redis_pickle_client.hset(user_id, "client_secret", pickle.dumps(creds))
service = build("calendar", "v3", credentials=creds, cache_discovery=False)
return service
return service
#Wrapper function that calls the function to create a new event
def finish_oauth(bot, update, args): #start_time, end_time, summary, args):
user_id = get_user_id(bot, update)
try:
print("WITHIN FINISH OAUTH")
print(args[0])
get_service(bot, update, code = args[0]) #If the code is present
update.message.reply_text("Authorization Succesfully Granted")
except Exception as e:
print(e)
update.message.reply_text("Authorization Failed")
#New event is the actual function that goes onto create the event.
#Summary is the same as the title for the event
def new_event(bot, update): #start_time, end_time, summary, code=None):
user_id = get_user_id(bot,update)
if redis_client.hexists(user_id, "credentials"):
print("AWESOME")
creds = pickle.loads(redis_pickle_client.hget(user_id, "credentials"))
print(creds)
service = build("calendar", "v3", credentials=creds, cache_discovery=False)
##Temporary code ##
start_time_str = "23 Jan 12:30pm"
title = "Test event"
matches = list(datefinder.find_dates(start_time_str))
if len(matches):
start_time = matches[0]
end_time = start_time + timedelta(hours=1)
timezone = 'Asia/Singaapore'
event = {
'summary': title,
'start': {
'dateTime': start_time.strftime("%Y-%m-%dT%H:%M:%S"),
'timeZone': timezone,
},
'end': {
'dateTime': end_time.strftime("%Y-%m-%dT%H:%M:%S"),
'timeZone': timezone,
},
}
return service.events().insert(calendarId='primary', body=event).execute()
# Need to find a way to store the code which the user has provided -- Do so with user_id
def get_user_id(bot, update):
user_id = update.message.from_user.username
return user_id
def start(bot, update):
"""Send a message when the command /start is issued."""
update.message.reply_text('Hi! I am a Google Calendar Bot. How may I help you?')
update.message.reply_text('Simply type the event you would like to schedule')
update.message.reply_text('Use /setup to sign-in, if this is your first time using the bot.')
update.message.reply_text('Later just type the event, the date and time and I will settle the rest!')
def help(bot, update):
"""Send a message when the command /help is issued."""
update.message.reply_text('How can I help?')
def error(bot, update, error):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, error)
def echo(bot, update):
"""Echo the user message."""
update.message.reply_text("Ok! So just to confirm, (Please give me a moment)")
jar_files = os.path.join(os.path.dirname(__file__), 'jars')
sutime = SUTime(jars=jar_files, mark_time_ranges=True)
userInput = update.message.text
finaltime = sutime.parse(userInput)
dayidentifier = finaltime[0]['text']
if len(finaltime) == 2:
text2 = finaltime[1]['text']
# Removing Date and Time from input
userInput = userInput.replace(dayidentifier, "")
if len(finaltime) == 2:
userInput = userInput.replace(text2, "")
# Remove stopwords
clean_userInput = [word for word in userInput.split() if word.lower() not in stopwords.words('english')]
finaltitle = ""
for i in clean_userInput:
finaltitle = finaltitle + " " + i
# Printing out Title and Date and Time
print("Title: " + finaltitle)
if len(finaltime) == 2:
final = "Date and Time: " + dayidentifier + " " + text2
else:
final = dayidentifier
#update.message.reply_text(final)
if len(finaltime) == 2:
if type(finaltime[1]['value']) == str:
index = finaltime[1]['value'].find("T")
update.message.reply_text("At: " + finaltime[1]['value'][index+1:])
else:
update.message.reply_text("From: " + finaltime[1]['value']['begin'] + " To: " + finaltime[1]['value']['end'])
update.message.reply_text("Date: " + finaltime[0]['value'])
else:
index = finaltime[0]['value'].find("T")
update.message.reply_text("At: " + finaltime[0]['value'][index + 1:])
update.message.reply_text("Date: " + finaltime[0]['value'][:index])
update.message.reply_text("Title: " + finaltitle)
update.message.reply_text("We have created the event based on the above details!")
def main():
"""Start the bot."""
# Create the EventHandler and pass it your bot's token.
updater = Updater("TOKEN", use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
dp.add_handler(CommandHandler("setup", get_service))
dp.add_handler(CommandHandler("permissions", finish_oauth, pass_args=True))
dp.add_handler(CommandHandler("new_event", new_event))
dp.add_handler(MessageHandler(Filters.text, echo))
# log all errors
dp.add_error_handler(error)
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()