Guest User

Untitled

a guest
May 23rd, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1. import requests
  2. import urllib
  3. from bs4 import BeautifulSoup
  4. import re
  5.  
  6. class ChatBot():
  7.  
  8.     host = "https://www.botlibre.com"
  9.     endpoint = "/rest/api/form-chat?"
  10.     botID = "165"
  11.     userID = "3063368621053017201"#get this from your account
  12.     conversationID = ""
  13.     history = []
  14.  
  15.     def __init__(self):
  16.         url = self.makeurl()
  17.         initRequest = requests.get(url)
  18.         soup = self.getsoup(initRequest)
  19.         self.conversationID = soup.response["conversation"]
  20.         self.getmessage(initRequest)
  21.  
  22.     def makeurl(self, message=""):
  23.         return self.host+self.endpoint+"&application={}&instance={}&conversation={}&message={}".format(self.userID,
  24.                                                                                                 self.botID,
  25.                                                                                                 self.conversationID,
  26.                                                                                                 message)
  27.  
  28.     def cleanhtml(self, html):
  29.       cleaner = re.compile("<.*?>")
  30.       html = re.sub(cleaner, "", html)
  31.       return html
  32.  
  33.     def getsoup(self, r):
  34.         return BeautifulSoup(r.text, 'html.parser')
  35.  
  36.     def getmessage(self, r=None):
  37.         if r:
  38.             soup = self.getsoup(r)
  39.             response = soup
  40.             if soup.message:
  41.                 response = soup.message.text
  42.             response = self.cleanhtml(response)
  43.             self.history.append(response)
  44.         elif self.history:
  45.             response = self.history[-1]
  46.         else:
  47.             response = ""
  48.         return response
  49.  
  50.     def sendmessage(self, message):
  51.         url = self.makeurl(message)
  52.         r = requests.get(url)
  53.         response = self.getmessage(r)
  54.         return response
  55.        
  56.  
  57.  
  58.  
  59. if __name__ == "__main__":
  60.  
  61.     cb = ChatBot()
  62.  
  63.     while True:
  64.         botResponse = cb.getmessage()
  65.         print(botResponse)
  66.         message = raw_input(">>> ")
  67.         message = urllib.quote_plus(message)
  68.         cb.sendmessage(message)
Advertisement
Add Comment
Please, Sign In to add comment