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 os import getpid,uname
17 from pprint import pprint
19 client_name='agi-'+uname()[1]+'-'+str(getpid())
22 conffile, topic = sys.argv[1:3]
24 config = ConfigParser()
25 config.add_section('mqtt')
26 # set defaults for anonymous auth
27 config.set('mqtt', 'username', '')
28 config.set('mqtt', 'password', '')
29 config.set('mqtt', 'port', '1883')
32 mqtt_server = config.get('mqtt', 'server')
33 mqtt_port = config.getint('mqtt', 'port')
34 mqtt_username = config.get('mqtt', 'username')
35 mqtt_password = config.get('mqtt', 'password')
39 line = sys.stdin.readline()
40 if not line or line == "\n":
44 agi = dict([line.rstrip('\n').replace('agi_', '', 1).split(': ', 1) for line in agi])
47 agi['extension'] = sys.argv[3]
49 def agi_exit(rc, *args):
51 print "VERBOSE rc=%s %s" % (rc, args)
54 def on_connect(mosq, rc, *args):
56 agi_exit(1, "Connection failed: %d" % rc)
58 def on_publish(mosq, *args):
62 client = paho.Client(client_name)
63 client.username_pw_set(mqtt_username, mqtt_password)
64 client.connect(mqtt_server, port=mqtt_port)
65 client.on_connect = on_connect
66 client.on_publish = on_publish
68 client.publish(topic, payload=json.dumps(agi))
70 agi_exit(1, "Message publish timed out")