Tuesday, October 11, 2016

Combine Facebook API with Twitter Bot

It has been a while since I last looked at Facebook Graph API. I had a small project that would benefit from crawling a Facebook group, here is what I want to do:

1. Crawl a Facebook group for feed periodically.
2. Parse out the Feeds for a particular person.
3. Tweet to me when that happens.

For #1 and #3, I have already done that before with Twython and AWS Lambda in this post. So I just need to figure out how to crawl Facebook group. Here were the steps I took:

1. Create a Facebook App ID and get App secret. This is pretty easy and self-explanatory.

2. Use the Facebook Graph API Page and Documentation to find the right URI to get the feed that you want. This was the most time consuming as Facebook changes access rights from version to version, and there are differences between App Token and User Token. What I finally found was using the User Token for version 2.2 (latest was 2.7) was what I needed.

Here is the curl example. You can get the group ID from going directly to the Facebook group and looking at the URI. The feed limit is just to speed up the response:

○ → curl -i -X GET \

>  "https://graph.facebook.com/v2.2/<facebook group ID>?fields=feed.limit(10)&access_token=<token>" 

3. The user token is only good for about an hour, you can make it a 60 day long term token. Here was the Facebook LInk, but I find this instruction to be much easier. 

4. Now that I have the feed, I just need to parse it out for message, creation time, and other fields that I wanted and fill in what I did before for Twitter bot and upload to Lambda. One thing that I did wrong was previously using update_status which spammed all of my followers, so I switched to direct_message. Here is the code: 

import requests, pprint, json, re, datetime
from twython import Twython
from twython.exceptions import TwythonError

with open('credentials.json') as f:
  credentials = json.loads(f.read())

client = Twython(credentials["consumer_key"],

url = "https://graph.facebook.com/v2.2/<group>?fields=feed.limit(50)&access_token=<long term access token>"

def lambda_handler(event, context):
    r = requests.get(url)
    for i in r.json()['feed']['data']:
        if i['from']['id'] == '<some user ID>':
            if re.search('shirt', i['message']):
                name = (i['from']['name'])
                createdTime = (i['created_time'])
                message = (i['message'])
                text = <your text>
                client.send_direct_message(screen_name="<some user>", text = text)

Thanks for reading. Happy Coding!

Tuesday, October 4, 2016

Google Coder for Raspberry Pi - for Kids and Adults

My wife sent me this link for Coder for Raspberry Pi a while back, but it wasn't until today (after a friendly reminder) that I began to set it up for her and the kid. The project was created by a small team of Googlers in New York, in their own words:

Well, sounds good to me! Set up was a breeze, at least for Mac it was a zip file that you download. Once unzipped, you get a 'CodeSetup' icon that you double click to launch:

It will then ask to start the process, plug in your SD Card, then unplug it when the image is transferred to the SD card.

You can then access the device via 'http://coder.local'. When you access it for the first time, it will ask you to change your password, then re-enter it with the new password.

If you have Raspberry Pi compatible wireless adaptor you can then set up WiFi in the setup menu.

The only additional thing I wanted to change the default 'coder.local' name to something else since I set up one for me, wife, and kid. No problem, this was explained in the FAQ:

Using Multiple Coder on the Network

Awesome, a quick peek shows that it is very visual, any HTML / CSS / JavaScript change you do will immediately reflected on the page. For example, this nice (scary?) looking eyeball that follows your mouse around. :)

I look forward to experiment with this nice project more with wife and kid. Looks like it is a full stack with Node.js in the backend. Not sure at this point.

Oh yeah, if you want to ssh to the Pi, the username is still pi and password is your new password:

MacBook-Air:5_Requests echou$ ssh pi@coder-eric.local
Warning: Permanently added 'coder-eric.local,' (ECDSA) to the list of known hosts.
pi@coder-eric.local's password:
Linux coder-eric 3.18.7+ #756 PREEMPT Wed Feb 18 16:08:51 GMT 2015 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sat Feb 21 06:27:55 2015
pi@coder-eric ~ $

I had enough time to poke around a little bit, seems like using Node.js (maybe Express) in the backend as webserver:

pi@coder-eric ~/tmp/npm-7237 $ npm --version
pi@coder-eric ~/tmp/npm-7237 $ node --version
pi@coder-eric ~/tmp/npm-7237 $

Here is their Coder GitHub Page if you want to take a look at the actual code.


Tuesday, September 27, 2016

Adafruit Circuit Playground Second Hour

If you have followed last week's post on Adafruit's Circuit Playground, you now have a working communication between your computer and the board. In this example, I would like to establish the communication between my Raspberry Pi over the serial communication to the board. This way I can leverage everything the RasPi has while utilizing the sensors and outputs from the board. 

