How to make a HTTP POST to "IFTTT"?

If you have a question or need help, this is the place to be.

How to make a HTTP POST to "IFTTT"?

Postby skribb » Fri May 06, 2016 1:16 pm

IFTTT can now receive web requests and I'd like to use that functionality!

Is there a plugin for that or do I need to script something in Python?

These are the instructions from IFTTT:
Image


Much appreciated :D
Automation is life.

Win7 64bit
EG: r1722
skribb
Experienced User
 
Posts: 157
Joined: Thu Feb 12, 2015 7:22 pm
Location: Win7 64bit

Re: How to make a HTTP POST to "IFTTT"?

Postby kgschlosser » Fri May 06, 2016 1:41 pm

python script, place the URL where is states SOME.URL.HERE

Code: Select all
URL = http:\\"SOME.URL.HERE" (leave the double quotes alone)


import urllib2 as urllib

try:
    response = urllib.urlopen(URL)
    print response
except urllib2.HTTPError as err:
    print err

If you like the work I have been doing then feel free to Image
User avatar
kgschlosser
Site Admin
 
Posts: 2715
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: How to make a HTTP POST to "IFTTT"?

Postby Pako » Sat May 07, 2016 8:12 am

Yes, you can easily create a sort of "EventGhost" channel on IFTTT.
I tried it a long time ago and it works well.
Here are two notes about this:
1) You need to be CONNECTED to Maker channel
2) To make the whole thing had some meaning, you must create a recipe. In that will be used the Maker channel (such "IF") and you can transmit up to three user values.

Here is a Python script with which you can make the desired request.
Code: Select all
import pycurl
from urllib import urlencode
from StringIO import StringIO

event = "EventGhost"                   # any string according to your needs
apikey = "<your Maker channel apikey>" # apikey you receive after signing up Maker channel
value1 = "Test001-value1"
value2 = "Test001-value2"
value3 = "Test001-value3"

def request(method, url, **kwargs):
        hdrs = [
            "X-User-Agent:EventGhost",
            'Accept:*/*',
            "Content-type:application/x-www-form-urlencoded",
        ]
        c = pycurl.Curl()
        if 'params' in kwargs:
            url = url + '?' + urlencode(kwargs['params'])
        c.setopt(pycurl.URL, url)
        c.setopt(pycurl.HTTPHEADER, hdrs)
        if method in ('POST', 'PATCH'):
            post_data = kwargs['data'] if 'data' in kwargs else {}
            postfields = urlencode(post_data)
            c.setopt(pycurl.POSTFIELDS, postfields)
            if method == 'PATCH':
                c.setopt(pycurl.CUSTOMREQUEST, 'PATCH')
        elif method == 'DELETE':
            c.setopt(pycurl.CUSTOMREQUEST, "DELETE")
        buff = StringIO()
        c.setopt(pycurl.WRITEFUNCTION, buff.write)
        c.perform()

        status_code = c.getinfo(pycurl.RESPONSE_CODE)
        c.close()
        resp = buff.getvalue()
        if status_code == 200:
            return (resp, True)
        else:
            eg.PrintError("IFTTT: Request error: %s" % str(resp))
            return (status_code, False)

eg.result = request(
    "POST",
    "https://maker.ifttt.com/trigger/%s/with/key/%s" % (event, apikey),
    data = {"value1" : value1, "value2" : value2, "value3" : value3}
    )


Note:
The "request" function is unnecessarily complicated for this case, it might be simplified. I copied it from some of my plugin.
You may need less than three user values. That depends on your recipe.
In this case, it is necessary to appropriately adjust the line
Code: Select all
data = {"value1" : value1, "value2" : value2, "value3" : value3}
You know flattr ? You can Image
User avatar
Pako
Plugin Developer
 
Posts: 2258
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic

Re: How to make a HTTP POST to "IFTTT"?

Postby skribb » Sun May 08, 2016 2:20 pm

Why did 3 posts get deleted in this thread?
Automation is life.

Win7 64bit
EG: r1722
skribb
Experienced User
 
Posts: 157
Joined: Thu Feb 12, 2015 7:22 pm
Location: Win7 64bit

Re: How to make a HTTP POST to "IFTTT"?

Postby Pako » Sun May 08, 2016 2:41 pm

skribb wrote:Why did 3 posts get deleted in this thread?
Because it is absolutely unrelated to the topic (no "POST to IFTTT").
You can find them in the newly established theme "http post to loxone miniserver" (name suggested V_J).

Pako
You know flattr ? You can Image
User avatar
Pako
Plugin Developer
 
