element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog Raspberry Pi Google Talk Robot
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
GPIO Pinout
Raspberry Pi Wishlist
Comparison Chart
Quiz
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Former Member
  • Date Created: 17 Jan 2013 2:09 AM Date Created
  • Views 1730 views
  • Likes 2 likes
  • Comments 7 comments
Related
Recommended
  • raspberry
  • python
  • rpiintermediate
  • pi
  • gpio
  • bot
  • google
  • talk
  • members_featured_project
  • software
  • linux

Raspberry Pi Google Talk Robot

Former Member
Former Member
17 Jan 2013

This is my submission for the Double Your Memory Pi Challenge.

image

 

http://youtu.be/vd6RlkAXWRs

 

Google Talk/Chat/Messenger is normally used by humans to interact with other humans. However, its underlying technology can also be used as a mechanism to implement software robots.  Internet bots, also known as web robots, WWW robots or simply 'bots' can also utilize the technology to perform automated functions over the web.  There are many such bots in existence, offering a diverse spectrum of services from jokes (jokes@askme.im) to URL Shortening using bit.ly (url@askme.im), even mathematical calculation (math@bot.im).  Using such bots is quick and easy to configure, all that must be done is to add the bot as a contact to your messaging account.  Then, whenever you desire data from the service, simply text the command to the bot and it will respond with the respective message.


The Raspi Bot is essentially the same as any other automated Internet robot.  To configure it, it must first have its own e-mail address associated with a Google talk account.  This e-mail address must also be added as a contact with the account that wishes to communicate to the bot.  Then, whenever the script is running on the remote machine, it will log into Google chat and appear as a friend in your contact list.

 

The software itself is essentially just a Python daemon script that is a wrapper around the XMPP protocol.  When executed, the script will sign in to Google talk using its own username and password.  The Python script is derived from the open source project pygtalkrobot: An open source python gtalk(google talk) bot framework using XMPPPY and PyDNS libraries, that also references the source code of python-jabberbot.

Software

The Raspi Bot requires several additional Python modules for use.  The easiest way to install these is with the python pip package manager. If you don't have it installed, you can install them using apt-get:

sudo apt-get install python-pip git-core python2.7-dev

Then update the easy_install index:

sudo easy_install -U distribute

And install the GPIO, xmpppy, and pydns modules:

sudo pip install RPi.GPIO xmpppy pydns

Then clone my repo for the Raspi Gtalk robot:

git clone https://github.com/mitchtech/raspi_gtalk_robot.git

Now change into the newly created directory:

cd raspi_gtalk_robot

Finally, you will need  to configure the Raspi Bot’s Gtalk username and password.  This is done by editing the fields BOT_GTALK_USER, BOT_GTALK_PASS, and BOT_ADMIN, on lines 31-33 in the raspiBot.py file.  It is recommended, though not required, to give the Raspi Bot its own Gmail account. Since access to the Raspberry Pi GPIO pins is restricted, the script needs to be run with sudo:

sudo python ./raspiBot.py

This basic sample script supports the following commands:

  • [pinon|pon|on|high] [pin] : turns on the specified GPIO pin
  • [pinoff|poff|off|low] [pin] : turns off the specified GPIO pin
  • [write|w] [pin] [state] : writes specified state to the specified GPIO pin
  • [read|r] [pin]: reads the value of the specified GPIO pin
  • [available|online|busy|dnd|away|idle|out|xa] [arg1] : set gtalk state and status message to specified argument
  • [shell|bash] [arg1] : executes the specified shell command argument after 'shell' or 'bash'

For example, sending the message "pinon 12" will turn on GPIO pin #12, "read 18" will read the current state of GPIO pin 18, or "bash ps" to execute the shell command 'ps'.


Use case #1: Home automation

One of the most obvious usages of this technology is for home automation purposes. The Raspi Bot can be accessed anywhere with Google talk, which to my understanding, is nearly every system in existence.  Send the Raspi Bot messages to turn on and off lights and other electrical appliances.This is also useful to provide immediate notification in the event of intrusion detection. The Raspi Bot can be supplemented with additional security sensors, including infrared motion, and ultrasonic distance sensors. If any pre-programmed sensor violates any predefined condition, you can be immediately notified via message from the Raspi Bot.


Use case #2: Remote shell

