GYH commited on
Commit
0e9aa07
·
1 Parent(s): 7dd2893

Add file rag/svr/discord_svr.py (#1008)

Browse files

### What problem does this PR solve?

File rag/svr/discord_svr.py is for discord bot.

### Type of change

- [x] New Feature (non-breaking change which adds functionality)

Files changed (1) hide show
  1. rag/svr/discord_svr.py +80 -0
rag/svr/discord_svr.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+ import discord
17
+ import requests
18
+ import base64
19
+ import asyncio
20
+
21
+ URL = '{YOUR_IP_ADDRESS:PORT}/v1/api/completion_aibotk' # Default: https://demo.ragflow.io/v1/api/completion_aibotk
22
+
23
+ JSON_DATA = {
24
+ "conversation_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxx", # Get conversation id from /api/new_conversation
25
+ "Authorization": "ragflow-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx", # RAGFlow Assistant Chat Bot API Key
26
+ "word": "" # User question, don't need to initialize
27
+ }
28
+
29
+ DISCORD_BOT_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxx" #Get DISCORD_BOT_KEY from Discord Application
30
+
31
+
32
+ intents = discord.Intents.default()
33
+ intents.message_content = True
34
+ client = discord.Client(intents=intents)
35
+
36
+
37
+ @client.event
38
+ async def on_ready():
39
+ print(f'We have logged in as {client.user}')
40
+
41
+
42
+ @client.event
43
+ async def on_message(message):
44
+ if message.author == client.user:
45
+ return
46
+
47
+ if client.user.mentioned_in(message):
48
+
49
+ if len(message.content.split('> ')) == 1:
50
+ await message.channel.send("Hi~ How can I help you? ")
51
+ else:
52
+ JSON_DATA['word']=message.content.split('> ')[1]
53
+ response = requests.post(URL, json=JSON_DATA)
54
+ response_data = response.json().get('data', [])
55
+ image_bool = False
56
+
57
+ for i in response_data:
58
+ if i['type'] == 1:
59
+ res = i['content']
60
+ if i['type'] == 3:
61
+ image_bool = True
62
+ image_data = base64.b64decode(i['url'])
63
+ with open('tmp_image.png','wb') as file:
64
+ file.write(image_data)
65
+ image= discord.File('tmp_image.png')
66
+
67
+ await message.channel.send(f"{message.author.mention}{res}")
68
+
69
+ if image_bool:
70
+ await message.channel.send(file=image)
71
+
72
+
73
+ loop = asyncio.get_event_loop()
74
+
75
+ try:
76
+ loop.run_until_complete(client.start(DISCORD_BOT_KEY))
77
+ except KeyboardInterrupt:
78
+ loop.run_until_complete(client.close())
79
+ finally:
80
+ loop.close()