forked from vsvipul/ContestBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
celery_tasks.py
64 lines (46 loc) · 1.88 KB
/
celery_tasks.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
from celery import Celery
from celery.schedules import crontab
from datetime import datetime,timedelta
from pymessenger.bot import Bot
import zulip
client = zulip.Client(config_file="zuliprc")
ACCESS_TOKEN = 'EAALxONaYePsBAM5oBExC3ZC9yFGVucIiZB7fxP00AKhZBc9gjPZAqtk7Ed8T8UlD8bhZBsA8pWIcSpPrGpItvUSEk1ZAMPfZBr6B7S5vXRUqzbpxJciSGFZCCWwRei8laoSqmCreAhYgXWva680ftzeZB89S9gbqZBCdPm5Tf0jcxxFQZDZD'
VERIFY_TOKEN = 'TESTINGTOKEN'
bot = Bot(ACCESS_TOKEN)
contestFile = 'contest.json'
# broker_url = 'pyamqp://guest@localhost//' for localhost
broker_url1 = 'amqp://etpgqwsn:[email protected]/etpgqwsn'
app = Celery('celery_tasks' ,broker=broker_url1)
def readFromFile():
with open(contestFile) as json_file:
return json.load(json_file)
def searchInJSON(platform,startTime,endTime):
contests = readFromFile()
tempContests = []
for contest in contests:
if contest in platform:
tempContests.append(contest)
def send_message_from_messenger(recipient_id, response):
#sends user the text message provided via input response parameter
bot.send_text_message(recipient_id, response)
return "success"
def send_message_from_zulip(recipient_id, response):
req = {
"type": "private",
"to": recipient_id,
"content": response
}
client.send_message(req)
def process_contests(contests):
idx = 0
arr = []
for contest in contests:
arr.append(contest['name'] + ' on ' + contest['platform'] + ' starting at ' + str(contest['startTime']) + ' and ending at ' + str(contest['endTime']) + '. Register at [link](' + contest['link'] + ').\n')
idx+=1
return arr
@app.task
def reminder(konsa, reply, user):
if konsa == "messenger":
send_message_from_messenger(user, reply)
elif konsa == "zulip":
send_message_from_zulip(user, reply)