forked from sng2c/mypeople-bot-buffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmypeople_bot_buffer
executable file
·171 lines (152 loc) · 5.19 KB
/
mypeople_bot_buffer
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env perl
use Mojolicious::Lite;
use JSON;
use LWP::UserAgent;
use LWP::Protocol::https;
use HTTP::Request::Common;
use Data::Printer;
use Encode;
# Documentation browser under "/perldoc"
plugin 'PODRenderer';
my $mech = LWP::UserAgent->new;
helper 'mech'=>sub{
return $mech;
};
plugin 'CHI'=>{
default => {
driver => 'Memory',
global => 1
}
};
sub enc{
my $s = shift;
$s = [] unless $s;
return to_json($s);
}
sub dec{
my $s = shift;
return [] unless $s;
return from_json($s);
}
get '/' => sub {
my $self = shift;
my $ip = $self->tx->local_address;
$self->render('index', ip=>$ip);
};
get '/myip' => sub {
my $self = shift;
my $data = $self->tx->remote_address;
$self->render(text=>$data);
};
my $max = 100;
any '/#id/callback' => sub{
my $self = shift;
my $id = $self->param('id');
my $msg = {};
$msg->{action} = $self->param('action');
$msg->{buddyId} = $self->param('buddyId');
$msg->{groupId} = $self->param('groupId');
$msg->{content} = $self->param('content');
$msg->{time} = time();
if( $msg->{action} ){
my $arr = dec( $self->chi->get($id) );
# 100개만 남기기
my $len = @{$arr};
if( $len > $max*1.5 ){
$arr = [splice @{$arr},(-1*$max)];
}
push(@{$arr},$msg);
$self->chi->set($id=>enc($arr));
$self->render(text=>'ok');
}
else{
$self->render(text=>'no action');
}
};
get '/#id' => sub{
my $self = shift;
my $id = $self->param('id');
my $data = $self->chi->get($id);
$data = enc unless $data;
$self->render(text=>$data);
};
get '/#id/flush' => sub{
my $self = shift;
my $id = $self->param('id');
my $data = $self->chi->get($id);
if( $data ){
$self->chi->set($id=>enc);
}
else{
$data = enc;
}
$self->render(text=>$data);
};
any '/proxy/*url' => sub{
my $self = shift;
my $req = $self->req;
my $url = $self->param('url').'?'.$self->req->query_params;
my $headers = $req->headers->to_hash;
delete($headers->{Host});
my $newres;
if( $req->method eq 'GET' ){
my $newreq = GET($url, %{$headers});
$newres = $mech->request($newreq);
}
elsif( $req->method eq 'POST'){
my $newreq = POST($url, %{$headers},Content=>$req->build_body);
$newres = $mech->request($newreq);
}
$self->render(text=>$newres->decoded_content);
};
app->start;
__DATA__
@@ index.html.ep
% layout 'default';
% title '마이피플 봇 Callback Buffer';
<div class="page-header">
<h2><%= title %></h2>
<span>마이피플 봇 API를 이용할 때에는, 사용자의 메세지를 HTTP콜백으로 받기 위해서 Public IP를 가진 웹서버가 하나 필요합니다.<br>
이 서비스는 HTTP콜백을 대신 받아서 임시로 저장해 주는 서비스로, 따로 Public IP를 가진 서버가 없더라도 마이피플 봇을 만들 수 있습니다.<br>
예를 들어, 집에 남는 PC, iPhone이나 Android에서도 마이피플 봇을 만들 수 있게 됩니다.<br>
받은 메세지들을 암호화해 주는 기능은 준비중입니다.</span>
</div>
<p>
<h3>이용방법</h3>
<ol>
<li>사용하려는 봇의 아이디를 <span class='label label-info'>mybot.bot</span>이라 했을 때, </li>
<li>콜백주소를 <%= url_for('')->to_abs %><span class='label label-info'>mybot.bot</span>/callback 으로 지정합니다.
<br>예시)<br>
<img src='<%= url_for('/img/callback_setting.png')->to_abs %>'>
</li>
<li>서버 IP는 <%= $ip %> 로 설정합니다.</li>
<li><%= url_for('')->to_abs %><span class='label label-info'>mybot.bot</span> 에서 쌓인 메세지들을 <b>조회만</b> 할 수 있습니다.</li>
<li><%= url_for('')->to_abs %><span class='label label-info'>mybot.bot</span>/flush 에서 쌓인 메세지들을 사용합니다. <strong>조회 후 삭제됩니다. 봇에서는 이 주소를 이용해 쌓인 메세지들을 가져오도록 합니다.</strong></li>
<li>각 메세지에는 time 항목이 추가됩니다. 이는 메세지를 받은 초단위 timestamp값입니다.</li>
<li>만약, 다른 사람이 내 버퍼에 접근하는 것이 싫다면, 예측할 수 없게끔 복잡하게 아이디를 지정하면 됩니다.(봇의 아이디와 같을 필요는 없습니다.)<br>예시) <%= url_for('')->to_abs %><span class='label label-info' >ddfacd30eeffef22</span>/callback</li>
<li>API를 호출할때에는 API주소앞에 <span class='label label-info'><%= url_for('proxy/')->to_abs %></span>를 붙여서 <%= url_for('proxy/')->to_abs %>https://apis.daum.net/mypeople 과 같이 해주면 됩니다. GET/POST 동일 </li>
</ol>
</p>
<p>
<h3>참고</h3>
<ul>
<li>Source : <a href='https://github.com/sng2c/mypeople-bot-buffer'>https://github.com/sng2c/mypeople-bot-buffer</a>
<li>MyPeople API : <a href='http://dna.daum.net/apis/mypeople'>http://dna.daum.net/apis/mypeople</a>
<li>Net::MyPeople::Bot: <a href='https://github.com/sng2c/net-mypeople-bot'>https://github.com/sng2c/net-mypeople-bot</a></li>
</ul>
</p>
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel="stylesheet" href="<%= url_for("/css/bootstrap.min.css") %>" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script language="javascript" src="<%= url_for("/js/bootstrap.min.js") %>"></script>
</head>
<body>
<div class="container">
<%= content %>
</div>
</body>
</html>