Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 4774f84

Browse files
Merge pull request #505 from Vivek-GuptaXCode/vivek-patch1
Added a new project : SMS ChatBot.
2 parents 2be7f0d + ae788a4 commit 4774f84

2 files changed

Lines changed: 130 additions & 0 deletions

File tree

projects/SMS_ChatBot/README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# SMS CHATBOT
2+
3+
4+
[![N|Solid](https://imgs.search.brave.com/TwFKAEHis0XNgwUaDKCTjeG0Hs2V46pM9vMyV9-1zGo/rs:fit:500:0:0/g:ce/aHR0cHM6Ly9sb2dv/cy13b3JsZC5uZXQv/d3AtY29udGVudC91/cGxvYWRzLzIwMjEv/MTAvUHl0aG9uLVN5/bWJvbC03MDB4Mzk0/LnBuZw)
5+
6+
## PREREQUISITIES
7+
* A Twilio account - sign up for a free one here - https://www.twilio.com/try-twilio
8+
* A Twilio phone number with SMS capabilities - learn how to buy a Twilio Phone Number here - https://support.twilio.com/hc/en-us/articles/223135247-How-to-Search-for-and-Buy-a-Twilio-Phone-Number-from-Console
9+
* OpenAI Account – make an OpenAI Account here - https://openai.com/api/
10+
* Python installed - download Python here - https://www.python.org/downloads/
11+
* ngrok - https://ngrok.com/download , a handy utility to connect the development version of our Python application running on your machine to a public URL that Twilio can access. This is needed for the development version of the application because your computer is likely behind a router or firewall, so it isn’t directly reachable on the Internet.
12+
13+
## GETTING STARTED
14+
15+
Since you will be installing some Python packages for this project, you will need to make a new project directory and a virtual environment.
16+
17+
If you're using a Unix or macOS system, open a terminal and enter the following commands:
18+
19+
```bash
20+
mkdir chatgpt-sms-python
21+
cd chatgpt-sms-python
22+
python3 -m venv venv
23+
source venv/bin/activate
24+
pip install openai twilio flask python-dotenv
25+
```
26+
27+
If you're following this tutorial on Windows, enter the following commands in a command prompt window:
28+
29+
```bash
30+
mkdir chatgpt-sms-python
31+
cd chatgpt-sms-python
32+
python -m venv venv
33+
venv\Scripts\activate
34+
```
35+
36+
Clone this repository to your local machine.
37+
38+
```bash
39+
git clone https://github.com/<your-username>/python-beginner-projects.git
40+
```
41+
42+
Navigate to the project directory.
43+
44+
```bash
45+
cd projects/SMS_chatBot/
46+
```
47+
48+
Open your terminal there and use this command.
49+
50+
```bash
51+
pip install openai twilio flask python-dotenv
52+
```
53+
54+
The last command uses ```pip```, the Python package installer, to install the four packages that you are going to use in this project, which are:
55+
* The OpenAI Python client library, to send requests to OpenAI's GPT-3 engine.
56+
* The Twilio Python Helper library, to work with SMS messages.
57+
* The Flask framework, to create the web application in Python.
58+
* The python-dotenv package, to read a configuration file.
59+
60+
As mentioned above, this project needs an OpenAI API Key. After making an OpenAI account, you can get an OpenAI API Key here by clicking on ``` + Create new secret key.```
61+
62+
![picture alt](https://assets.cdn.prod.twilio.com/images/cPaoAYgpTi5cTblt2G5s-C2Vvv4xHh1V9rzb-wBX8t0.format-webp.webp)
63+
64+
65+
The Python application will need to have access to this key, so we are going to make a .env file where the API key can safely be stored in the same folder SMS_ChatBot. The application will be able to import this key.
66+
:
67+
68+
>OPENAI_API_KEY= <YOUR-OPENAI-KEY>
69+
70+
Make sure that the OPENAI_API_KEY is safe and that you don't expose your .env file in a public location such as GitHub.
71+
72+
Now, your Flask app will need to be visible from the web so Twilio can send requests to it. ngrok lets you do this. With ngrok installed, run ```ngrok http 5000``` in a new terminal tab in the directory your code is in.
73+
74+
You should see the screen above. Grab that ngrok Forwarding URL to configure your Twilio number: select your Twilio number under Active Numbers in your Twilio console, scroll to the Messaging section, and then modify the phone number’s routing by pasting the ngrok URL in the textbox corresponding to when A Message Comes In as shown below:
75+
76+
![picture alt](https://assets.cdn.prod.twilio.com/images/CqOXGybKIPpoovGDaIb-dnmxlOqYT6OVeBtj356saMA.format-webp.webp)
77+
78+
Click Save and now your Twilio Phone Number is configured so that it maps to your web application server running locally on your computer.
79+
80+
81+
After completing the above process,we are ready to launch our script.
82+
83+
```bash
84+
python main.py
85+
```
86+
87+
if you are using python 3.X then run this command.
88+
89+
```bash
90+
python3 main.py
91+
```
92+
93+
Now take out your phone and text your Twilio Phone Number a question or prompt so that OpenAI can answer it or generate text!
94+
95+
![picture alt](https://assets.cdn.prod.twilio.com/images/Screenshot_2023-01-12_at_1.44.48_PM.format-webp.webp)

projects/SMS_ChatBot/main.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from flask import Flask, request
2+
from twilio.twiml.messaging_response import MessagingResponse
3+
import os
4+
import openai
5+
6+
# Set your OpenAI API key using the environment variable OPENAI_API_KEY
7+
openai.api_key = os.getenv("OPENAI_API_KEY")
8+
9+
app = Flask(__name__)
10+
11+
@app.route("/sms", methods=['POST'])
12+
def chatgpt():
13+
# Get the incoming SMS message
14+
inb_msg = request.form['Body'].lower()
15+
16+
# Print the incoming message for debugging
17+
print(inb_msg)
18+
19+
# Use the OpenAI API to generate a response
20+
response = openai.Completion.create(
21+
model="gpt-3.5-turbo",
22+
prompt=inb_msg,
23+
max_tokens=3000,
24+
temperature=0.7
25+
)
26+
27+
# Create a response using Twilio's MessagingResponse
28+
resp = MessagingResponse()
29+
resp.message(response["choices"][0]["text"])
30+
31+
return str(resp)
32+
33+
if __name__ == "__main__":
34+
# Run the Flask app in debug mode
35+
app.run(debug=True)

0 commit comments

Comments
 (0)