Skip to content

Commit

Permalink
add slack bot
Browse files Browse the repository at this point in the history
  • Loading branch information
madawei2699 committed Mar 10, 2023
1 parent 8fee112 commit 2113852
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 7 deletions.
113 changes: 113 additions & 0 deletions docs/chatGPT/CDDR.md
Original file line number Diff line number Diff line change
Expand Up @@ -957,3 +957,116 @@ if __name__ == "__main__":
```

以上代码示例为参考,其中某些变量名可能需要替换或者直接以 URL 给出,供参考。

---

You
---



I want to create a slack bot, when user @ this bot and send a message, this bot can response the user something text message, can u give me how to implement this bot?

ChatGPT
-------

Certainly, I can help you with that! Here are the steps to create a Slack bot that responds to messages:

1. Create a new bot in Slack by going to [https://api.slack.com/apps](https://api.slack.com/apps) and clicking on "Create New App".
2. Give your app a name and select the workspace where you want to create the bot.
3. Click on "Bot" in the menu on the left-hand side, and then click on "Add a Bot User". This will create a bot user for your app.
4. Copy the bot user OAuth access token that Slack gives you. You will need this in order to authenticate your code when communicating with Slack.
5. Next, create a new file and name it something like `bot.py` or `slackbot.py`.
6. In your Python code, import the `slack-sdk` and `slack-bolt` packages:

```python
from slack_sdk import WebClient
from slack_bolt import App
```

7. Create a new instance of the `WebClient` class using the bot user OAuth access token:

```python
client = WebClient(token="YOUR_TOKEN_HERE")
```

8. Create a new instance of the `App` class:

```python
app = App(token="YOUR_TOKEN_HERE")
```

9. Define a function that the bot will use to respond to messages. This function should take in a `payload` argument containing information about the message that was sent to the bot. For example:

```python
def handle_message(payload):
message = payload["event"]["text"]
channel_id = payload["event"]["channel"]

if "hello" in message.lower():
response = "Hi there!"
client.chat_postMessage(channel=channel_id, text=response)
```

10. Use the `App` class to define a route that the bot will use to listen for messages. The route should be triggered whenever a user sends a message that starts with the bot's name or mention, using the `@` symbol. For example:

```python
@app.event("app_mention")
def handle_mention(payload):
handle_message(payload)
```

11. Run the application using `app.run()`. This will start the bot and allow it to listen for messages.

That's it! Your bot is now ready to respond to messages in Slack. Of course, you can modify the `handle_message` function to include any other logic you'd like your bot to have.

You
---



actually I want to use flask to integrate with the slack event, how to do that?

ChatGPT
-------

Sure, I can help you with that!

Here's an overview of how to create a Slack bot using Flask:

1. Create a bot in your Slack Workspace: Go to [https://api.slack.com/apps](https://api.slack.com/apps) and create a new app or select an existing one. Then create a "Bot User" under the "Bot Users" section.

2. Install the Bot in your workspace: After creating the bot user, you should install the bot in one or more of your Slack channels. To do this go to the "Install App" section under settings.

3. Setup Flask environment: Open up your terminal or command prompt, and create a new folder for your project. Navigate into the folder, and create a virtual environment by running the command `python -m venv env`. Activate your virtual environment by running `source env/bin/activate` on Mac/Linux or `.\env\Scripts\activate` on Windows.

4. Install Dependencies: With your virtual environment activated, you need to install flask and slack-sdk modules. You can do that by running `pip install flask slack-sdk`

5. Implement Flask Application: Create a new python file (i.e., `app.py`) inside your project folder having the following code:


```python
from slack_bolt import App # Importing Slack Bolt

app = App(token="xoxb-<your-bot-token>", signing_secret="<your-signing-secret>")

@app.event("app_mention")
def handle_mentions(context):
print(context)

if __name__ == "__main__":
app.start(port=5000) # Run Flask server
```

Note: Replace `xoxb-<your-bot-token>` with your Bot User OAuth Access Token value, and `<your-signing-secret>` with your Signing Secret value which can be found in the Slack App Credentials.

6. Start Your Flask App: Run the application by executing `python app.py` in your terminal. This will start a Flask server listening on port 5000.

7. Configure Event Subscription: Go to your App's configuration page and enable `Event Subscriptions`. Using `Request URL`, enter your Ngrok URL concatenated with `/slack/events` (e.g., "[https://example.ngrok.io/slack/events](https://example.ngrok.io/slack/events)").

8. Subscribe to Bot Events: Under Event Subscription, subscribe to `app_mention` event.

9. Testing: Go to one of the Slack channels where you installed the bot and mention it in a message. The bot should respond with a message as handled in the `handle_mentions()` function.


That's it! You have successfully created a Slack bot and integrated it with Flask. You can modify the `handle_mentions()` function to do something according to your requirements.
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ llama-index==0.4.24
httpx==0.23.3
requests==2.28.2
openai==0.27.1
trafilatura==1.4.1
trafilatura==1.4.1
slackclient==2.9.4
slackeventsapi==3.0.1
28 changes: 22 additions & 6 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
import re
import os
import openai
import slack
from slackeventsapi import SlackEventAdapter
from llama_index import GPTSimpleVectorIndex, LLMPredictor, TrafilaturaWebReader
from langchain.chat_models import ChatOpenAI

app = Flask(__name__)

OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
openai.api_key = OPENAI_API_KEY

app = Flask(__name__)
SLACK_TOKEN = os.environ.get('SLACK_TOKEN')
SLACK_SIGNING_SECRET = os.environ.get('SLACK_SIGNING_SECRET')
slack_event_adapter = SlackEventAdapter(SLACK_SIGNING_SECRET, '/slack/events', app)

client = slack.WebClient(token=SLACK_TOKEN)

def insert_space(text):

Expand Down Expand Up @@ -38,10 +45,7 @@ def extract_urls(text):
urls.append(word)
return urls


@app.route('/chat', methods=['POST'])
def chat():
message = request.json['message']
def get_answer_from_chatGPT(message):
message_normalized = insert_space(message)
urls = extract_urls(message_normalized)
if len(urls) > 0:
Expand All @@ -56,10 +60,22 @@ def chat():
)
print(completion.usage)
response = completion.choices[0].message.content
return response

response = {"response": f"{response}"}
@app.route('/chat', methods=['POST'])
def chat():
response = {"response": f"{get_answer_from_chatGPT(request.json['message'])}"}
return jsonify(response)

@slack_event_adapter.on('app_mention')
def handle_mentions(payload):
event = payload.get('event', {})
channel = event.get('channel')
user = event.get('user')
text = event.get('text')
user_message = text.replace('@my-gpt-reader-bot', '')
client.chat_postMessage(channel=channel, link_names=1, text=f'<@{user}>, ' + get_answer_from_chatGPT(user_message))


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

0 comments on commit 2113852

Please sign in to comment.