2017-10-18 40 views
0

我用Chatterbot庫創建了我的第一個聊天機器人。現在我想通過Heroku部署它,但這是不可能的。我的chatbot是由一些文件(py,csv,yml,json,txt)組成的。這是結構:Chatterbot圖書館和Heroku部署

botusers(CSV文件) Magghy(PY文件) magghybot(PY文件) Procfile 要求(txt文件) telegramtoken(txt文件) conversation.yml(名爲郎文件夾中) math_words.json(名爲郎文件夾) NLTK(txt文件) 運行時(txt文件)

我創建了一個 「Procfile」(工人:蟒蛇magghybot.py)和 「Requirements.txt」

然後我部署了我的Heroku上的項目並沒有問題。但是當我試圖與我的機器人開始對話時,它並沒有回答我。

當我寫在終端英勇日誌,不會有問題,但僅此字符串:

2017-10-18T10:16:08.079891+00:00 heroku[worker.1]: Starting process with command python magghybot.py 
 
2017-10-18T10:16:08.745016+00:00 heroku[worker.1]: State changed from starting to up 
 
2017-10-18T10:16:10.087633+00:00 heroku[worker.1]: Process exited with status 0 
 
2017-10-18T10:16:10.098959+00:00 heroku[worker.1]: State changed from up to crashed 
 
2017-10-18T10:16:10.100468+00:00 heroku[worker.1]: State changed from crashed to starting 
 
2017-10-18T10:16:14.445838+00:00 heroku[worker.1]: Starting process with command python magghybot.py 
 
2017-10-18T10:16:14.982759+00:00 heroku[worker.1]: State changed from starting to up 
 
2017-10-18T10:16:15.767656+00:00 heroku[worker.1]: Process exited with status 0 
 
2017-10-18T10:16:15.782460+00:00 heroku[worker.1]: State changed from up to crashed

似乎有與我的文件PY問題。

我magghybot.py有這樣的代碼:

import sys 
 
from inspect import getsourcefile 
 
from os.path import abspath 
 
from chatterbot import ChatBot 
 

 
class MagghyBot(object): 
 
    
 
    def __init__(self, lang = "english"): 
 
     self.language = lang 
 
     self.chatbot = ChatBot(
 
      'MagghyBot', 
 
      logic_adapters=[ 
 
      "chatterbot.logic.MathematicalEvaluation", 
 
      "chatterbot.logic.TimeLogicAdapter", 
 
      "chatterbot.logic.BestMatch" 
 
      ], 
 
      #input_adapter="chatterbot.input.VariableInputTypeAdapter", 
 
      #output_adapter="chatterbot.output.OutputAdapter" 
 
      trainer='chatterbot.trainers.ChatterBotCorpusTrainer' 
 
     ) 
 
     self.instdir = "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/chatterbot_corpus/data" + self.language + "/" 
 
     self.localdir = os.path.abspath(os.path.dirname(sys.argv[0])) + "/lang/" + self.language + "/chatcorpus/" 
 

 
    def train(self): 
 
     if self.checkdirnotempty(self.localdir): 
 
      print(self.localdir) 
 
      self.chatbot.train(
 
       self.localdir 
 
      ) 
 
     elif self.checkdirnotempty(self.instdir): 
 
      print(self.instdir) 
 
      self.chatbot.train(
 
       self.instdir 
 
      ) 
 
     else: 
 
      print("Using standard english corpus") 
 
      self.chatbot.train("chatterbot.corpus.english.greetings") 
 
    
 
    def reply(self, phrase = ""): 
 
     # Get a response to an input statement 
 
     response = self.chatbot.get_response(phrase) 
 
     return response 
 
    
 
    def checkdirnotempty(self, folder = ""): 
 
     check = False 
 
     if os.path.isdir(folder): 
 
      entities = os.listdir(folder) 
 
      for entity in entities: 
 
       if os.path.isfile(folder + entity): 
 
        check = True 
 
        break 
 
     return check

我Magghy.py有這樣的代碼:

import time 
 
import random 
 
import datetime 
 
import telepot 
 
import os 
 
import sys 
 
import subprocess 
 
from magghybot import MagghyBot 
 

 
telegramtoken = '' #telegram bot token from BotFather 
 
checkuserid = 394287240 #enable users whitelist, so only certain people can talk with this bot 
 
usersfile = 'botusers' #the file where we store the list of users who can talk with bot 
 
attemptsfile = '/tmp/attempts.log' #the file where we log denied accesses 
 
active = 1 #if set to 0 the bot will stop 
 

 
language = "italiano" 
 

 
chatter = MagghyBot(language) 
 
