2

I want to implement to send direct message to my twitter friends using Twitter REST API. I am getting all my friends id and put invite button for sending a message from my app.

Following is the function call I am using -

func sendInviteMessage(_ client: TWTRAPIClient, recepientID: String){

        let statusesShowEndpoint = " https://api.twitter.com/1.1/direct_messages/events/new.json"
        let params = ["recipient_id": recepientID,"message_data":"Hello","type":"message_create"]
        var clientError : NSError?

        let request = client.urlRequest(withMethod: "POST", urlString: statusesShowEndpoint, parameters: params, error: &clientError)

        client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
            if connectionError != nil {
                print("Error: \(String(describing: connectionError?.localizedDescription))")
            }

            do {
                let json = try JSONSerialization.jsonObject(with: data!, options: [])
                print("json: \(json)")

            } catch let jsonError as NSError {
                print("json error: \(jsonError.localizedDescription)")
            }
        }

    }

When I tap invite button it gives me unsupported url response.

iHarshad
  • 143
  • 11
  • Required parameters to be send – iHarshad Aug 06 '18 at 09:11
  • @V_rohit - Can u please help me on this – iHarshad Aug 06 '18 at 10:15
  • Pro tip: it looks like most of your questions are this sort of length, and they are just not a good fit for Stack Overflow. We expect questions to set out a problem in detail, and to show something that was tried, or some other prior research/effort. You may find that if a number of your earlier (off-topic) questions are put on hold - and many of them should be - an automatic question ban may be applied to your account. – halfer Aug 06 '18 at 10:16
  • In this case, you could put your title into a search engine and research the problem yourself. You could then try piece of code, make an effort to understand how they work, and ask a question _if_ you run into a problem. – halfer Aug 06 '18 at 10:17
  • show your tried code – Wings Aug 06 '18 at 10:18
  • I am posted the code in question – iHarshad Aug 06 '18 at 10:45

1 Answers1

2

params is wrong. And you can use MutableURLRequest.

https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event#example-request

let params = [
              "event": [
                "type":"message_create",
                "message_create": [ 
                  "target": ["recipient_id": recepientID],
                  "message_data": ["text": "Hello"]
                ]
              ]
            ]
var request = client.urlRequest(withMethod: "POST", 
                                 urlString: statusesShowEndpoint, 
                                parameters: nil, 
                                     error: &clientError)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try? JSONSerialization.data(withJSONObject: params, options: [])
ryo miyake
  • 21
  • 1