Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /www/eksidoio_182/public/wp-includes/formatting.php on line 3545

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/eksidoio_182/public/wp-includes/kses.php on line 1805

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /www/eksidoio_182/public/wp-includes/formatting.php on line 3545

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/eksidoio_182/public/wp-includes/kses.php on line 1805

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /www/eksidoio_182/public/wp-includes/formatting.php on line 3545

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/eksidoio_182/public/wp-includes/kses.php on line 1805

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /www/eksidoio_182/public/wp-includes/formatting.php on line 3545

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/eksidoio_182/public/wp-includes/kses.php on line 1805
Build an automated, AI-Powered Slack Chatbot with ChatGPT using Flask - VEP Test Site
Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /www/eksidoio_182/public/wp-includes/formatting.php on line 3545

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/eksidoio_182/public/wp-includes/kses.php on line 1805

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /www/eksidoio_182/public/wp-includes/formatting.php on line 3545

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/eksidoio_182/public/wp-includes/kses.php on line 1805

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /www/eksidoio_182/public/wp-includes/formatting.php on line 3545

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/eksidoio_182/public/wp-includes/kses.php on line 1805

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /www/eksidoio_182/public/wp-includes/formatting.php on line 3545

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/eksidoio_182/public/wp-includes/kses.php on line 1805

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /www/eksidoio_182/public/wp-includes/formatting.php on line 3545

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/eksidoio_182/public/wp-includes/kses.php on line 1805

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /www/eksidoio_182/public/wp-includes/formatting.php on line 3545

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/eksidoio_182/public/wp-includes/kses.php on line 1805

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /www/eksidoio_182/public/wp-includes/formatting.php on line 3545

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/eksidoio_182/public/wp-includes/kses.php on line 1805

Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /www/eksidoio_182/public/wp-includes/formatting.php on line 3545

Deprecated: preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/eksidoio_182/public/wp-includes/kses.php on line 1805

Build an automated, AI-Powered Slack Chatbot with ChatGPT using Flask

In this blog, we will discover how to build a Slack bot, add it to our Slack channel, and receive text replies from ChatGPT.

Step 1: Create a Slack Bot

Slack Bot must be created in order to automate messages with ChatGPT. Please follow the directions from steps 1 to 23 in our blog post Slack Bot with Python. Before moving forward, please make sure that you followed the blog’s instructions.

Step 2: ChatGPT API

Please view our recent blog post, WhatsApp Chatbot With ChatGPT Step 2 “ChatGPT API”, which includes guidelines for configuring and utilising ChatGPT

Step 3: Integrate ChatGPT API with Flask Application

It’s time to integrate our ChatGPT API with the Flask application once its installation is successful.

However, the ChatGPT API will continue to run in the Firefox browser’s background, which could interfere with receiving responses from the ChatGPT API, therefore we must end the process that is currently running the browser.

def process():
try:

# iterating through each instance of the process
for line in os.popen("ps ax | grep firefox | grep -v grep"):
fields = line.split()

# extracting Process ID from the output
pid = fields[0]

# terminating process
os.kill(int(pid), signal.SIGKILL)
print("Process Successfully terminated")

except:

The flask application we developed in step 1 has to be modified now. To receive ChatGPT’s response to user messages, replace the existing code in your Flask app with the following code.

import slack
import os, signal

from flask import request
from flask import Flask
from chatgpt_wrapper import ChatGPT
from slackeventsapi import SlackEventAdapter

SLACK_TOKEN="<Your TOKEN>"
SIGNING_SECRET="<Your SIGNING SECRET>"

app = Flask(__name__)
slack_event_adapter = SlackEventAdapter(SIGNING_SECRET, '/slack/events', app)

@ slack_event_adapter.on('message')
def message(payload):
print(payload)
client = slack.WebClient(token=SLACK_TOKEN)

try:
if request.method == 'POST':
event = payload.get('event', {})

if event['client_msg_id']:
channel_id = event.get('channel')
user_id = event.get('user')
text = event.get('text')
print(text)
bot = ChatGPT()
chatbot_res = bot.ask(text)
process()
print("ChatGPT Response=>",chatbot_res)
client.chat_postMessage(channel=channel_id,text=chatbot_res)

return chatbot_res

except Exception as e:
print(e)
pass
return '200 OK HTTPS.'

if __name__ == "__main__":
app.run(debug=True)

Note:
Run the flask application in the terminal: python SCRIPT_NAME.py

Run the ngrok on terminal: ngrok http 5000

Step 4: Test Slack Chatbot

To receive the ChatGPT-generated response, kindly send some messages to the Slack bot. On a server, you will also receive a response.

Here is the ChatGPT response on our server.

We will get the below response on our Slack Bot.

Originally published at Build An Automated, AI-Powered Slack Chatbot With ChatGPT Using Flask on January 10, 2023.


Build an automated, AI-Powered Slack Chatbot with ChatGPT using Flask was originally published in Chatbots Life on Medium, where people are continuing the conversation by highlighting and responding to this story.


Posted

in

by

Tags: