智能聊天机器人源码—仿微信界面

版权所有,禁止匿名转载;禁止商业使用。

enter code here 这是一个IOS智能聊天机器人的源码,采用了仿微信的风格设计,调用的是图灵机器人的API,能够实现智能聊天、讲故事、讲笑话、查天气、查公交等丰富的功能。还可以去图灵机器人的官网上体验产品http://www.tuling123.com/openapi/cloud/proexp.jsp


1. 仿微信界面: UITableView


/

/add UItableView
  self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-88) style:UITableViewStylePlain];
  [self.tableView registerClass:[ChartCell class] forCellReuseIdentifier:cellIdentifier];
  self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
  self.tableView.allowsSelection = NO;
  self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chat_bg_default.jpg"]];
  self.tableView.dataSource=self;
  self.tableView.delegate=self;
  [self.view addSubview:self.tableView];
2.仿微信界面:KeyBordVIew
  //add keyBorad
   self.keyBordView=[[KeyBordVIew alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-44, self.view.frame.size.width, 44)];
   self.keyBordView.delegate=self;
   [self.view addSubview:self.keyBordView];
  //注册通知, 键盘收起, 弹出
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
//键盘弹出响应
-(void)keyboardShow:(NSNotification *)note
{
   CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
   CGFloat deltaY=keyBoardRect.size.height;
   [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
  self.view.transform=CGAffineTransformMakeTranslation(0, -deltaY);
   }];
}
//键盘收起响应
-(void)keyboardHide:(NSNotification *)note
{
   [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
  self.view.transform = CGAffineTransformIdentity;
   }];
}

3. 图灵机器人API的获取


//API申请地址:www.tuling123.com
//API体验地址:http://www.tuling123.com/openapi/cloud/proexp.jsp
//在注册之前,需要注册图灵机器人API并获取自己的APIkey,然后才能够进行下一步的开发工作。

4.图灵机器人API调用


//每当编辑完问题后
//1. 显示自己的问题 messageType=1
//2. 调用API,返回结果
-(void)KeyBordView:(KeyBordVIew *)keyBoardView textFiledReturn:(UITextField *)textFiled
{
  //显示自己的问题
  ChartCellFrame *cellFrame=[[ChartCellFrame alloc]init];
  ChartMessage *chartMessage=[[ChartMessage alloc]init];
  chartMessage.icon=@"icon01.png";
  chartMessage.messageType=1;
  chartMessage.content=textFiled.text;
  cellFrame.chartMessage=chartMessage;
  [self.cellFrames addObject:cellFrame];
  [self.tableView reloadData];
  //滚动到当前行
  [self tableViewScrollCurrentIndexPath];
  //利用用户问题, 查询结果
  //API请求格式。 具体格式见图灵官网
  //6c2cfaf7a7f088e843b550b0c5b89c26 替换成你申请的key即可
  NSString* urlString = [NSString stringWithFormat:@"http://www.tuling123.com/openapi/api?key=6c2cfaf7a7f088e843b550b0c5b89c26&&info=%@", textFiled.text];
  //NSUTF8StringEncoding编码。 避免中文错误
  urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  //调用API
  NSURL *url = [NSURL URLWithString:urlString];
  testRequest = [ASIHTTPRequest requestWithURL:url];
  [testRequest setDelegate:self];
  [testRequest startAsynchronous];
  textFiled.text=@"";
  myTextField = textFiled;
}
#pragma mark - 返回机器人回答
//调用API完毕, 返回图灵回答结果
//1. 收起键盘
//2. 显示回答内容
- (void)requestFinished:(ASIHTTPRequest *)request
{
  //收起键盘
  [myTextField resignFirstResponder];
  // 当以文本形式读取返回内容时用这个方法
  // 解析返回的json数据
  NSString *responseString = [request responseString];
  self.testDic = [responseString objectFromJSONString];
  self.testArr = [testDic objectForKey:@"text"];
  //显示回答内容
  ChartCellFrame *cellFrame=[[ChartCellFrame alloc]init];
  ChartMessage *chartMessage=[[ChartMessage alloc]init];
  chartMessage.icon=@"icon02.png";
  chartMessage.messageType=0;
  chartMessage.content=[NSString stringWithFormat:@"%@", self.testArr];
  cellFrame.chartMessage=chartMessage;
  [self.cellFrames addObject:cellFrame];
  [self.tableView reloadData];
  //滚动到当前行
  [self tableViewScrollCurrentIndexPath];
}
// API请求失败
- (void)requestFailed:(ASIHTTPRequest *)request
{
  NSError *error = [request error];
  NSLog(@"error --- %@",error);
  UIAlertView *alert_ = [[UIAlertView alloc]initWithTitle:@"提示" message:@"无网络可用,请检查网络状态" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles: nil];
  [alert_ show];
}

仿微信界面,聊天机器人

仿微信界面,聊天机器人


0 0