3
votes

I am a beginner to the Django framework and I am building a Django app that uses the Slack RTM API.

I have a coded a program in python that performs the OAuth authentication process like so :

def initialize():
  url="https://slack.com/api/rtm.connect"
  payload={"token":"xxx"}
  r=requests.post(url,payload)
  res=json.loads(r.text)
  url1=res['url']
  ws = create_connection(url1)
  return ws

My Requirement:

The stream of events I receive (from my slack channel that my slack app is added to) is processed to filter out events of the type - message ,then match the message with a regex pattern and then store the matched string in a database.

As a stand alone python program I am receiving the stream of events from my channel.

My questions:

  1. How do I successfully integrate this code to Django so that I can fulfill my requirement?

  2. Do I put the code in templates/views? What is the recommended method to process this stream of data?

1

1 Answers

0
votes
def initialize():
  url = "https://slack.com/api/rtm.connect"
  r = requests.get(url, params={'token': '<YOUR TOKEN>'})
  res = r.json()
  url1=res['url']
  ws = create_connection(url1) #Note: There is no function called create_connnection() so it will raise an error
  return ws

if you read the API web methods, you see :

Preferred HTTP method: GET

See here: Slack rtm.connect method

look at the comment, and thats the right code, see the differences between this code and yours.

basically to get JSON from a request don't use json.loads because this search your local computer not the request use r.json() so it call the json you got from r.

Note that r.text will return raw text output so when you want to get url it will not be identified, with r.json you can call the object url as stated about

Hope this help.

and please could you tell us more what you wanna do with this in view ? because template is a directory which contains all the HTML files which you don't need to work with.

but why views.py ?