-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsobot.py
148 lines (127 loc) · 5.1 KB
/
sobot.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
import requests
import sys
import json
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import logging
API = "http://openedx.ing.puc.cl/user_api/v1/account/login_session/"
EMAIL = 'EMAIL'
PASSWORD = 'PASSWORD'
BOT_TOKEN = '700596284:AAFQgaXUPXJNTDExZuySj1EI7bdlJylHBSI'
FETCH = 300
OLD_DATA = []
NEW_DATA = []
def start(bot, job):
bot.send_message(job.context, text='🤖 Hola! 🤖\n Este es el bot para recibir mensajes del foro de Sistemas Operativos y Redes. Para comenzar, escribe /login <email> <password> para iniciar sesión.')
def getToken(client):
client.get(API)
if 'csrftoken' in client.cookies:
return client.cookies['csrftoken']
else:
return client.cookies['csrf']
def loginToAPI(client, token):
print("TOKEN, ", token)
login_data = dict(email=EMAIL, password=PASSWORD, csrfmiddlewaretoken=token, next='/')
r = client.post(API, data=login_data, headers=dict(Referer=API))
return (r.status_code)
print("LOGIN", r.status_code, r.reason)
def update(bot, job):
"""Revisa si hay nuevos comentarios"""
global OLD_DATA
global NEW_DATA
global EMAIL
global PASSWORD
client = requests.session()
csrftoken = getToken(client)
loginToAPI(client, csrftoken)
s = client.get("http://openedx.ing.puc.cl/api/discussion/v1/threads/?course_id=course-v1%3APUC%2BIIC2333_II%2BIIC2333_II_2018&sort_order=desc")
#print("GET COMMENTS", s.status_code, s.reason)
answer = s.json()
print(answer)
if len(OLD_DATA) == 0:
OLD_DATA = answer['results']
NEW_DATA = OLD_DATA
else:
OLD_DATA = NEW_DATA
NEW_DATA = answer['results']
if OLD_DATA != NEW_DATA:
result = [x for x in NEW_DATA if x not in OLD_DATA][0]
author = result['author']
title = result['title']
comment = result['raw_body']
link = str("http://openedx.ing.puc.cl/courses/"+
result['course_id']+"/discussion/forum/"+
result['topic_id']+"/threads/"+result['id'])
text = str("📩 Nueva pregunta/comentario 📩 \n"+
"👤: " + author + "\n"+
"Título: "+title+"\n"+
"Comentario: "+comment+"\n"+
"🔗: "+link)
bot.send_message(job.context, text=text)
else:
bot.send_message(job.context, text="✅ No hay nuevos mensajes")
def getComments(bot,job):
"""Revisa si hay nuevos comentarios"""
global OLD_DATA
global NEW_DATA
global EMAIL
global PASSWORD
client = requests.session()
csrftoken = getToken(client)
loginToAPI(client, csrftoken)
s = client.get("http://openedx.ing.puc.cl/api/discussion/v1/threads/?course_id=course-v1%3APUC%2BIIC2333_II%2BIIC2333_II_2018&sort_order=desc")
#print("GET COMMENTS", s.status_code, s.reason)
answer = s.json()
if len(OLD_DATA) == 0:
OLD_DATA = answer['results']
NEW_DATA = OLD_DATA
else:
OLD_DATA = NEW_DATA
NEW_DATA = answer['results']
if OLD_DATA != NEW_DATA:
result = [x for x in NEW_DATA if x not in OLD_DATA][0]
author = result['author']
title = result['title']
comment = result['raw_body']
link = str("http://openedx.ing.puc.cl/courses/"+
result['course_id']+"/discussion/forum/"+
result['topic_id']+"/threads/"+result['id'])
text = str("📩 Nueva pregunta/comentario 📩 \n"+
"👤: " + author + "\n"+
"Título: "+title+"\n"+
"Comentario: "+comment+"\n"+
"🔗: "+link)
bot.send_message(job.context, text=text)
return(s.json())
def login(bot, update, args, job_queue, chat_data):
global EMAIL
global PASSWORD
chat_id = update.mesage.chat_id
try:
EMAIL = str(args[0])
PASSWORD = str(args[1])
job = job_queue.run_once(getComments, FETCH, context=chat_id)
chat_data['job'] = job
update.message_reply_text('Login exitoso. Se revisará el foro cada 5 minutos comprobando si existe un nuevo mensaje.\n También puedes ocupar /update para comprobar.')
except(IndexError, ValueError):
update.message.reply_text("Hubo un error en el proceso. Uso: /login <email> <password>")
def error(bot, update, error):
logger.warning('Update "%s" caused error "%s"',update,error)
def main():
try:
print("hola")
updater = Updater(BOT_TOKEN)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", start))
dp.add_handler(CommandHandler("login", login,
pass_args=True,
pass_job_queue=True,
pass_chat_data=True))
dp.add_handler(CommandHandler("update", update))
dp.add_error_handler(error)
updater.start_polling()
updater.idle()
except(error):
print(error)
if __name__ == '__main__':
main()