-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat-bot.py
62 lines (31 loc) · 1.18 KB
/
chat-bot.py
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
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
from twilio.rest import Client
# Initialize the ChatBot model
bot = ChatBot('Customer Service Bot')
# Train the ChatBot model with customer service questions and answers
trainer = ChatterBotCorpusTrainer(bot)
trainer.train('chatterbot.corpus.english.greetings',
'chatterbot.corpus.english.conversations')
# Twilio credentials
account_sid = 'YOUR_TWILIO_ACCOUNT_SID'
auth_token = 'YOUR_TWILIO_AUTH_TOKEN'
client = Client(account_sid, auth_token)
# Receive incoming messages from WhatsApp
def receive_message():
messages = client.messages.list(to='whatsapp:+14155238886')
for message in messages:
return message.body
# Respond to incoming messages from WhatsApp
def respond_to_message(message):
response = bot.get_response(message)
client.messages.create(to='whatsapp:+14155238886',
from_='whatsapp:+14155238886',
body=response)
# Main function to run the chatbot
def main():
message = receive_message()
respond_to_message(message)
# Run the chatbot
if __name__ == '__main__':
main()