The Raspi Bot can be used essentially as a remote shell.  In this configuration, every message sent to the Raspi Bot will be interpreted as a shell command with the output piped back to the user in the form of a response message.  Obviously, this could raise some security concerns. To protect against misuse, the Raspi Bot will only respond to Google chat messages from the Google user designated as the administrator of the bot.  By default, messages from any other user will simply be ignored.The following is a small subset of the relatively benign commands possible to be run remotely via the Raspi Bot:

  • vmstat - system activity, hardware and information
  • uptime - how long the system has been running
  • w - logged in users and their process activity
  • ps - reports a snapshot of the current processes
  • free - physical and swap memory usage
  • iostat - average CPU load, disk activity

Arguably, disclosure of any amount of information about system can be considered a security issue such as that reported by some of the above tools.  For users more concerned about convenience over security exposure, much more elaborate commands can be run, such as executing additional scripts or accessing private data.

 

Use case #3: Remote Reboot

Another problem that can be solved by the Raspi Bot is frozen remote machines. We've all been there before, attempting to access a remote machine only to find it to be completely non-responsive to any form of remote login.  These cases, we (or maybe a system administrator somewhere) would usually have to make a trip to the physical location of the server and push ‘the big red button’ to reboot the affected machine.  The situation can be eliminated completely by deputizing a Raspi Bot as a remote reboot agent. This can be done by adding bot controlled relay(s) to the power supplies of the machines.  In the event any of the machines controlled by the Raspi Bot becomes non-responsive, simply send the appropriate Google talk message to flip the respective relay, and reboot the affected machine.  Here's a diagram of how this would look:

raspi_remote_reboot

  • Sign in to reply

Top Comments

  • Former Member
    Former Member over 12 years ago +1
    I noticed that the timestamps in the replies from the Bot was off, they correspond with GMT and not my local time. I changed: "+time.strftime("%Y-%m-%d %a %H:%M:%S", time.gmtime to: "+time.strftime("%Y…
  • Former Member
    Former Member over 12 years ago in reply to kraaima +1
    Hi Peter, Here's how to do it with an array. First change the single BOT_ADMIN to an array with all the bot admins: BOT_ADMIN = [ 'admin1@gmail.com ', 'admin2@gmail.com '] Then add a for loop to iterate…
  • Former Member
    Former Member over 12 years ago

    Thanks Michael!

    I am trying to get the robot into a group conversation but Google tells me that Group Chats with this Client are not possible.

    Do you have any idea to get this little nice bot into groups? Or can you give me hint where I can find that out?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • kraaima
    kraaima over 12 years ago in reply to Former Member

    Thanx Michael, that worked out great!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 12 years ago in reply to kraaima

    Hi Peter,

    Here's how to do it with an array. First change the single BOT_ADMIN to an array with all the bot admins:

    BOT_ADMIN = ['admin1@gmail.com', 'admin2@gmail.com']

    Then add a for loop to iterate over the admin array before the admin check. So instead of:

    if jid == BOT_ADMIN:

    It would look something like:

    for admin in BOT_ADMIN:

         if jid == admin:

              # do stuff here

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 12 years ago in reply to Former Member

    Thanks for the feedback Ben, I've updated the git repo to use localtime instead of GMT.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • kraaima
    kraaima over 12 years ago in reply to kraaima

    I added some line of code to add multiple admin..

     

    The dirty way:

    Add on line 34:

    BOT_ADMIN2 = 'AdminNumberTwoAdress@gmail.com'

     

    Then change this line (60):

       if jid == BOT_ADMIN:

    to:

      if jid == BOT_ADMIN or BOT_ADMIN2:

     

    Now you have two admins who can "control" the bot.

     

    Next I added (65):

     

        #This method  does nothing usefull but send text back to the user (chatbot?)

         def command_002_answer(self, user, message , args):

            '''(huh|hello|test|hallo)( +(.*))?$(?i)'''

            print "User says hi\n "

            self.replyMessage(user, "\nHi there! "+time.strftime("%Y-%m-%d %a %H:%M:%S", time.localtime()))

     

    From this point on I can let the bot talk to us..

     

    Next up is a way to walk trough all pins to see what they are doing at the moment and give me a overview of that.

     

    Also.. It would be really handy if we "disable" the pinon's for pins we dont need/want.

     

    Thanx Ben Mason for the localtime tip!

    And ofcourse Michael Mitchell for the main article.

     

    If you (like me) are still wanting to know the array way for python look here:

    http://stackoverflow.com/a/1781649

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube