forked from zeha/agi-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt
70 lines (56 loc) · 1.79 KB
/
mqtt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
#
# Usage:
# AGI(/etc/asterisk/agi/mqtt,/etc/asterisk/agi/mqtt.cfg,calls/missed)
#
import sys
from ConfigParser import ConfigParser
import paho.mqtt.client as paho
import json, time, ssl
from asterisk.agi import *
conffile, topic = sys.argv[1:3]
config = ConfigParser()
config.add_section('mqtt')
config.set('mqtt', 'username', '')
config.set('mqtt', 'password', '')
config.set('mqtt', 'port', '1883')
config.read(conffile)
mqtt_server = config.get('mqtt', 'server')
mqtt_port = config.getint('mqtt', 'port')
mqtt_username = config.get('mqtt', 'username')
mqtt_password = config.get('mqtt', 'password')
#Defining callbacks
def on_message(client, userdata, message):
print("received message =",str(message.payload.decode("utf-8")))
def on_log(client, userdata, level, buf):
print("log: ",buf)
def on_connect(client, userdata,flags,rc):
print("connected")
def on_publish(client, userdata,flags,rc):
print("publishing")
def publish(topic, message, waitForAck = False):
mid = client.publish(topic, message, 2)[1]
if (waitForAck):
while mid not in receivedMessages:
time.sleep(0.25)
def agi_exit(rc, *args):
if rc != 0:
print "VERBOSE rc=%s %s" % (rc, args)
sys.exit(rc)
#creating client and registering callbacks
client=paho.Client()
client.on_message=on_message
client.on_log=on_log
client.on_connect=on_connect
client.op_publish=on_publish
#initializing AGI and getting callerID of incoming caller
agi = AGI()
callerId = agi.env['agi_callerid']
print("connecting to broker")
#uncomment if using MQTT over SSL
#client.tls_set(cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2)
#client.tls_insecure_set(True)
client.username_pw_set(mqtt_username, mqtt_password)
client.connect(mqtt_server, port=mqtt_port)
client.publish(topic,callerId)
agi_exit(1, "Message publish timed out")