当前位置: 首页 > 工具软件 > Asterisk-Java > 使用案例 >

asterisk-java ami4 一些基本功能的例子

燕玉堂
2023-12-01

转载自: https://www.cnblogs.com/cbdd/p/5497133.html

 

asterisk-java ami4 一些基本功能的例子

比如:挂机,拨号,拨外线,保留通话,示闲,示忙等等。。。。

在api中可以获得这些方法说明

/**
  * 给分机挂机
  * @param Extension 要挂机的分机号
  * @return
  * @throws TimeoutException 
  * @throws IOException 
  * @throws IllegalStateException 
  * @throws IllegalArgumentException 
  */
 public static ManagerResponse getHangup(String Extension,Integer value) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
   if(BaseUtil.getConnection()==null){
    BaseUtil.getAsteriskJava(); 
   } 
   HangupAction hangupAction=new HangupAction();
   hangupAction.setActionId(BaseController.getactionId("Hangup", Extension));
   hangupAction.setCause(value);//挂机原因 
   hangupAction.setChannel(BaseUtil.getChannel(Extension));
   ManagerResponse response =BaseUtil.getConnection().sendAction(hangupAction);
   return response; 
 }

 

 /**
  * 分机外呼
  * @param Extension 发起呼叫的分机号
  * @param phone 被呼叫的外线号
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 @SuppressWarnings("deprecation")
 public static ManagerResponse getWOriginate(String Extension,String phone) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
   if(BaseUtil.getConnection()==null){
    BaseUtil.getAsteriskJava();
   }
   OriginateAction origaction=new OriginateAction();
   origaction.setContext("from-internal");//路由器
   String action=BaseController.getactionId("Originate", Extension);
   origaction.setActionId(action);//呼叫action id
   origaction.setChannel("SIP/"+Extension);//呼叫连接通道,呼叫时,先呼通自己
   origaction.setExten("799"+Extension+phone+"#");//要外呼的电话799+分机号是
   origaction.setPriority(1);//优先级
   origaction.setTimeout(20000);//呼叫等待(毫秒)
   origaction.setCallerId(phone);//呼叫号码
   origaction.setAsync(true);//设置异步才会有结果返回
   ManagerResponse response =BaseUtil.getConnection().sendAction(origaction);  
   System.out.println("外呼结果"+response);
   return response;
 }


 

 /**
  * 分机呼分机
  * @param Extension 发起呼叫的分机号
  * @param phone 被呼叫的分机号
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 @SuppressWarnings("deprecation")
 public static ManagerResponse getROriginate(String Extension,String phone) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
   if(BaseUtil.getConnection()==null){
    BaseUtil.getAsteriskJava();
   }
   OriginateAction origaction=new OriginateAction();
   origaction.setContext("from-internal");//路由器
   String action=BaseController.getactionId("Originate", Extension);
   origaction.setActionId(action);//呼叫action id
   origaction.setChannel("SIP/"+Extension);//呼叫连接通道,呼叫时,先呼通自己
   origaction.setExten(phone);//要外呼的电话
   origaction.setPriority(1);//优先级设置1时,不会回拨,为什么
   origaction.setTimeout(20000);//呼叫等待(毫秒)
   origaction.setCallerId(phone);//呼叫号码
   origaction.setAsync(true);//设置异步才会有结果返回
   ManagerResponse response =BaseUtil.getConnection().sendAction(origaction);   
   return response;
 }


 

 /**
  * 插入通话
  * @param Extension 发起呼叫的分机号
  * @param phone 被插入通话的分机号
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 @SuppressWarnings("deprecation")
 public static ManagerResponse getCOriginate(String Extension,String phone) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
   if(BaseUtil.getConnection()==null){
    BaseUtil.getAsteriskJava();
   }
   OriginateAction origaction=new OriginateAction();
   origaction.setContext("spy");//路由器
   String action=BaseController.getactionId("Originate", Extension);
   origaction.setActionId(action);//呼叫action id
   origaction.setChannel("SIP/"+Extension);//呼叫连接通道,呼叫时,先呼通自己
   origaction.setExten("557"+phone);//要插入通话的分机
   origaction.setPriority(1);//优先级
   origaction.setTimeout(20000);//呼叫等待(毫秒)
   origaction.setAsync(true);//设置异步才会有结果返回
   ManagerResponse response =BaseUtil.getConnection().sendAction(origaction);   
   return response;
 }


 

/**
  * 监听通话
  * @param Extension 发起监听的分机号
  * @param phone 要监听的分机号
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 @SuppressWarnings("deprecation")
 public static ManagerResponse getJOriginate(String Extension,String phone) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
   if(BaseUtil.getConnection()==null){
    BaseUtil.getAsteriskJava();
   }
   OriginateAction origaction=new OriginateAction();
   origaction.setContext("spy");//路由器
   String action=BaseController.getactionId("Originate", Extension);
   origaction.setActionId(action);//呼叫action id
   origaction.setChannel("SIP/"+Extension);//呼叫连接通道,呼叫时,先呼通自己
   origaction.setExten("556"+phone);//要插入通话的分机
   origaction.setPriority(1);//优先级
   origaction.setTimeout(20000);//呼叫等待(毫秒)
   origaction.setAsync(true);//设置异步才会有结果返回
   ManagerResponse response =BaseUtil.getConnection().sendAction(origaction);   
   return response;
 }


 

/**
  * 获取分机状态
  * @param Extension 分机号
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse getExtension(String Extension) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  ExtensionStateAction extaction=new ExtensionStateAction(Extension,"default");
  
  String Actionid=BaseController.getactionId("Extension", Extension);
  extaction.setActionId(Actionid);
  ManagerResponse response =BaseUtil.getConnection().sendAction(extaction);
  return response;
 }


 

/**
  * 获取分机通话的录音文件
  * @param Extension 需要录音的分机号
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse getMonitor(String Extension) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  MonitorAction MonitorAction=new MonitorAction();    
  MonitorAction.setActionId(BaseController.getactionId("Monitor", Extension));
  MonitorAction.setMix(false);//获取false为1个语音文件,true为两个
  MonitorAction.setChannel(BaseUtil.getChannel(Extension));
  MonitorAction.setFile("分机号:"+Extension+" 时间:"+ToolUtils.formatDate(new Date(), "yyyymmddhhmmss")+" 监听录音");//语音文件的名称
  //MonitorAction.setFormat("");//录音文件编码格式
  ManagerResponse response =BaseUtil.getConnection().sendAction(MonitorAction);
  return response;
 }


 

/**
  * 获取所有的sip话机信息连接情况
  * @param Actionid 唯一标识
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse getSIPpeers(String Actionid) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  SipPeersAction sippeers=new SipPeersAction();   
  sippeers.setActionId(Actionid);
  ManagerResponse response =BaseUtil.getConnection().sendAction(sippeers);
  return response;
 }


 

/**
  * 重定向通话到指定的分机,外线
  * @param outextension 进行指定的本机号
  * @param inextension 指定的分机号,外线
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse getRedirect(String outextension,String inextension) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  MapCacheUtil mapcache=MapCacheUtil.getInstance();
  String channel=BaseUtil.getChannel(outextension);//获取本机的通道
  RedirectAction redrec=new RedirectAction();
  redrec.setActionId(BaseController.getactionId("Redirect", inextension));
  redrec.setChannel(String.valueOf(mapcache.get(channel)));//要转移的通道
  redrec.setContext("from-internal");
  redrec.setExten(inextension);//要转入的分机
  redrec.setPriority(1);//优先级
  ManagerResponse response=BaseUtil.getConnection().sendAction(redrec);
  return response;
 }

 

 /**
  * 通话保留
  * @param Extension
  * @param channel 对方通道
  * @param channel2 自己通道
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse getPark(String Extension) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  MapCacheUtil mapcache=MapCacheUtil.getInstance();
  ParkAction park=new ParkAction();
  String channel=BaseUtil.getChannel(Extension);
  park.setActionId(BaseController.getactionId("Park", Extension));
  park.setChannel(String.valueOf(mapcache.get(channel)));//要保留的通道
  park.setChannel2(channel);//时间结束要返回的通道  
  park.setTimeout(60*60*1000);//保留通话的时间
  ManagerResponse response=BaseUtil.getConnection().sendAction(park);
  return response;
 }


 

/**
  * 通话接回
  * @param Extension
  * @param channel 对方通道
  * @param channel2 自己通道
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse getBackPark(String Extension,String parked) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  OriginateAction origaction=new OriginateAction();
  origaction.setContext("from-internal");//路由器
  String action=BaseController.getactionId("Originate", Extension);
  origaction.setActionId(action);//呼叫action id
  origaction.setChannel("SIP/"+Extension);//呼叫连接通道,呼叫时,先呼通自己  
  origaction.setExten(parked);
  origaction.setPriority(1);//优先级设置1时,不会回拨,为什么
  origaction.setAsync(true);//设置异步才会有结果返回
  ManagerResponse response =BaseUtil.getConnection().sendAction(origaction);   
  return response;
 }

 

 /**
  * 桥接通话
  * @param Extension
  * @param channel 对方通道
  * @param channel2 自己通道
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse getBridge(String Extension) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  MapCacheUtil mapcache=MapCacheUtil.getInstance();
  BridgeAction bridge=new BridgeAction();
  String channel=BaseUtil.getChannel(Extension);
  bridge.setActionId(BaseController.getactionId("Bridge", Extension));
  bridge.setChannel1(String.valueOf(mapcache.get(channel)));
  bridge.setChannel2(channel);
  bridge.setTone(true);
  //park.setTimeout(30000);//保留通话的时间
  ManagerResponse response=BaseUtil.getConnection().sendAction(bridge);
  return response;
 }

 

 /**
  * 获取分机示忙示闲状态值
  * @param Extension
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse getdatabasednd(String Extension) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  DbGetAction db=new DbGetAction();
  db.setActionId(BaseController.getactionId("DND", Extension));
  db.setFamily("DND");
  db.setKey(Extension);
  ManagerResponse response=BaseUtil.getConnection().sendAction(db);
  return response;
 }


 

/**
  * 设置分机示闲状态值
  * @param Extension
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse deldatabasednd(String Extension) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  DbDelAction db=new DbDelAction();
  db.setActionId(BaseController.getactionId("DND", Extension));
  db.setFamily("DND");
  db.setKey(Extension);
  ManagerResponse response=BaseUtil.getConnection().sendAction(db);
  return response;
 }
 /**
  * 设置分机示忙状态值
  * @param Extension
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalStateException
  * @throws IOException
  * @throws TimeoutException
  */
 public static ManagerResponse setdatabasednd(String Extension) throws IllegalArgumentException, IllegalStateException, IOException, TimeoutException{
  if(BaseUtil.getConnection()==null){
   BaseUtil.getAsteriskJava();
  }
  DbPutAction db=new DbPutAction();
  db.setActionId(BaseController.getactionId("DND", Extension));
  db.setFamily("DND");
  db.setKey(Extension);
  db.setVal("yes");
  ManagerResponse response=BaseUtil.getConnection().sendAction(db);
  return response;
 }

 

 类似资料: