# -*- coding: utf-8 -*-
import time
import vk_api # https://github.com/python273/vk_api/wiki
from Talker import Talker
import goslate
from pprint import pprint
vk = vk_api.VkApi(login='+37345934875', password='ksahUAu87Ays')
vk.auth()
values = {'out': 0, 'count': 100, 'time_offset': 60}
def write_msg(user_id, s):
vk.method('messages.send', {'user_id': user_id, 'message': s})
def read_msg():
response = vk.method('messages.get', values)
return response
talker = Talker()
while True:
response = read_msg()
answer = ''
if response['items']:
msg = response['items'][0]['body']
message = msg.encode('utf8')
pprint(message)
answer = talker.answerUser(message)
if answer == "":
answer = "OK"
values['last_message_id'] = response['items'][0]['id']
for item in response['items']:
write_msg(item[u'user_id'], answer)
response['items'] = ""
time.sleep(1)
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import goslate
class Talker(object):
chatbot = ChatBot("max",
logic_adapters=[
'chatterbot.logic.MathematicalEvaluation'
],
storage_adapter='chatterbot.storage.SQLStorageAdapter',
database='./database.sqlite3')
def __init__(self):
conversation = [
"Hello",
"Hi there!",
"How are you doing?",
"I'm doing great.",
"That is good to hear",
"Thank you.",
"You're welcome.",
"what you want?",
"Lol",
]
self.chatbot.set_trainer(ListTrainer)
self.chatbot.train(conversation)
def answerUser(self, question):
return self.chatbot.get_response(question)