免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1691 | 回复: 0

[iOS] 仿微信界面的智能聊天机器人源码 [复制链接]

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2015-06-24 22:20:00
发表于 2015-06-23 11:33 |显示全部楼层
这是一个IOS智能聊天机器人的源码,采用了仿微信的风格设计,调用的是图灵机器人的API,能够实现智能聊天、讲故事、讲笑话、查天气、查公交等丰富的功能。代码如下:
1.仿微信界面: UITableView
  1. [code]//add UItableView
  2.     self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-88) style:UITableViewStylePlain];
  3.     [self.tableView registerClass:[ChartCell class] forCellReuseIdentifier:cellIdentifier];
  4.     self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
  5.     self.tableView.allowsSelection = NO;
  6.     self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chat_bg_default.jpg"]];
  7.     self.tableView.dataSource=self;
  8.     self.tableView.delegate=self;
  9.     [self.view addSubview:self.tableView];
复制代码
2.仿微信界面:KeyBordVIew
  1. [code]//add keyBorad
  2.     self.keyBordView=[[KeyBordVIew alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-44, self.view.frame.size.width, 44)];
  3.     self.keyBordView.delegate=self;
  4.     [self.view addSubview:self.keyBordView];
  5.   
  6.     //注册通知, 键盘收起, 弹出
  7.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
  8.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
  9.   
  10. //键盘弹出响应
  11. -(void)keyboardShow:(NSNotification *)note
  12. {
  13.     CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  14.     CGFloat deltaY=keyBoardRect.size.height;
  15.       
  16.     [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
  17.          
  18.         self.view.transform=CGAffineTransformMakeTranslation(0, -deltaY);
  19.     }];
  20. }
  21.   
  22. //键盘收起响应
  23. -(void)keyboardHide:(NSNotification *)note
  24. {
  25.     [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
  26.         self.view.transform = CGAffineTransformIdentity;
  27.     }];
  28. }
复制代码
3. 图灵机器人API的获取
  1. //API申请地址:www.tuling123.com
  2. //API体验地址:http://www.tuling123.com/openapi/cloud/proexp.jsp
  3. //在注册之前,需要注册图灵机器人API并获取自己的APIkey,然后才能够进行下一步的开发工作。
复制代码
4.图灵机器人API调用
  1. [code]//每当编辑完问题后
  2. //1. 显示自己的问题 messageType=1
  3. //2. 调用API,返回结果
  4. -(void)KeyBordView:(KeyBordVIew *)keyBoardView textFiledReturn:(UITextField *)textFiled
  5. {
  6.       
  7.     //显示自己的问题
  8.     ChartCellFrame *cellFrame=[[ChartCellFrame alloc]init];
  9.     ChartMessage *chartMessage=[[ChartMessage alloc]init];
  10.       
  11.     chartMessage.icon=@"icon01.png";
  12.     chartMessage.messageType=1;
  13.     chartMessage.content=textFiled.text;
  14.     cellFrame.chartMessage=chartMessage;
  15.       
  16.     [self.cellFrames addObject:cellFrame];
  17.     [self.tableView reloadData];
  18.       
  19.     //滚动到当前行
  20.     [self tableViewScrollCurrentIndexPath];
  21.       
  22.     //利用用户问题, 查询结果
  23.       
  24.     //API请求格式。 具体格式见图灵官网
  25.     //6c2cfaf7a7f088e843b550b0c5b89c26 替换成你申请的key即可
  26.     NSString* urlString = [NSString stringWithFormat:@"http://www.tuling123.com/openapi/api?key=6c2cfaf7a7f088e843b550b0c5b89c26&&info=%@", textFiled.text];
  27.       
  28.     //NSUTF8StringEncoding编码。 避免中文错误
  29.         urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  30.     //调用API
  31.     NSURL *url = [NSURL URLWithString:urlString];
  32.     testRequest = [ASIHTTPRequest requestWithURL:url];
  33.     [testRequest setDelegate:self];
  34.     [testRequest startAsynchronous];
  35.       
  36.     textFiled.text=@"";
  37.     myTextField = textFiled;
  38. }
  39.   
  40. #pragma mark - 返回机器人回答
  41. //调用API完毕, 返回图灵回答结果
  42. //1. 收起键盘
  43. //2. 显示回答内容
  44. - (void)requestFinished:(ASIHTTPRequest *)request
  45. {
  46.       
  47.     //收起键盘
  48.     [myTextField resignFirstResponder];
  49.       
  50.     // 当以文本形式读取返回内容时用这个方法
  51.     // 解析返回的json数据
  52.     NSString *responseString = [request responseString];
  53.     self.testDic = [responseString objectFromJSONString];
  54.     self.testArr = [testDic objectForKey:@"text"];
  55.       
  56.       
  57.     //显示回答内容
  58.     ChartCellFrame *cellFrame=[[ChartCellFrame alloc]init];
  59.     ChartMessage *chartMessage=[[ChartMessage alloc]init];
  60.       
  61.     chartMessage.icon=@"icon02.png";
  62.     chartMessage.messageType=0;
  63.     chartMessage.content=[NSString stringWithFormat:@"%@", self.testArr];
  64.     cellFrame.chartMessage=chartMessage;
  65.       
  66.     [self.cellFrames addObject:cellFrame];
  67.     [self.tableView reloadData];
  68.       
  69.     //滚动到当前行
  70.     [self tableViewScrollCurrentIndexPath];
  71.       
  72. }
  73.   
  74. // API请求失败
  75. - (void)requestFailed:(ASIHTTPRequest *)request
  76. {
  77.     NSError *error = [request error];
  78.     NSLog(@"error --- %@",error);
  79.       
  80.     UIAlertView *alert_ = [[UIAlertView alloc]initWithTitle:@"提示" message:@"无网络可用,请检查网络状态" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles: nil];
  81.     [alert_ show];
  82. }
复制代码
1.jpg
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP