-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurvey_runner.py
145 lines (121 loc) · 7.13 KB
/
survey_runner.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
import json
import model
import os
import time
def process(message_in_id):
if (message_in_id is not None):
print("Process message_in_id : %s" % (message_in_id))
msgInMod = model.MessageIn()
msgOutMod = model.MessageOut()
statusPanelMod = model.PatternControl()
errorMsgMod = model.ErrorMessage()
surveyMod = model.Survey()
msg = msgInMod.getById(message_in_id)
if (msg['flag'] == 'just_arrived'):
msgInMod.updateFlag(msg['id'], 'read')
pattern_control = statusPanelMod.getPathControlByChatId(msg['chat_id'])
if(pattern_control['current_processed'] == 'tmp_survey'):
survey = surveyMod.getById(pattern_control['temp_survey_id'])
if (survey is None):
statusPanelMod.changeCurrentSurveyId(msg['chat_id'], 'NULL')
statusPanelMod.changeCurrentSurveyQuestionId(msg['chat_id'], 'NULL')
statusPanelMod.changeCurrentProcessed(msg['chat_id'], 'idle')
msgOutMod.insert(msg['chat_id'], 'text', 'Survei saat ini tidak ada atau telah selesai')
return 1
if(str(msg['content']).lower() == 'ya'):
statusPanelMod.changeCurrentProcessed(msg['chat_id'], 'survey')
statusPanelMod.changeCurrentSurveyId(msg['chat_id'], pattern_control['temp_survey_id'])
statusPanelMod.changeTempSurveyId(msg['chat_id'],"NULL")
pattern_control = statusPanelMod.getPathControlByChatId(msg['chat_id'])
elif(str(msg['content']).lower() == 'tidak'):
statusPanelMod.changeCurrentProcessed(msg['chat_id'], 'idle')
statusPanelMod.changeTempSurveyId(msg['chat_id'], "NULL")
msgOutMod.insert(msg['chat_id'], 'text', 'Survei dibatalkan')
msgInMod.updateFlag(msg['id'], 'processed')
model.MessageInQueueSurvey().deleteMessage(message_in_id)
return 1
else:
errorMsgMod.sendToMsgOut(msg['chat_id'], msg['message_id'], 3)
msgInMod.updateFlag(msg['id'], 'processed')
model.MessageInQueueSurvey().deleteMessage(message_in_id)
return 1
elif(pattern_control['current_processed'] == 'survey'):
survey = surveyMod.getById(pattern_control['current_survey_id'])
if (survey is None):
statusPanelMod.changeCurrentSurveyId(msg['chat_id'], 'NULL')
statusPanelMod.changeCurrentSurveyQuestionId(msg['chat_id'], 'NULL')
statusPanelMod.changeCurrentProcessed(msg['chat_id'], 'idle')
msgOutMod.insert(msg['chat_id'], 'text', 'Survei saat ini tidak ada atau telah selesai')
return 1
question = surveyMod.getQuestionById(pattern_control['current_survey_question_id'])
if(question['is_closed']):
answers = str(question['answers']).lower().replace(" ","").split(",")
if(str(msg['content']).lower().replace(" ","") in answers):
surveyMod.insertRespond(pattern_control['current_survey_id'],
pattern_control['current_survey_question_id'],
msg['chat_id'], msg['content'])
else:
errorMsgMod.sendToMsgOut(msg['chat_id'], msg['message_id'], 3)
msgInMod.updateFlag(msg['id'], 'processed')
model.MessageInQueueSurvey().deleteMessage(message_in_id)
return 1
else:
surveyMod.insertRespond(pattern_control['current_survey_id'],
pattern_control['current_survey_question_id'],
msg['chat_id'], msg['content'])
finish = True
questions = surveyMod.getQuestionBySurveyId(pattern_control['current_survey_id'])
temp = 1
for question in questions:
respond = surveyMod.countRespond(question['id'], msg['chat_id'])
if(respond is None or respond<1):
finish = False
statusPanelMod.changeCurrentSurveyQuestionId(msg['chat_id'], question['id'])
if(question['is_closed']):
answerOption = []
answers = str(question['answers']).split(",")
for answer in answers:
answerOption.append({'markup_type':'text', 'answer':answer})
content = ("<b>Pertanyaan %i dari %i pertanyaan</b> \n\n"%(temp, len(questions))+
question['question']+"\n\n"+
"<i>Pilih jawaban yang ada pada tombol markup</i>\n"+
"/clear untuk keluar dari operasi")
msgOutMod.insert(msg['chat_id'], 'text', content, is_replymarkup=1, reply_markup=answerOption)
else:
content = ("<b>Pertanyaan %i dari %i pertanyaan</b> \n\n" % (temp, len(questions)) +
question['question'] + "\n\n" +
"<i>Silahkan kirim jawaban Anda</i>\n"+
"/clear untuk keluar dari operasi")
msgOutMod.insert(msg['chat_id'], 'text', content)
break
temp+=1
if(finish):
statusPanelMod.changeCurrentSurveyId(msg['chat_id'],'NULL')
statusPanelMod.changeCurrentSurveyQuestionId(msg['chat_id'], 'NULL')
statusPanelMod.changeCurrentProcessed(msg['chat_id'], 'idle')
msgOutMod.insert(msg['chat_id'], 'text', 'Terimakasih telah mengikuti survei.')
msgOutMod.insert(msg['chat_id'], 'text', 'Hasil survei dapat dilihat pada <a href="https://chatbot.citizenlab.web.id/survei/%i">link ini</a>.' % (pattern_control['current_survey_id']))
msgInMod.updateFlag(msg['id'], 'processed')
model.MessageInQueueSurvey().deleteMessage(message_in_id)
else:
model.MessageInQueueSurvey().deleteNullMessage()
def main():
start_time = time.time()
try:
model.ConRecord().insert(int(os.getpid()), "survey_runner")
except:
print("Can't create session")
else:
while round(time.time() - start_time) < 360:
try:
queues = model.MessageInQueueSurvey().getMessage()
if queues is not None:
for queue in queues:
process(queue[0])
except Exception as e:
print("Something problem : %s" % (e))
time.sleep(0.3)
model.ConRecord().updateOff(int(os.getpid()))
os.system('nohup /home/citizenl/virtualenv/engine__python/3.5/bin/python /home/citizenl/engine_python_new/survey_runner.py >> survey.log &')
if __name__ == '__main__':
main()