5 # AGI(/etc/asterisk/agi/mqtt,/etc/asterisk/agi/mqtt.cfg,calls/missed)
6 # or, to override the extension:
7 # AGI(/etc/asterisk/agi/mqtt,/etc/asterisk/agi/mqtt.cfg,calls/missed,+43123456789)
11 from ConfigParser import ConfigParser
12 import paho.mqtt.client as paho
16 from pprint import pprint
18 conffile, topic = sys.argv[1:3]
20 config = ConfigParser()
21 config.add_section('mqtt')
22 # set defaults for anonymous auth
23 config.set('mqtt', 'username', '')
24 config.set('mqtt', 'password', '')
25 config.set('mqtt', 'port', '1883')
28 mqtt_server = config.get('mqtt', 'server')
29 mqtt_port = config.getint('mqtt', 'port')
30 mqtt_username = config.get('mqtt', 'username')
31 mqtt_password = config.get('mqtt', 'password')
35 line = sys.stdin.readline()
36 if not line or line == "\n":
40 agi = dict([line.rstrip('\n').replace('agi_', '', 1).split(': ', 1) for line in agi])
43 agi['extension'] = sys.argv[3]
45 def agi_exit(rc, *args):
47 print "VERBOSE rc=%s %s" % (rc, args)
50 def on_connect(mosq, rc, *args):
52 agi_exit(1, "Connection failed: %d" % rc)
54 def on_publish(mosq, *args):
58 client = paho.Client('agi')
59 client.username_pw_set(mqtt_username, mqtt_password)
60 client.connect(mqtt_server, port=mqtt_port)
61 client.on_connect = on_connect
62 client.on_publish = on_publish
64 client.publish(topic, payload=json.dumps(agi))
66 agi_exit(1, "Message publish timed out")