-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot-aiml.py
More file actions
103 lines (85 loc) · 1.86 KB
/
bot-aiml.py
File metadata and controls
103 lines (85 loc) · 1.86 KB
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
#!/usr/bin/env python2
# encoding=utf8
"""
start imports
"""
"""
General utils imports
"""
import os
import os.path
import atexit
import sys
reload(sys)
"""
Time Managmente imports
"""
import time
from datetime import datetime
"""
set aditional path
"""
sys.path.append(os.path.join(os.path.dirname(__file__), 'pyAIML/'))
"""
import bot specific code
"""
import pyAIML
import telebot
"""
set General Options
"""
"""
Set Sys Options
Can't modify
"""
sys.setdefaultencoding('utf8')
"""
Set Boot options
you can modify
"""
TOKEN = os.environ['TELEGRAM_TOKEN']
AIMLName = "comperBot"
BootName = "belinda"
BRAINNAME = "brain.sav"
"""
code core
"""
"""
Set Aiml Brain,
recovery if exist, fetch new predicates and set Data
"""
k = pyAIML.Kernel()
if os.path.isfile(BRAINNAME):
k.loadBrain(BRAINNAME)
k.setTextEncoding("UTF-8")
k.learn(os.path.join(os.path.dirname(__file__), 'AIML/', AIMLName + "/") + "*.aiml")
k.loadSubs(os.path.join(os.path.dirname(__file__), 'AIML/', AIMLName + "/") + AIMLName+ ".ini")
k.setBotPredicate("bot_name", BootName)
"""
create trigger for saving the brian status
"""
atexit.register(lambda : k.saveBrain(BRAINNAME))
"""
Define listener
"""
def listener(*messages):
"""
When new message get will call this function.
:param messages:
:return:
"""
f = open('chat.log', 'a')
for m in messages:
chatid = m.chat.id
if m.content_type == 'text':
text = m.text
response = k.respond(text, chatid)
tb.send_message(chatid, "Escribiste: " + text)
tb.send_message(chatid, response)
f.write(str(datetime.now()) + "\t Chat: " + str(chatid) + "\t Msg: " + text + "\t Respuesta: " + response + "\n")
tb = telebot.TeleBot(TOKEN)
tb.get_update() # cache exist message
tb.set_update_listener(listener) #register listener
tb.polling(3)
while True:
time.sleep(2)