Posts: 2258
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic

Re: How to make a HTTP POST to "IFTTT"?

Postby skribb » Sun May 08, 2016 3:50 pm

Thank you, it would have been nice to get an explanation in the thread so I'd get an email notification tho :)
Automation is life.

Win7 64bit
EG: r1722
skribb
Experienced User
 
Posts: 157
Joined: Thu Feb 12, 2015 7:22 pm
Location: Win7 64bit

Re: How to make a HTTP POST to "IFTTT"?

Postby rdgerken » Thu Jul 20, 2017 4:03 pm

I was unable to get Pako's code to work - some issue with self signed certificate in the chain?? After some farting around, I came up with this simplified version (via stackoverflow) that seems to work fine:

Code: Select all
import requests

event = "EventGhost"               #Your IFTTT Webhook Request EventString
apikey = "xx-xxxxxx-xxxxxxx-xxxx"  #Your IFTTT Webhook API key
value1 = "Test001-value1"
value2 = "Test001-value2"
value3 = "Test001-value3"

url     = "https://maker.ifttt.com/trigger/%s/with/key/%s" % (event, apikey)
payload = {"value1" : value1, "value2" : value2, "value3" : value3}
headers = {}
res = requests.post(url, data=payload, headers=headers)
rdgerken
Experienced User
 
Posts: 85
Joined: Fri Sep 21, 2012 7:41 pm

Re: How to make a HTTP POST to "IFTTT"?

Postby holdestmade » Fri Jul 21, 2017 11:20 am

Thanks @rdgerken, works great for me too.

Simon
holdestmade
 
Posts: 34
Joined: Thu Dec 04, 2014 2:44 pm

Re: How to make a HTTP POST to "IFTTT"?

Postby jachin99 » Wed Sep 13, 2017 7:46 pm

I'm starting to dig into exactly how I can use Google Home to control EventGhost, and that quest has landed me in this thread for the time being. Using webhooks for the That portion of my recipe i have reached a point where IFTTT is asking me for a public facing URL where it can send the command but I have no idea what address to use, or how to link whatever URL I choose to EventGhost. Do I need to setup a Web Server in EG using the appropriate plugin? Any help pointing me in the right direction would be appriciated
jachin99
Experienced User
 
Posts: 217
Joined: Sat Feb 13, 2016 8:39 pm

Re: How to make a HTTP POST to "IFTTT"?

Postby kgschlosser » Wed Sep 13, 2017 10:37 pm

well the question is how does it want to connect.. is it going to do an HTTP, FTP I am guessing you can simply use the socket library and open a port up to see how it connects. because when it does it is going to send some kind of data in and we can simply print out that data to see what it is doing.

the code below will open port 80 for an incoming connection. it will timeout after 5 minutes but you can simply close EG and it will close It may however hand your port as i didn't add all the crap to close it down properly. it will print out the data that comes in from google. i have to buffer set to 8K of data but this will at least let you know what the data is. you can paste this into a script in EG this will bind port 80 so make sure there is nothing else running on that port. I am guessing here that it is port 80 because you used the term URL

Code: Select all


import threading
import traceback
import socket

def run():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.settimeout(600)
    try:
        sock.bind(('0.0.0.0', 80))
    except socket.error:
        traceback.print_exc()
    else:
        try:
            sock.listen(3)
            conn, address = sock.accept()
            data = conn.recv(8128)
            conn.close()
            print 'Connection from:', address[0]
            print data
        except socket.timeout:
            pass

        try:
            sock.close()
        except socket.error:
            pass

threading.Thread(target=run).start()

If you like the work I have been doing then feel free to Image
User avatar
kgschlosser
Site Admin
 
Posts: 2715
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: How to make a HTTP POST to "IFTTT"?

Postby yokel22 » Thu Sep 14, 2017 10:40 am

It's asking for a callback url. Any website that's (front facing) accessible via internet. You can use eventghost.net for your url. Or if you have a dns routed website of your own, you could use that too.

This doesn't pertain to that question. Just a suggestion for what your ultimately trying to achieve. For commands between eg & google home I setup a cludgy iftt setup. Basically I pass the string to a Dropbox file. Then use the folder watcher plugin to monitor that folder for changes. Next the text file gets parsed for the command via python or the file operations plugin.

You could also use the pushbullet, email, or sms plugins with iftt to accomplish the same task. This is cludgy, but works. Using the api is obviously the better route.
yokel22
Experienced User
 
Posts: 130
Joined: Thu Feb 05, 2015 5:56 pm
Location: U.S. - Kansas city

