Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import urllib
- from bs4 import BeautifulSoup
- import re
- class ChatBot():
- host = "https://www.botlibre.com"
- endpoint = "/rest/api/form-chat?"
- botID = "165"
- userID = "3063368621053017201"#get this from your account
- conversationID = ""
- history = []
- def __init__(self):
- url = self.makeurl()
- initRequest = requests.get(url)
- soup = self.getsoup(initRequest)
- self.conversationID = soup.response["conversation"]
- self.getmessage(initRequest)
- def makeurl(self, message=""):
- return self.host+self.endpoint+"&application={}&instance={}&conversation={}&message={}".format(self.userID,
- self.botID,
- self.conversationID,
- message)
- def cleanhtml(self, html):
- cleaner = re.compile("<.*?>")
- html = re.sub(cleaner, "", html)
- return html
- def getsoup(self, r):
- return BeautifulSoup(r.text, 'html.parser')
- def getmessage(self, r=None):
- if r:
- soup = self.getsoup(r)
- response = soup
- if soup.message:
- response = soup.message.text
- response = self.cleanhtml(response)
- self.history.append(response)
- elif self.history:
- response = self.history[-1]
- else:
- response = ""
- return response
- def sendmessage(self, message):
- url = self.makeurl(message)
- r = requests.get(url)
- response = self.getmessage(r)
- return response
- if __name__ == "__main__":
- cb = ChatBot()
- while True:
- botResponse = cb.getmessage()
- print(botResponse)
- message = raw_input(">>> ")
- message = urllib.quote_plus(message)
- cb.sendmessage(message)
Advertisement
Add Comment
Please, Sign In to add comment