Skip to content
Daegeun Kim edited this page Jun 15, 2013 · 21 revisions

MyPeople Node Client Library

사용하기에 앞서

  • 다음커뮤니케이션 개발자 네트워크 사이트에 접속하신 다음에 마이피플 봇 등록을 먼저 하셔서 API 키를 발급받아야 합니다.
  • 콜백을 받기위해서는 외부에서 접속이 가능한 사이트이어야 합니다.
  • 같은 업체에서 제공하는 가상 머신을 사용하는데도 다음 마이피플에서 콜백을 전달하지 못하는 경우가 더러 있습니다. (이유는 알 수 없습니다만)
  • 그리고 지정한 IP에대해서만 전송을 할 수 있습니다.
  • 해외에서 봇을 띄워서 사용하시는 경우에는 봇 계정 개인정보 설정에서 해외 접속을 허용하셔야 정상 작동합니다. (이런 문구는 눈을 씻고 찾아봐도 없습니다.)
  • 콜백 주소는 http://도메인 혹은 IP주소:포트번호/mypeople/callback 로 설정합니다.
  • 태생 자체가 hubot-mypeople 을 위해서 만들게 되었습니다. 그와 많은 연관성을 띕니다.

API 문서를 보고 싶은 경우는 아래로 더 내리면 있습니다.

hubot-mypeople

휴봇에대해서 알고 싶은 분은 링크를 누르세요. 사용 방법은 생략하며 연동하는 방법만 소개합니다. 마이피플용 휴봇 어댑터 소스 보기는 링크를 누르세요.

휴봇을 생성하고 package.json을 수정

"dependencies" 항목에 "hubot-mypeople": "0.1.1" 정보를 추가합니다.

$ hubot --create mybot
$ cd mybot
$ vi package.json

$ cat package.json
...
  "dependencies": {
    "hubot":         ">= 2.5.1",
    "hubot-scripts": ">= 2.4.2",
    "hubot-mypeople": "0.1.1",
    "express": ">=3.0.0"
  },
...

휴봇 실행

$ export HUBOT_MYPEOPLE_KEY='API KEY'
$ export HUBOT_MYPEOPLE_NICK='hubot'
$ PORT=3000 bin/hubot -a mypeople

휴봇 설명 끝 이제부터 API 설명.

Install

$ npm install mypeople

APIs

mock 서버를 만들었다면 "server" 설정을 변경하면 됩니다.

var Client = require('mypeople').Client;
var client = new Client('KEY');
// or
var client = new Client('KEY', {server: 'https://api.daum.net'});

모든 callback 함수는 첫 인자는 에러정보를 포함하며, 다음 인자는 결과 값을 담습니다. 노드가 쓰는 일반적인 형태라고 보시면 됩니다.

메세지 전송

메세지를 보내는 경우

client.sendMessage(string friendId, string content, function callback);

첨부파일을 보내는 경우

client.sendMessage(string friendId, Stream input, function callback);

Examples

client.sendMessage("FRIEND_ID", "안녕하세요. 방갑습니다.", function(err, res) {
});

var fs = require("fs");
client.sendMessage("FRIEND_ID", fs.createReadStream("/path/to/file.jpg"), function(err, res) {
});

그룹 메세지 전송

메세지를 보내는 경우

client.sendGroupMessage(string groupId, string content, function callback);

첨부파일을 보내는 경우

client.sendGroupMessage(string groupId, Stream input, function callback);

Examples

client.sendGroupMessage("GROUP_ID", "안녕하세요. 방갑습니다.", function(err, res) {
});

var fs = require("fs");
client.sendGroupMessage("GROUP_ID", fs.createReadStream("/path/to/file.jpg"), function(err, res) {
});

친구 정보 보기

  • 0.2버전 이후부터는 callback 함수 signature는 (MixedType error, Array[Object] friends) 에서 (Error error, Object friend, string data) 로 변경됩니다. data는 API서버에서 내려주는 결과 그대로를 담습니다.
client.getFriendInfo(string friendId, function callback);

Examples

var sampleResult = {
  "buddyId":"BU_zkljfo2je0fjzkljfo2je0fj",
  "name":"홍길영",
  "photoId":"myp_pub:4FFFC9490378330009"
};

client.getFriendInfo("FRIEND_ID", function(err, friend, data) {
  if (err != null) {
    console.log("Your name is " + friend.name + "(" + friend.buddyId + ")");
  }
});

그룹 멤버 보기

  • 0.2버전 이후부터는 callback 함수 signature는 (MixedType error, Array[Object] friends) 에서 (Error error, Array[Object] friends, string data) 로 변경됩니다. data는 API서버에서 내려주는 결과 그대로를 담습니다.
client.getGroupMembers(string groupId, function callback);

대화 그룹에서 나가기

client.exitGroup(string groupId, function callback);

파일 다운로드 받기

client.download(string fileId, function callback).pipe(
  stream output
);

Usages

콜백서버 띄우기

message event 결과가 "friend"가 아닌 "user"로 바뀔 수 있습니다. (0.3.1 부터 user로 변경되었습니다.) 상세한 문서 에서 볼 수 있습니다.

var Receiver = require('mypeople').Receiver;
var receiver = new Receiver('KEY');
receiver.addListener('message', function(m) {
  console.log(m);
});
receiver.start();
message format

참고로, "friend" => "user" 로 바뀔 수 있습니다.

{
  "message": "Message",
  "friend": {
    "id": "xxx"
  },
  "group": {
    "id": "xx"
  }
}