首页 > 代码库 > 【IOS源码】智能聊天机器人源码—仿微信界面

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

这是一个IOS智能聊天机器人的源码,采用了仿微信的风格设计,调用的是图灵机器人的API,能够实现智能聊天、讲故事、讲笑话、查天气、查公交等丰富的功能

[1].[代码] 仿微信界面: UITableView跳至 [1] [2] [3] [4] [5] [6]

?
1
2
3
4
5
6
7
8
9
//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:[ChartCellclass] 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 跳至 [1] [2] [3] [4] [5] [6]

?
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
    //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的获取 跳至 [1] [2] [3] [4] [5] [6]

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

[4].[代码] 图灵机器人API调用 跳至[1] [2] [3] [4] [5] [6]

?
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//每当编辑完问题后
//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];
}

[5].[图片] 图灵1.jpg 跳至[1] [2] [3] [4] [5] [6]

技术分享

[6].[图片] 图灵2.jpg 跳至[1] [2] [3] [4] [5] [6]

技术分享

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