NIMMessageMaker.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // NIMMessageMaker.m
  3. // NIMKit
  4. //
  5. // Created by chris.
  6. // Copyright (c) 2015年 NetEase. All rights reserved.
  7. //
  8. #import "NIMMessageMaker.h"
  9. #import "NSString+NIMKit.h"
  10. #import "NIMKitLocationPoint.h"
  11. #import "NIMKit.h"
  12. #import "YOUPAINIMInputAtCache.h"
  13. @implementation NIMMessageMaker
  14. + (NIMMessage*)msgWithText:(NSString*)text
  15. {
  16. NIMMessage *textMessage = [[NIMMessage alloc] init];
  17. textMessage.text = text;
  18. return textMessage;
  19. }
  20. + (NIMMessage*)msgWithAudio:(NSString*)filePath
  21. {
  22. NIMAudioObject *audioObject = [[NIMAudioObject alloc] initWithSourcePath:filePath scene:NIMNOSSceneTypeMessage];
  23. NIMMessage *message = [[NIMMessage alloc] init];
  24. message.messageObject = audioObject;
  25. message.text = @"发来了一段语音";
  26. return message;
  27. }
  28. + (NIMMessage*)msgWithVideo:(NSString*)filePath
  29. {
  30. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  31. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
  32. NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
  33. NIMVideoObject *videoObject = [[NIMVideoObject alloc] initWithSourcePath:filePath scene:NIMNOSSceneTypeMessage];
  34. videoObject.displayName = [NSString stringWithFormat:@"视频发送于%@",dateString];
  35. NIMMessage *message = [[NIMMessage alloc] init];
  36. message.messageObject = videoObject;
  37. message.apnsContent = @"发来了一段视频";
  38. return message;
  39. }
  40. + (NIMMessage*)msgWithImage:(UIImage*)image
  41. {
  42. NIMImageObject *imageObject = [[NIMImageObject alloc] initWithImage:image scene:NIMNOSSceneTypeMessage];
  43. NIMImageOption *option = [[NIMImageOption alloc] init];
  44. option.compressQuality = 0.7;
  45. imageObject.option = option;
  46. return [NIMMessageMaker generateImageMessage:imageObject];
  47. }
  48. + (NIMMessage *)msgWithImagePath:(NSString*)path
  49. {
  50. NIMImageObject * imageObject = [[NIMImageObject alloc] initWithFilepath:path scene:NIMNOSSceneTypeMessage];
  51. return [NIMMessageMaker generateImageMessage:imageObject];
  52. }
  53. + (NIMMessage *)generateImageMessage:(NIMImageObject *)imageObject
  54. {
  55. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  56. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
  57. NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
  58. imageObject.displayName = [NSString stringWithFormat:@"图片发送于%@",dateString];
  59. NIMMessage *message = [[NIMMessage alloc] init];
  60. message.messageObject = imageObject;
  61. message.apnsContent = @"发来了一张图片";
  62. return message;
  63. }
  64. + (NIMMessage*)msgWithLocation:(NIMKitLocationPoint *)locationPoint{
  65. NIMLocationObject *locationObject = [[NIMLocationObject alloc] initWithLatitude:locationPoint.coordinate.latitude
  66. longitude:locationPoint.coordinate.longitude
  67. title:locationPoint.title];
  68. NIMMessage *message = [[NIMMessage alloc] init];
  69. message.messageObject = locationObject;
  70. message.apnsContent = @"发来了一条位置信息";
  71. return message;
  72. }
  73. + (NIMMessage *)msgWithRobotQuery:(NSString *)text
  74. toRobot:(NSString *)robotId
  75. {
  76. NIMMessage *queryMessage = [[NIMMessage alloc] init];
  77. //要在界面上显示的消息
  78. queryMessage.text = text;
  79. NSString *robotContent = [[NIMKit sharedKit] infoByUser:robotId option:nil].showName;
  80. NSString *content = [text stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@%@%@",NIMInputAtStartChar,robotContent,NIMInputAtEndChar] withString:@""];
  81. // bot 服务不接受空字符串,当上层没有查询的时候,发送长度为 1 的空格 (这个由上层交互自己定义)
  82. content = content.length? content : @" ";
  83. NIMRobotObject *object = [[NIMRobotObject alloc] initWithRobot:content robotId:robotId];
  84. queryMessage.messageObject = object;
  85. return queryMessage;
  86. }
  87. + (NIMMessage *)msgWithRobotSelect:(NSString *)text
  88. target:(NSString *)target
  89. params:(NSString *)params
  90. toRobot:(NSString *)robotId
  91. {
  92. NIMMessage *queryMessage = [[NIMMessage alloc] init];
  93. NIMRobotObject *object = [[NIMRobotObject alloc] initWithRobotId:robotId target:target param:params];
  94. queryMessage.messageObject = object;
  95. queryMessage.text = text;
  96. return queryMessage;
  97. }
  98. @end