The package I need is PySerial, which can be installed via 'sudo pip install pyserial'. It is used as the 'serial' package in Python code. 

Here is the objective: 
  1. The Arduino code will print out serial output when either the right or left button is pressed.
  2. When the B key is pressed, the LED light will light up one at a time. 
  3. In the Python code we will send the letter B 9 times then wait for any serial input

Here is the Python code. Python Code. A few notes: 

- Raspberry Pi the serial port is /dev/ttyACM0 and /dev/cu.usbmodem142 on Mac, therefore I comment out the line depending on if I am on the Mac or Raspberry Pi. 
- The character is sent as Bytecode, so chr(0x42) is used for letter 'B'. Here is an ASCII table. 

- Timeout of 1 second is used to gradually light up the LED. 

import serial
import time

#ser = serial.Serial('/dev/ttyACM0', 9600)
ser = serial.Serial('/dev/cu.usbmodem1421', 9600)

# ASCII character 'B'
sendChr = chr(0x42)

# Send character B which turns on light one at a time
for i in range(9):

# Read the input from board
while True:
    message = ser.readline()

Here is the Arduino Code: 

- I am using the Demo code provided by Adafruit:

- Use the library manager to install the Adafruit Circuit Playground Libraries

// Demo program for testing library and board - flip the switch to turn on/off buzzer

#include <Adafruit_CircuitPlayground.h>
#include <Wire.h>
#include <SPI.h>

// we light one pixel at a time, this is our counter
uint8_t pixeln = 0;

byte byteRead;

