SMS Google Search

by EvanKale in Circuits > Mobile

5191 Views, 47 Favorites, 0 Comments

SMS Google Search

00_title.png

I live in a place where data plans are still extremely limited and overpriced. Over the years I've learned to make do with a basic phone plan which I mainly use to send text messages with. But there are times when I'm lost on the road and have no choice but to find a local coffee shop with WiFi just so I can Google an address or number. That is, until I had this idea – what if I could simply Google search with a text message?

The implementation is simple. All we need is one Twitter account linked to our mobile phone so that we can tweet out a search query through SMS, and another Twitter account, controlled by a server application, that listens to the SMS tweets from our phone and tweets out the search results. As long as the Twitter account linked to our phone accepts SMS notifications from the Twitter account controlled by the server, then all search results will be returned to our phone as an SMS.

www.evankale.blogspot.ca

Requisites

  1. Basic programming knowledge is required. There will be example source code included for the server application - you will need to know how to compile Java or can translate it to another language of your choice.
  2. Your mobile carrier will also need to have Twitter SMS support.

Obtaining API Keys

We will start by obtaining all the necessary development keys so that our server app can perform a Google search and push the results to Twitter. We'll need to make use of the Twitter API and Google's Custom Search Engine API.

For Twitter, we will need 4 keys:

  1. OAuth Consumer Key
  2. OAuth Consumer Secret
  3. OAuth Access Token
  4. OAuth Token Secret

For Google's custom search (CSE), we will need 2 keys:

  1. Google developer API key
  2. Google CSE engine ID

Create a Twitter Account for the Server

10_gglmnky_signup.png

We'll first head over to www.twitter.com to sign up for a new account that will be used to push search results.

Verify Mobile Number

11_gglmnky_add_mobile_number.png

In the new account, go to settings, head to the mobile section, and link a mobile phone number to the account. We will then need to verify the number with a PIN that is SMS'd to the phone number.

Twitter developer accounts require a linked phone number to create developer keys, once we obtain those, we will be transferring our phone number to another account.

Create a Twitter App

12_gglmnky_create_application.png

Head over to http://apps.twitter.com to create a new Twitter application.

Obtain Twitter Keys

13_gglmnky_app_permission.png

In the app settings, set the access level to "Read, Write, and access direct messages". Regenerate both consumer keys and access tokens, and take note of them.

These keys should be kept privately.

Create Twitter Account for Search Querys

15_gglmnky_delete_phone.png
16_text2tw_mobile_subscription.png

Go back to Twitter settings and unlink our mobile phone number from the server account. We'll then create a new Twitter account and link our mobile number to this new account.

For the mobile settings of this new Twitter account, turn on "Tweets from people you've enabled for mobile notifications", and turn off the rest.

Follow the Server Account

17_text2tw_follow_gglmnky.png

Using the new account, we will search for the handle of the server account, follow it, then turn on mobile notifications from it. Now we're done with Twitter!

Creating a Google Account

20_gglmnky_google_account.png

Moving on to obtaining our Google keys - head over to https://accounts.google.com to create a Google account if you don't already have one.

Create a New Google Project

21_gglmnky_create_google_project.png

While signed in to our Google account, go to https://console.developers.google.com/project. We'll create a new project, give it a name, and leave the project ID as is (can be changed but has naming restrictions).

Enable Custom Search API

23_search_api.png

In the project details, navigate to "APIs & auth", then "APIs". Search for the term "search", then click on "Custom search API" and enable it.

Create API Key

25_create_api_key.png
27_create_api_key_3.png

Under the "credentials" subcategory, click "Create a new key", then create a new server key. Enter the IP address of the location of where our server will run. In my case, this would be my home IP address. We can check our public IP address from https://www.whatismyip.com/.

Afterwards, make note of our API key that has been generated.

Create a Custom Search Engine

28_create_cse.png

Head over to https://cse.google.com/cse to create a custom search engine.

Custom Search Engine Settings

29_create_yelp_search.png

In this example, we will limit our search results to Yelp results. More sites to search (or even the entire web) can be added, but narrowing down our search to a single site can help us pinpoint the exact results we are looking for.

Obtain Search Engine ID

31_yelp_search_engine_id.png

In our custom search engine setup, click on "Search engine ID", and make note of it. Now we've obtained all the necessary keys and IDs!

Building the Server Application

(Sample server application source code written in Java is provided in the next step)

There are two parts to building the server application:

  • Twitter management - receives tweets from the account linked to mobile phone (using the Twitter streaming API), and tweets out the Google search results (using the Twitter update API).
    • I used Twitter4J (a Java library that encapsulates the Twitter API), to handle both streaming and update APIs. Various other libraries are available in different languages.
  • Google search management - performs a Google search with the search query tweeted out from the account linked to the mobile phone (using Google Custom Search API).
    • A simple HTTP GET query in the form of:

https://www.googleapis.com/customsearch/v1?key=insert_your_API_key_here&cx=insert_your_search_engine_ID_here&q=insert_a_search_query_here

will return a JSON object containing the search results of the input query. I used JSON in Java to parse this result and tweet out the appropriate information.

Server Source Code Sample

40_search_sample.png

Head over to https://github.com/evankale/TwitterPoster to download the sample server source code (hit "Download ZIP" if you don't have Git set up). You will need to be set up to compile Java or be able to translate this source into another language.

You will need to replace the place holder Strings in TwitterManager.java with the appropriate Twitter keys and account ID (one linked to mobile phone), and the place holder Strings in GoogleManager.java with the Google CSE keys.

This server sample app is designed to search two sites - Wikipedia and Yelp.

To perform a Wikipedia search - send an SMS to twitter shortcode (ie, tweet) a search query in the form: "wiki [random string] [max number of search results returned] [search string]"

To perform a Yelp search - send an SMS to twitter shortcode (ie, tweet) a search query in the form: "yelp [random string] [max number of search results returned] [search string]"

A list of Twitter shortcodes can be found here: Twitter shortcodes

Limitations:

  • Every tweet has a maximum character limit of 140. All tweets above 140 characters are truncated. All spaces are removed in the Wikipedia search to save characters.
  • Twitter's API limits a user to make 15 request every 15 minutes (ie, post 15 search results every 15 minutes)
  • The exact same tweet cannot be tweeted more than once. This can bypassed by adding a random string in the tweet.

Where to Put the Server?

41_google_monkey_server.png

Many of us probably don't want to leave a server computer running in our homes 24/7 just to provide a search service for ourselves.

So I chose to run my server on a low powered Android device that I no longer use (an old Samsung Galaxy Q phone that runs Android 2.2). An Android project of the sample server app can be found here: https://github.com/evankale/TwitterPosterAndroid

Another idea is to run the server app on your home router if it is capable of running homebrew programs (ie, a router flashed with DD-WRT firmware).

Virtual private server hosting is also another way to go - although the monthly cost of a subscription is not worthwhile for running this single service.

Conclusion

That's it! Happy Tweeting!

If you liked this Instructable, then perhaps you'll like some of my other projects! You can check them out at Blogspot, or visit my YouTube Channel.