-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot_reply.py
41 lines (36 loc) · 1.25 KB
/
bot_reply.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
import eliza
import random
import time
from cleverwrap import CleverWrap
from threading import RLock
class BotReply():
def __init__(self, cleverbot_token):
self.cleverbot_token = cleverbot_token
self.cleverbots = {}
self.cleverbots_lock = RLock()
def _init_cleverbot(self, tid):
with self.cleverbots_lock:
if tid in self.cleverbots:
return self.cleverbots[tid]
cleverbot = CleverWrap(self.cleverbot_token)
self.cleverbots[tid] = cleverbot
return cleverbot
@staticmethod
def _reply_time(message, delay=2, cpm=500, noise=3):
typing_time = 60 / cpm * len(message)
noise_time = random.random() * noise
return delay + typing_time + noise_time
def _compute_reply(self, tid, message):
cleverbot = self._init_cleverbot(tid)
try:
return cleverbot.say(message)
except:
return eliza.analyze(message)
def get_reply(self, tid, message):
received = time.time()
reply = self._compute_reply(tid, message)
reply_time = self._reply_time(reply)
wait_time = received + reply_time - time.time()
if wait_time > 0:
time.sleep(wait_time)
return reply