This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
leave_record.py
94 lines (80 loc) · 2.79 KB
/
leave_record.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
#!/usr/bin/python3.6
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove
from telegram.ext import ConversationHandler
from graphqlclient import GraphQLClient
import json
from decouple import config
TYPE, REASON = range(2)
def get_cms_token():
api_url = config('API_URL')
client = GraphQLClient(api_url)
query = """
mutation TokenAuth($username: String!, $password: String!) {
tokenAuth(username: $username, password: $password) {
token
}
}
"""
admin_user = config('ADMIN_USER')
admin_pass = config('ADMIN_PASS')
variables = {
"username":admin_user,
"password": admin_pass
}
result = json.loads(client.execute(query, variables))
return result["data"]["tokenAuth"]["token"]
def getType(t):
if t == "Health":
return "M"
elif t == "Family/Home":
return "F"
elif t == "Academics":
return "A"
return "T"
def mutate_cms(d):
api_url = config('API_URL')
client = GraphQLClient(api_url)
query ="""
mutation RecordLeaveToday($userId: String!, $reason: String!, $type: String!, $botToken: String!, $token: String!)
{
RecordLeaveToday(userId: $userId, reason: $reason, type: $type, botToken: $botToken, token: $token)
{
id
}
}
"""
token = config('BOT_TOKEN')
variables = {
"userId": d['user'],
"reason": d['reason'],
"type": getType(d['type']),
"botToken": token,
"token": get_cms_token()
}
print(variables)
client.execute(query, variables)
class LeaveRecord:
def getType(bot, context):
reply_keyboard = [['Health', 'Family/Home', 'Tired', "Academics"]]
bot.message.reply_text("Sad to know that, you wont be coming to lab today...\n"
"What's the general cause?",
reply_markup=ReplyKeyboardMarkup(reply_keyboard,
one_time_keyboard=True))
return TYPE
def getReason(bot, context):
d = context.user_data
d['type'] = bot.message.text
bot.message.reply_text('Hmmm... Can you tell a little more about it?', reply_markup=ReplyKeyboardRemove())
return REASON
def registerLeave(bot, context):
d = context.user_data
d['user'] = bot.message.from_user['id']
d['reason'] = bot.message.text
bot.message.reply_text("Thank you for informing me.")
bot.message.reply_text("Hope to see you soon again in the lab...")
mutate_cms(d)
return ConversationHandler.END
def cancel(bot, context):
bot.message.reply_text('Bye! I hope we can talk again some day.',
reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END