Telegram SMS-bridge via twilio

And here we go again. Even though my old project is still running nicely, the disappointing part it the need to regularly reboot the system as usb umts sticks seem to freeze after a week or so.

There is also the problem of running the system at home or whereever, but not on my servers in the computing center, so I had a look into various sms-gateway provider and decided to build a solution around twilio.

Even though twilio is nicely documented for the REST-API and provides you with many code examples anything handling responses is basically aimed at forwarding incomming replies to a webservice you would set up. No, I don’t want that 😉 .

Luckily, the API allows exactly what is needed – it is just not the kind of code example popping up if you search for ‘receiving SMS’.

Thus: here is the modification of the telepot-bot to use twilio instead of local hardware:


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time
import telepot
import re
from import TwilioRestClient 

class twsmsbot(telepot.Bot):
    def __init__ (self, uid, token, twacc, twauth, twsndr):
        self._mode = 0
        self._recipient = ""
        self._active = 1
        self._user = uid
        self._twclient = TwilioRestClient(twacc, twauth) 
        self._txsndr = twsndr
    def handle (self, msg):
        chat_id = msg['chat']['id']
        command = msg['text']
        if chat_id != self._user:
            self.sendMessage( chat_id, "unauthorized user")
            return 0
        res = re.match("^/(\w+)(\s[\+\w\d]+)*\Z", command, flags=0)
        if res:
            if == "status":
            elif == "setrcpt":
                self._recipient =
                self.sendMessage( chat_id, "recipient set to: %s" % self._recipient)
            elif == "open":
                if self._recipient != "":
                    self._mode = 1
                    self.sendMessage( chat_id, "relay active")
                    self.sendMessage( chat_id, "set recipient first!")
            elif == "close":
                self.sendMessage( chat_id, "relay deactived")
                self._mode = 0
            elif == "terminate":
                self._active = 0
                self.sendMessage( chat_id, "unrecognized command!")
            if self._mode == 1 and self._recipient != "":
                self.sendsm ( command )
                self.sendMessage( chat_id, "relay not enabled")

    def status (self):
        self.sendMessage( self._user , "recipient: %s, relay status: %d" % (self._recipient, self._mode))
    def sendsm (self, msg):
        self._twclient.messages.create(to=self._recipient, from_=self._txsndr, 

    def recvsm (self ):
        l = self._twclient.messages.list()
        for i in l:
            m = self._twclient.messages.get(
            self.sendMessage(self._user, "From: %s \n at: %s \n %s" % (m.from_,m.date_created,m.body))

    def run (self ):
        while self._active == 1:
            self.recvsm ()
mybot = twsmsbot (<adminuid>, "bot-token", 
        "twilio-auth-token", "sender")