[英]Return JSON objects retrieved from Slack with Django REST
I set up a Django REST API with Slack so I could program a support bot.我使用 Slack 设置了一个 Django REST API,这样我就可以编写一个支持机器人。 It's working fine in that I can program new messages and responses.
它工作正常,因为我可以编写新消息和响应。 However I also want to able to return JSON objects that I've retrieved from Slack.
但是,我也希望能够返回从 Slack 检索到的 JSON 对象。
Here's my views.py这是我的 views.py
class Events(APIView):
def post(self, request, *args, **kwargs):
slack_message = request.data
#verify token
if slack_message.get('token') != SLACK_VERIFICATION_TOKEN:
return Response(status=status.HTTP_403_FORBIDDEN)
#checking for url verification
if slack_message.get('type') == 'url_verification':
return Response(data=slack_message, status=status.HTTP_200_OK)
#send a greeting to the bot
if 'event' in slack_message:
#process message if event data is contained in it
event_message = slack_message.get('event')
#ignore bot's own message
if event_message.get('subtype') == 'bot_message':
return Response(status=status.HTTP_200_OK)
#handle the message by parsing the JSON data
user = event_message.get('user')
text = event_message.get('text')
channel = event_message.get('channel')
bot_text = 'Hi <@{}> :wave:'.format(user)
#finally use the slack api to post the message with chat.postMessage
if 'hello' in text.lower():
Client.api_call(method='chat.postMessage',
channel=channel,
text=bot_text)
return Response(status=status.HTTP_200_OK)
return Response(status=status.HTTP_200_OK)
So this works fine, I'm using ngrok
to tunnel to the Slack API.所以这很好用,我使用
ngrok
隧道到 Slack API。 Now I want to return the list of convos in a channel, which I do with conversations.history
:现在我想返回频道中的 convos 列表,我用
conversations.history
.history 来做:
class ConversationArchive(APIView):
def save_conversation(self, request):
conversation_history = Client.api_call(method='conversations.history',
token='xxx',
channel='xxx')
return conversation_history
Now when I check the conversation view in my browser it shows me现在,当我在浏览器中查看对话视图时,它会显示我
HTTP 200 OK
Allow: OPTIONS
Content-Type: application/json
Vary: Accept
{
"name": "Conversation Archive",
"description": "",
"renders": [
"application/json",
"text/html"
],
"parses": [
"application/json",
"application/x-www-form-urlencoded",
"multipart/form-data"
]
}
Which is not the list of conversations, I know my method call on the Slack API is correct because I've tested it in me Events
view by telling the bot to send the conversation_history
in the chat, crude but effective.这不是对话列表,我知道我对 Slack API 的方法调用是正确的,因为我已经在我的
Events
视图中测试了它,告诉机器人在聊天中发送conversation_history
历史,粗略但有效。 So how would I return this aa JSON object with the framework?那么我将如何使用框架返回这个 JSON 对象? Would I need to create and serialize a model?
我需要创建和序列化模型吗?
If you're using Django 1.7+ and if conversation_history is a JSON serializable object, then just provide it as the JsonResponse from the APIView:如果您使用的是 Django 1.7+ 并且 session_history 是一个 JSON 可序列化对象,那么只需将其作为来自 APIView 的 JsonResponse 提供:
from django.http import JsonResponse
class ConversationArchive(APIView):
def save_conversation(self, request):
conversation_history = Client.api_call(method='conversations.history',
token='xxx',
channel='xxx')
return JsonResponse(conversation_history)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.