2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-01 21:40:32 +00:00
Comrad/komrade/backend/switchboard.py

44 lines
1.5 KiB
Python
Raw Normal View History

2020-09-05 21:11:42 +00:00
# internal imports
import os,sys; sys.path.append(os.path.abspath(os.path.join(os.path.abspath(os.path.join(os.path.dirname(__file__),'..')),'..')))
from komrade import *
2020-09-06 06:50:23 +00:00
from komrade.backend import *
2020-09-05 21:11:42 +00:00
# external imports
from flask import Flask, request, jsonify
from flask_classful import FlaskView
class TheSwitchboard(FlaskView, Logger):
2020-09-06 20:26:55 +00:00
default_methods = ['GET']
excluded_methods = ['phone','op','send']
2020-09-08 15:36:01 +00:00
@property
def op(self):
from komrade.backend.the_operator import TheOperator
if type(self)==TheOperator: return self
if hasattr(self,'_op'): return self._op
global OPERATOR,OPERATOR_KEYCHAIN
if OPERATOR: return OPERATOR
self._op=OPERATOR=TheOperator()
return OPERATOR
2020-09-05 22:39:28 +00:00
def get(self,msg):
2020-09-05 22:44:23 +00:00
self.log('Incoming call!:',msg)
2020-09-05 22:41:43 +00:00
if not msg:
self.log('empty request!')
return OPERATOR_INTERCEPT_MESSAGE
2020-09-05 22:51:34 +00:00
# unenescape
msg = msg.replace('_','/')
2020-09-08 16:09:51 +00:00
str_msg_from_op = self.op.answer_phone(msg)
2020-09-08 15:36:01 +00:00
# str_msg_from_op = msg.replace('_','/')
2020-09-08 11:23:41 +00:00
self.log('Switchboard got msg back from Operator:',str_msg_from_op)
return str_msg_from_op
2020-09-05 21:11:42 +00:00
def run_forever(port='8080'):
2020-09-07 17:13:39 +00:00
global OPERATOR,TELEPHONE,TELEPHONE_KEYCHAIN,OPERATOR_KEYCHAIN
2020-09-07 17:53:16 +00:00
OPERATOR_KEYCHAIN,TELEPHONE_KEYCHAIN=connect_phonelines()
TELEPHONE = TheTelephone()
OPERATOR = TheOperator()
2020-09-05 21:11:42 +00:00
app = Flask(__name__)
2020-09-05 21:47:29 +00:00
TheSwitchboard.register(app, route_base='/op/', route_prefix=None)
2020-09-07 18:26:19 +00:00
app.run(debug=False, port=port, host='0.0.0.0')