void setup() {
  //while (!Serial);
  Serial.println("Circuit Playground test!");


void loop() {

  /************* TEST BOTH BUTTONS */
  if (CircuitPlayground.leftButton()) {
    Serial.println("Left button pressed!");
  if (CircuitPlayground.rightButton()) {
    Serial.println("Right button pressed!");

  /************** Read Serial In, 65 DEC ASCII is letter B */
  byteRead = Serial.read();
  if(byteRead == 66) {
    Serial.println("Letter B input, turn on light");
    CircuitPlayground.setPixelColor(pixeln++, CircuitPlayground.colorWheel(25 * pixeln));
    if (pixeln == 11) {
    pixeln = 0;

Another screenshot for the IDE: 

You can also use the serial monitor included with the Arduino IDE to test: 

This is really fun, I cant wait to see all the possibilities when combining the two! 

Tuesday, September 20, 2016

Adafruit Circuit Playground First Hour

The Adafruit Circuit Playground arrive a few days ago. I remember seeing it for the first time at PyCon 2016 when Tony D. had a demo board and I was really impressed by it. Imagine a board not much bigger than a regular US quarter PACKED with sensors.

Here are two quick demos with playing some sound on the board:

Source: https://www.adafruit.com/

  • 10 x mini NeoPixels, each one can display any rainbow color
  • 1 x Motion sensor (LIS3DH triple-axis accelerometer with tap detection, free-fall detection)
  • 1 x Temperature sensor (thermistor)
  • 1 x Light sensor (phototransistor)
  • 1 x Sound sensor (MEMS microphone)
  • 1 x Mini speaker (magnetic buzzer)
  • 2 x Push buttons, left and right
  • 1 x Slide switch
  • 8 x alligator-clip friendly input/output pins
    Includes I2C, UART, and 4 pins that can do analog inputs/PWM output
  • All 8 pads can act as capacitive touch inputs
  • Green "ON" LED so you know its powered
  • Red "#13" LED for basic blinking
  • Reset button

No soldering required, just plug it in, setup and off you go with your prototype!

I just had some time to play with it enough to set up the environment and get a few program going. Below are some useful links and screenshots. I was able to go thru the two sound tutorials:

1. Get the latest Arduino software.
2. Here is the Adafruit Getting Started with Circuit Playground instructions.
3. Circuit Playground Intro
4. Make sure you can see the serial port:

MacBook-Air:~ echou$ ls /dev/cu*
/dev/cu.Bluetooth-Incoming-Port    /dev/cu.usbmodem1411

MacBook-Air:~ echou$ 

5. Setup screenshots:

6. I was able to get to these three tutorials:

- Playground Speaker

- Circuit Playground Library:

- The sound of music:

The only thing that I found for the sound of music tutorial is that extra library is needed besides pitches.h: 

I am really looking forward to tinker with it more, the idea really is to integrate this with Raspberry Pi and Python to make some interesting project! 

Happy Coding! 

Tuesday, September 13, 2016

AWS Internet of Things Button

I received my AWS IoT Button a few days ago. This is a programmable button based on the Amazon Dash Button, you know, those little buttons that allows you to order stuff from Amazon by just pressing on it. What is different about this button is the programmability and getting you introduced to the AWS IoT platform with sample code and wizard workflow.

The 'Getting Started' on this page is a good introduction, since the purpose of the button is to allow you to experience the end user experience when they received your future IoT device, I dont repeat or rob you of the experience here. What I want to show would be the sample output of each of the step for me so you can compare with your own if you run into something different.

The whole process took about 1 hour for me, it would have been quicker if I had not deviate from the wizard of trying to see if there were Python Lambda code or if if I had not change the IoT permission in the Wizard.

Here are some picture of the steps:

1. Picture of the IoT button when it arrived:

2. Overall workflow of what we will go thru

3. Choosing the IoT button from the AWS IoT portal

4. Configure the IoT trigger to go to Lambda

5. The page will automatically generate the cert and private key, download them and set up the button by following the instruction on the page

6. Lambda function is now complete

7. You can now press the button, which will blink blue while operating and green when it is done. 

8. Here are some screenshots of the email sent, subscription confirmation page, and following emails. Notice in the subject line there is also the type of button push, single or double click.

This was really fun. I am thinking of other use. The only thing is the sample code is in Node.js, so I need to spend some time looking it over before I can start think of other use cases. At some point I should convert that to Python. :)

Happy coding!

Tuesday, September 6, 2016

Make a Twitter Bot in Python and AWS Lambda

I followed this excellent tutorial from Joel Grus on how to make a Twitter Bot. If you have followed some of my post before, Project Mining Social Web 2nd Edition or Python with JSon, you know I have had a little bit of experience using the Twitter API.

Here is a screenshot of the Twitter Bot:

What is different this time, is that I wish to use AWS Lambda as the backend. I want to know what is required and what the workflow was. I wont repeat the steps Joel listed out in his tutorial, just some of the things I found out:

1. Seems that AWS Lambda wants the main file to be lambda_function.py and the main function to be lambda_handler(event, context).

2. The scheduling trigger is now "CloudWatch Events - Schedule" instead of just schedule.

3. I learned this from a friend that to always immediately put the credential file in .gitignore to avoid accidentally checking it in on GitHub.

echou$ cat .gitignore

I am now the proud owner of a Twitter Bot that searches for Cisco layoff news and retweet them. On this date of Aug. 19, 2016 that was the big news of the week.



Here is what my code ended up as:

from __future__ import print_function
from twython import Twython
from twython.exceptions import TwythonError
import re
import json, pprint
with open('credentials.json') as f:
  credentials = json.loads(f.read())
client = Twython(credentials["consumer_key"],
query = "cisco layoffs -filters:retweets"
rgx = r"cisco"
def lambda_handler(event, context):
    results = client.search(q=query)
    for tweet in results["statuses"]:
        text = tweet["text"]
        # re.search matches anywhere in the string; re.I means case-insensitive
        if re.search(rgx, text, re.I):
            # client.retweet will raise an error if we try to retweet a tweet
            # that we've already retweeted. to avoid having to keep track, we
            # just use a try/except block
            except TwythonError as e:

results = client.search(q=query)

Happy Coding!

Tuesday, August 23, 2016

Curlify Your Python Requests Objects

I love things that are simple and useful. Egor's Curlify Python module is one of these gems I saw this morning.

I already have a Python script that demonstrate A10 Network's AXAPI using the Requests package:


With this package, I changed exactly one line of code wherever I wanted to give an curl example of the same operation.

For example, after importing the curlify file, instead of printing out r.content in line 23, I just print out curlify.to_curl(r.reqeust) and boom, I have a curl command I can pass to people! How cool is that?

7 import requests, json, curlify
 17 # Acquire athorization token
 18 auth_headers = {'content-type': 'application/json'}
 19 auth_payload = {"credentials": {"username": "admin", "password": "a10"}}
 20 auth_endpoint = '/axapi/v3/auth'
 21 url = base_url + auth_endpoint
 22 r = requests.post(url, data=json.dumps(auth_payload), headers=auth_headers, verify=Fa    lse)
 23 print(curlify.to_curl(r.request))

Of course, you get the most out of it if you use Requests already (who isnt?) and you can just write it in Requests. When somebody else wants a simpler operation, say using just curl, that you can just easily dish it out to them in less than 5 minutes.

This is my final output from the same Python file above:

The only different from the plain output is I added a '-k' to the curl operation because I have a self-signed certificate on the A10 Thunder TPS.

Nice tool, nice output, no sweat. My type of thing. :)