How to use the web dev module to send a POST to LINE API?

I have an Ignition client that wants to send messages to LINE, a freeware app for instant communications on electronic devices. LINE offers an API that I should be able to interface with from the Ignition web dev module. This is a screenshot from the developers page.

curl -v -X POST https://api.line.me/v2/bot/message/push \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer {channel access token}' \
-H 'X-Line-Retry-Key: {UUID}' \
-d '{
    "to": "U4af4980629...",
    "messages":[
        {
            "type":"text",
            "text":"Hello, world1"
        },
        {
            "type":"text",
            "text":"Hello, world2"
        }
    ]
}'

It seems like I need to setup a bot to deal with incoming messages but I also need to package the data above into the following function:

I found this link here.

but I don't see how to put the request body together in the example.

import system
import urllib2

# Generate URL for API call to read products

url = 'https://api.line.me/v2/bot/message/push'

# Create header with information required by API

urlHeader = {“Content-Type”: “application/json”, “Authorization”: “XXXXX”, “X-Line-Retry-Key”: “XXXXX”}

request = urllib2.Request(url, headers=urlHeader)
contents = urllib2.urlopen(request).read()

data = system.util.jsonDecode(contents)

It seems like I should be able to use httpPost but I am not sure how to package the required information into the function.
https://docs.inductiveautomation.com/display/DOC81/system.net.httpPost

Any input on this issue is appreciated.

Use no python libraries. Use no deprecated functions. Use system.net.httpClient(). (Lots of posts here on the forum with examples.)

Is system.net.httpPost deprecated?

Technically, no, but it is missing a bunch of modern functionality. Don't use any of the system.net.http*() functions other than httpClient().

Well damn... it feels like every time I talk to you @pturmel I have to go back and "fix" 5+ projects.

1 Like

Why do you want to use web dev ?
If you're using an external API, you can do that from any script. web dev would be to build the API, not use it.

Or do you need to redirect API calls to another API ?

To answer your question:
Using system.net.httpClient and client.post(), you can specify headers and data.

1 Like

You are right. I didn't need web dev module to accomplish this... just a lot of reading.

Final working code:

headers = {'Authorization': 'Bearer Iyo5Kd_this_is_some_long_string_1cDny'}
data = {"to": "U48_user_id_found_in_webhook_4a4", "messages":[{"type":"text","text":"Hello, world1"}]}
uri = 'https://api.line.me/v2/bot/message/push'
response = system.net.httpClient().post(url = uri, headers=headers, data=data)
print response

Steps to get here:

  1. Create LINE Developers account.

  2. Create an official channel.

  1. For testing create a RequestBin that will accept a webhook.
  1. Enable webook URL created at pipedream in LINE developers account.

image

  1. When a user adds the official LINE account a webhook will be sent to Pipedream where you can identify the userId.

  2. Put the UserId from pipedream into the working code above in an Ignition script console and that user will receive the message.

Now my next problem is how do I collect various UserId as people on the floor add the official LINE account but I may just throw that problem back to the official IT department.

3 Likes