Last active
June 10, 2020 10:16
-
-
Save shivasiddharth/b743140fb351169957e60a95c617e709 to your computer and use it in GitHub Desktop.
Python script for Raspberry Pi chatbot server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import socket | |
import sys | |
import RPi.GPIO as GPIO | |
import _thread | |
import datetime | |
import random | |
import requests | |
import os | |
GPIO.setwarnings(False) | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setup(11,GPIO.OUT) | |
#Leave host address blank | |
host = '' | |
#Set host port | |
port = 8888 | |
address = (host, port) | |
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
server_socket.bind(address) | |
server_socket.listen(5) | |
#Variable for the number of connections | |
numbOfConn = 0 | |
#Name of list used for connections | |
addressList = [] | |
clients = set() | |
#small database of our bot | |
greetings = ['hola', 'hello', 'hi', 'hey', 'howdy'] | |
wellness_questions = ['how are you', 'how are you doing'] | |
wellness_responses = ['i am doing great', 'i am fine. thanks', 'all good here'] | |
covid_questions = ['where have you been', 'have you gone out anywhere', 'did you walk around'] | |
covid_answers = ['with corona around i do not dare to keep a foot out'] | |
print("Listening for client ..........") | |
#Chat code processing | |
def chatterbox(data): | |
if data in covid_questions: | |
random_covid = random.choice(covid_answers) | |
print(random_covid) | |
sclient(random_covid) | |
elif data in wellness_questions: | |
random_response = random.choice(wellness_responses) | |
print(random_response) | |
sclient(random_response) | |
elif data in greetings: | |
random_greeting = random.choice(greetings) | |
print(random_greeting) | |
sclient(random_greeting) | |
elif 'light on'in data or 'led on' in data: | |
sclient("light turn on") | |
GPIO.output(11,True) | |
print("Light on") | |
elif 'light off' in data or 'led off' in data: | |
sclient("light turn off") | |
GPIO.output(11,False) | |
print("Light Off") | |
elif 'time' in data: | |
now = datetime.datetime.now() | |
time=str(now.hour)+":"+str(now.minute) | |
print(time) | |
sclient(time) | |
elif 'date'in data: | |
now = datetime.datetime.now() | |
date=str("%s/%s/%s" % (now.month,now.day,now.year)) | |
print(date) | |
sclient(date) | |
else: | |
conn.send(b"Something went wrong. I am unable to help you with that right now.") | |
add_data = open("newdata.txt", 'a') | |
add_data.write("\n") | |
add_data.write(data) | |
add_data.close() | |
#Send reply | |
def sclient(mess): | |
for c in clients: | |
try: | |
c.send(mess.encode()) | |
except: | |
c.close() | |
#Server code | |
def clientthread(conn,addressList): | |
while True: | |
output = conn.recv(2048); | |
if output.strip() == "disconnect": | |
conn.close() | |
sys.exit("Received disconnect message. Shutting down.") | |
conn.send("connection loss") | |
elif output: | |
print("Message received from client:") | |
data = output.decode() | |
data=str(data).lower() | |
print(data) | |
print("Reply from the server:") | |
chatterbox(data) | |
while True: | |
#Accept connections | |
conn, address = server_socket.accept() | |
print("Connected to client at ", address) | |
clients.add(conn) | |
#Creat new thread for client connections | |
_thread.start_new_thread(clientthread,(conn,addressList)) | |
conn.close() | |
sock.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Link to Client APK and MIT App Inventor file : https://mega.nz/folder/vo53EBJD#SFc6GsZ4un7vJmYWJVzapw