events-observe-zerokey.py 2.24 KB
Newer Older
Johannes Barthel's avatar
intial  
Johannes Barthel committed
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
#!/usr/bin/env python3

import greenswitch
from xml.dom.minidom import parse
import pymongo
import json
import logging
logging.basicConfig(level=logging.DEBUG)

# find out password for esl socket
dom = parse("/opt/freeswitch/etc/freeswitch/autoload_configs/event_socket.conf.xml")

password = None
for param in dom.getElementsByTagName("param"):
	if param.attributes.get("name").nodeValue == "password":
		password = param.attributes.get("value").nodeValue

if password is None:
	raise ValueError("no password found in config")

# connect to mongo
mongo = pymongo.MongoClient("127.0.1.1")
db = mongo.meteor

# connect to freeswitch
fs = greenswitch.InboundESL(host='127.0.0.1', port=8021, password=password)
fs.connect()

def get_fs_member_id(caller_num, conference_id):
	fs_conference = json.loads(fs.send("api conference {} json_list".format(conference_id)).data)[0]
	fs_member_id = [m["id"] for m in fs_conference["members"] if m["caller_id_number"] == caller_num][0]
	return fs_member_id

def show_event(evt):
	#print(evt.headers)
	digit = int(evt.headers["DTMF-Digit"])
	if digit != 0:
		return
	caller_num = evt.headers["Caller-Username"]
	# SECURITY: this assumes there is only one voiceUser per call. What happens
	#           if there are multiple concurrent calls from the same number in
	#           different meetings?
	voice_user = db.voiceUsers.find_one({"callerNum":caller_num, "joined": True})
	if voice_user is None:
		return
	meeting_id = voice_user["meetingId"]
	meeting = db.meetings.find_one({"meetingId": meeting_id})
	locked = meeting["lockSettingsProps"]["disableMic"]
	conference_id = meeting["voiceProp"]["voiceConf"]
	fs_member_id = get_fs_member_id(caller_num, conference_id)
	print("caller {} pressed {}, they are in a {} meeting.".format(caller_num, digit, "locked" if locked else "unlocked"))
	try:
		if not locked:
			print(fs.send("api conference {} tmute {}".format(conference_id, fs_member_id)).data)
		else:
			print(fs.send("api conference {} mute {}".format(conference_id, fs_member_id)).data)
	except KeyError as e:
		print("Error when sending (t)mute:")
		print(e)

# register handler
print(fs.send('EVENTS PLAIN DTMF').data)
# fs.register_handle("DTMF", lambda event: print(event.__dict__))
fs.register_handle("DTMF", show_event)
fs.process_events()