Sunday, January 17, 2016

Taking advantage of weechat, sms gateway and mailgun, so I get irc messages directed at me on my phone

I wrote this script a while ago so I could have the weechat instance running on my server send me texts whenever I was highlighted in IRC. IRC is love, IRC is life.

So what I did was found my sms gateway typically your email to sms gateway is something like:
your_number@sms-gateway.carrier.com

I used mailgun to deal with email, because who would even want to deal with email any other way...gross. Mailgun is a free service. This also creates a log of everything in /var/log/highlight.log. And I think at one point I had a really ugly stupid script that would check the md5 of the log file on the server, and if it had changed it would give me a growl alert on my mac...which is terrible design and I'm glad I don't do that anymore.


import weechat
end_command = ">> /var/log/highlight.log"
import subprocess
weechat.register("snail", "Bsdpunk", "1.0", "GPL3", "Test script", "", "")
weechat.prnt("", "Hello, from python script!")
import requests
key = "mailgun_api_key"
sandbox = "mailgun_domain"
recipient = 'my_number@sms-gateway.carrier.com'

def join_cb(data, signal, signal_data):
    nick = weechat.info_get("irc_nick_from_host", signal_data)
    server = signal.split(",")[0]
    channel = signal_data.split(":")[-1]
    buffer = weechat.info_get("irc_buffer", "%s,%s" % (server, channel))
    if buffer:
        weechat.prnt(buffer, "Eheh, %s has joined this channel!" % nick)
    return weechat.WEECHAT_RC_OK

def message(data, bufferp, uber_empty, tagsn, isdisplayed, ishilight, prefix, message):
    #weechat.prnt("", data)
    #weechat.prnt("", bufferp)
    #weechat.prnt("", uber_empty)
    #weechat.prnt("", tagsn)
    #weechat.prnt("", isdisplayed)
    #weechat.prnt("", ishilight)
    #weechat.prnt("", prefix)
    #weechat.prnt("", message)
    if int(ishilight):
    weechat.prnt("", message)
        full_command = "echo '" +message + "' " + end_command
        weechat.prnt("", full_command)
        process = subprocess.call(full_command, shell=True)
        request_url = 'https://api.mailgun.net/v2/{0}/messages'.format(sandbox)
        request = requests.post(request_url, auth=('api', key), data={
            'from': 'hammer@mechanicalpinata.com',
            'to': recipient,
            'subject': "IRC Message",
            'text':prefix+' '+message
        })
        weechat.prnt('','Status: {0}'.format(request.status_code))
        weechat.prnt('','Body:   {0}'.format(request.text))
    return weechat.WEECHAT_RC_OK

weechat.hook_print("", "irc_privmsg", "", 1, "message", "")import weechat
end_command = ">> /var/log/highlight.log"
import subprocess
weechat.register("snail", "Bsdpunk", "1.0", "GPL3", "Test script", "", "")
weechat.prnt("", "Hello, from python script!")
import requests
key = "mailgun_api_key"
sandbox = "mailgun_domain"
recipient = 'my_number@sms-gateway.carrier.com'

def join_cb(data, signal, signal_data):
    nick = weechat.info_get("irc_nick_from_host", signal_data)
    server = signal.split(",")[0]
    channel = signal_data.split(":")[-1]
    buffer = weechat.info_get("irc_buffer", "%s,%s" % (server, channel))
    if buffer:
        weechat.prnt(buffer, "Eheh, %s has joined this channel!" % nick)
    return weechat.WEECHAT_RC_OK

def message(data, bufferp, uber_empty, tagsn, isdisplayed, ishilight, prefix, message):
    #weechat.prnt("", data)
    #weechat.prnt("", bufferp)
    #weechat.prnt("", uber_empty)
    #weechat.prnt("", tagsn)
    #weechat.prnt("", isdisplayed)
    #weechat.prnt("", ishilight)
    #weechat.prnt("", prefix)
    #weechat.prnt("", message)
    if int(ishilight):
    weechat.prnt("", message)
        full_command = "echo '" +message + "' " + end_command
        weechat.prnt("", full_command)
        process = subprocess.call(full_command, shell=True)
        request_url = 'https://api.mailgun.net/v2/{0}/messages'.format(sandbox)
        request = requests.post(request_url, auth=('api', key), data={
            'from': 'hammer@mechanicalpinata.com',
            'to': recipient,
            'subject': "IRC Message",
            'text':prefix+' '+message
        })
        weechat.prnt('','Status: {0}'.format(request.status_code))
        weechat.prnt('','Body:   {0}'.format(request.text))
    return weechat.WEECHAT_RC_OK

weechat.hook_print("", "irc_privmsg", "", 1, "message", "")


2 comments:

Martin Harari Thuresson said...

Thanks for the neat idea, I have not heard about weechat before.

If your operator does not have support for Email Gateway. I would recommend using a SMS API (for example 46elks, where I work).
Or any other from this list for example: http://blog.mashape.com/list-of-50-sms-apis/

Amar Sms said...
This comment has been removed by the author.