智慧聊天機器人原始碼—仿微信介面

uoou發表於2015-03-11

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];
}

enter image description here

enter image description here

相關文章