chatter.train() 
 

 
if telegramtoken == '' and os.path.isfile("telegramtoken.txt"): 
 
    text_file = open("telegramtoken.txt", "r") 
 
    telegramtoken = text_file.read().replace("\n", "") 
 
    text_file.close() 
 

 
print("Connecting to Telegram...") 
 
bot = telepot.Bot(telegramtoken) 
 
print(bot.getMe()) 
 

 

 

 
    
 
def listusers(): 
 
    if not os.path.isfile(usersfile): 
 
     return '' 
 
    text_file = open(usersfile, "r") 
 
    lines = text_file.read().split(',') 
 
    text_file.close() 
 
    del lines[-1] #remove last element since it is blank 
 
    return lines 
 

 
def adduser(name): 
 
    csv = "" 
 
    users = listusers() 
 
    if users != "": 
 
     for usr in users: 
 
      csv = csv+usr+"," 
 
    csv = csv+name+"," 
 
    text_file = open(usersfile, "w") 
 
    text_file.write(csv) 
 
    text_file.close() 
 
    
 
def deluser(name): 
 
    csv = "" 
 
    users = listusers() 
 
    if users != "": 
 
     for usr in users: 
 
      if usr != name: 
 
       csv = csv+usr+"," 
 
    text_file = open(usersfile, "w") 
 
    text_file.write(csv) 
 
    text_file.close() 
 

 
def handle(msg): 
 
    global bot 
 
    global chatter 
 
    global language 
 
    
 
    chat_id = msg['chat']['id'] 
 
    sender = msg['from']['id'] 
 

 
    users = listusers() 
 

 

 
    if checkuserid == 1: 
 
     verified = 0 
 
     if users != "": 
 
      for usr in users: 
 
       if str(sender) == usr: 
 
        verified = 1 
 
     if verified == 0: 
 
      bot.sendMessage(chat_id, "I don't talk with strangers, dear "+str(sender)) 
 
      #write this user in the list of attempted accesses 
 
      if attemptsfile != '': 
 
       lines = '' 
 
       if os.path.isfile(attemptsfile): 
 
        text_file = open(attemptsfile, "r") 
 
        lines = text_file.read() 
 
        text_file.close() 
 
       lines = lines + str(datetime.datetime.now()) + " --- UserdID: " + str(sender) + " DENIED \n" 
 
       text_file = open(attemptsfile, "w") 
 
       text_file.write(lines) 
 
       text_file.close() 
 
      return 
 
    
 
    command = '' 
 
    
 
     
 
    try: 
 
     if msg['text'] != '': 
 
      command = msg['text'] 
 
      print('Got command: ' + command) 
 
    except: 
 
     print("No text in this message") 
 
     
 

 
    if command == '/time': 
 
     bot.sendMessage(chat_id, str(datetime.datetime.now())) 
 
    elif '/adduser' in command: 
 
     if len(command.split(' ')) > 1: 
 
      usrname = command.split(' ')[1] 
 
      adduser(usrname) 
 
      bot.sendMessage(chat_id, "User "+usrname+" added") 
 
    elif '/deluser' in command: 
 
     if len(command.split(' ')) > 1: 
 
      usrname = command.split(' ')[1] 
 
      deluser(usrname) 
 
      bot.sendMessage(chat_id, "User "+usrname+" deleted") 
 
    elif command == '/help': 
 
     bot.sendMessage(chat_id, "/adduser /deluser /time /exit") 
 
    elif command == '/exit': 
 
     global active 
 
     active = False 
 
     bot.sendMessage(chat_id, "The bot will shutdown in 10 seconds") 
 
    elif command != '': 
 
     answer = chatter.reply(command) 
 
     bot.sendMessage(chat_id, str(answer)) 
 

 

 

 
bot.message_loop(handle) 
 
print('I am listening ...') 
 

 
while active: 
 
    time.sleep(10) 
 
print("Exiting") 
 
sys.exit()

回答

0

我推薦使用Heroku上的數據庫,而不是常規文件來存儲數據。 Heroku是雲主機,每次啓動都可以在不同的機器上進行。這對磁盤使用有一些限制。

這裏是我的實驗項目 - 對Heroku的部署

https://github.com/lisitsky/dj-tg-alpha-bot

Feeel免費準備問它問題的Django電報聊天機器人,它的工作邏輯。

+0

非常感謝,所以我的問題引起的本地路徑(self.instdir和self.localdir)?在積極的情況下,我必須改變整個結構,使用Django。 :( – Adryr83

+0

Django並不是構建Bot的唯一方法,我用它是因爲它提供了一些有用的功能,例如管理面板 –

+0

嘗試將數據保存在數據庫中Python有像'psycopg2'這樣的很好的庫 –