Send Hangouts message from command line on Debian

Finally found a possibility to send a message from commandline via Google Hangouts, thanks to the hangups messaging client.

Installation

Hangups requires Python 3.3, 3.4, or 3.5 and can be installed via pip3:

apt-get install python3-pip
pip3 install hangups

First start

Create an Google Account for your “Chat Bot” and follow the User Guide / Logging In.

hangups --token-path /root/.cache/hangups/refresh_token.txt

Open https://hangouts.google.com, log in with your “Chat Bot” account and invite your contacts.

Example send message application

NOTE: The following example works for user “root”. If another user should be used, the send message application must be saved as /usr/local/bin/send_hangouts_message.py and references to /root changed to /home/USERNAME.

Download “send message” example application:

wget https://raw.githubusercontent.com/tdryer/hangups/master/examples/send_message.py -O /usr/local/sbin/send_hangouts_message.py

Edit /usr/local/sbin/send_hangouts_message.py and change the following part, basically use stdin as MESSAGE, change REFRESH_TOKEN_PATH and CONVERSATION_ID:

"""Example of using hangups to send a chat message to a conversation."""

import sys

import asyncio

import hangups


# ID of the conversation to send the message to. Conversation IDs can be found
# in the hangups debug log by searching for "conversation_id".
CONVERSATION_ID = 'UgyoEJW1M5TLSnPWY494AaABAQ'

# Plain-text content of the message to send.
MESSAGE = ''.join(sys.stdin)

# Path where OAuth refresh token is saved, allowing hangups to remember your
# credentials.
REFRESH_TOKEN_PATH = '/root/.cache/hangups/refresh_token.txt'

* Get CONVERSATION_ID:

grep -A 1 UgyoEJW1M5TLSnPWY494AaABAQ /root/.cache/hangups/log/hangups.log

Sending Hangouts message

echo "hello world" | python3 /usr/local/sbin/send_hangouts_message.py
--- Logging error ---
Traceback (most recent call last):
--- Logging error ---
Traceback (most recent call last):
...

# supress error messages
echo "hello world" | python3 /usr/local/sbin/send_hangouts_message.py 2>/dev/null

Tested on Debian Jessie.

References:
https://github.com/tdryer/hangups
http://hangups.readthedocs.org/en/stable/

Advertisements