Re: How to make a HTTP POST to "IFTTT"?

Postby jachin99 » Mon Sep 18, 2017 7:53 pm

@kgschlosser, I'm not sure how it wants to connect but I would assume it uses either http or https. I can't see IFTTT using FTP for automation tasks but I cant verify it doesn't either. When you say use the socket library do you mean open a websocket in EG via a python script or open one using python itself. Would any work need to be done on the router or does the code you provide use an already accessible port? When I fill in the data for your code, which IP address should I use as in my public IP or private IP?

yokel22 wrote:It's asking for a callback url. Any website that's (front facing) accessible via internet. You can use eventghost.net for your url. Or if you have a dns routed website of your own, you could use that too.

This doesn't pertain to that question. Just a suggestion for what your ultimately trying to achieve. For commands between eg & google home I setup a cludgy iftt setup. Basically I pass the string to a Dropbox file. Then use the folder watcher plugin to monitor that folder for changes. Next the text file gets parsed for the command via python or the file operations plugin.

You could also use the pushbullet, email, or sms plugins with iftt to accomplish the same task. This is cludgy, but works. Using the api is obviously the better route.


i'd like to keep this as simple as possible so I'm trying to avoid using things like pushbullet, dropbox, etc. About your callback url idea, exactly which device/account would be "calling back" to which other account/device? My understanding is that a callback url is when executable code is sent in response to a web request. Is this right? I don't have any web capabilities setup for EG but if i did setup the web server could I just use an already open port like port 80 as suggested above?

Thanks to all
jachin99
Experienced User
 
Posts: 217
Joined: Sat Feb 13, 2016 8:39 pm

Re: How to make a HTTP POST to "IFTTT"?

Postby yokel22 » Mon Sep 18, 2017 11:05 pm

If it will allow a socket connection, you should be able to get it to work like that. I have a feeling they aren't going to let you leave an open socket connection to their server though. Let me play around with a bit & see if it works the way i'm thinking it does.

If you just need to forward events from IFTTT to an EG webserver. Then the callback is irrelevant, it just needs a valid address. This is the way i have fitbit setup, the callback url isn't necessary for my purposes but it does need a valid address to connect to.

**Edit**

Okay, so i tested it out & it will work if you port forward your ISP assigned IP address(or DNS address). Do it like you would any other webserver get request, shown below. Leaving your port forwarded is definitely less secure than other IFTTT methods. You may shave some time though as the request's seem to be very quick.

Code: Select all
http://{IP}:{PORT}?myEvent&myPayload1&myPayload2


You'll need to setup a webserver plugin instance. The port you choose in the webserver config will be the port you use above. The IP can be determined by googling what's my ip? You'll need to forward the port on your router with the port set in the webserver instance along with the local IP of the computer running EG.

To generate events from EG to IFTT you can do a standard post command. This works on builds 5 RC2 and below, but is throwing an error about the requests module on EG 5 RC4 right now.

Code: Select all
import requests
import json
key = "yourIFTTTWebHooksKey"
event = 'testWebHooks'
payload1 = 'val 1'
payload2 = 'val 2'
payload3 = 'val 3'

url = 'https://maker.ifttt.com/trigger/%s/with/key/%s' % (event, key)
payload = {"value1" : payload1, "value2" : payload2, "value3" : payload3}
headers = {'content-type': 'application/json'}
fowardEvent = requests.post(url, data=json.dumps(payload), headers=headers)


"kgschlosser", this is the error i get with that script on EG 5 RC4. Looks like the requests module just didn't get included in the release.
EG5rc4RequestsError.jpg
Error
Last edited by yokel22 on Tue Sep 19, 2017 2:52 am, edited 2 times in total.
yokel22
Experienced User
 
Posts: 130
Joined: Thu Feb 05, 2015 5:56 pm
Location: U.S. - Kansas city

Re: How to make a HTTP POST to "IFTTT"?

Postby kgschlosser » Tue Sep 19, 2017 12:54 am

Oh not requests is there. but there is an SSL error. I am not sure why it is not using it's stock cert i will have to tinker around a bit with it to find out.
If you like the work I have been doing then feel free to Image
User avatar
kgschlosser
Site Admin
 
Posts: 2715
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: How to make a HTTP POST to "IFTTT"?

Postby yokel22 » Tue Sep 19, 2017 1:02 am

So right you are, right there at the bottom. :)
yokel22
Experienced User
 
Posts: 130
Joined: Thu Feb 05, 2015 5:56 pm
Location: U.S. - Kansas city


Return to General Support

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests