-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpnbot.slide
120 lines (71 loc) · 4.33 KB
/
pnbot.slide
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
GoでTwitter bot を作ってみた🐤
横浜Go読書会 #7
10 Jun 2017
Tooru Takahashi
@tooru
https://github.com/tooru/pnbot
* 話すこと
- Twitter Botとは
- GoでのTwitter Botの作り方 (準備・配信型・応答型)
- デモ
- やってみて所感
- まとめ
* Twitter Bot とは
.link http://twinavi.jp/guide/section/twitter/glossary/%E3%83%9C%E3%83%83%E3%83%88%EF%BC%88bot%EF%BC%89%E3%81%A8%E3%81%AF ツイナビ - ボット(bot)とは
Twitter の機能を使って作られた、機械による自動発言システム。語源はロボットから来ている。
特定の時間に自動ツイートする bot、ユーザーの bot宛の発言にリプライする bot、
特定のキーワードに反応する bot 等、様々な bot が存在する。
自動ツイートの例
.link https://twitter.com/traindelay @traindelay 電車遅延なう
# .link https://twitter.com/kiri_tori @kiri_tori
チャットボット作成サポートの事業もある
.link http://ai.userlocal.jp/document/about/ 株式会社ユーザーローカル - チャットボットとは
LINE / Facebook / Slack も流行っている?
.link http://appllio.com/20161009-8595-line-reversi-bot LINEトーク上で「リバーシ」を遊べるチャットボットが面白い
古くは、、IRCの頃から
.link http://www.din.or.jp/~ohzaki/uzura.htm 人工無能うずら(人工痴能)の部屋
* GoでのTwitter Botの作り方 1/3 (準備)
1. Twitter APIを使えるようにする
.link https://apps.twitter.com Twitter Application Management
Twitter APIを叩くアプリを登録し、API Key等を入手
2. Twitter Library を取得
$ go get github.com/dghubble/go-twitter/twitter
$ go get github.com/dghubble/oauth1
今回はdghubble/go-twitter を使用、ChimeraCoder/anaconda もよさそう
両方ともTwitter Developer Documentationに載っている
.link https://dev.twitter.com/resources/twitter-libraries Twitter Libraries - Twitter Developers
* GoでのTwitter Botの作り方 2/3 配信型 (つぶやく方法)
.code -numbers slide/tweet.go
⚠️APIキーのハードコードは危険です。別ファイル等から取得しましょう。
* GoでのTwitter Botの作り方 3/3 応答型 (ざっくりと)
1. 自身へのMention (@<user>がついたツイート)を取得、アプローチは2つ
1-a. プログラム起動後のMentionに対応 (通知方式)
.link https://dev.twitter.com/streaming/userstreams User streams - Twitter Developers
1-b. プログラム起動前のMentionにも対応:HomeTimeline(自分の発言)を調べて最後に返信したMention以降に対応する (ポーリング方式)
.link https://dev.twitter.com/rest/reference/get/statuses/home_timeline GET statuses/home_timeline - Twitter Developers
.link https://dev.twitter.com/rest/reference/get/statuses/mentions_timeline GET statuses/mentions_timeline - Twitter Developers
2. ツイートを解析&応答する文字列を生成
ツイートの解析結果が取得できる (例えば、@<user>を抽出、本文を抽出など)
.link https://dev.twitter.com/overview/api/entities-in-twitter-objects#details-of-the-change Entities in Object - Twitter Developers
3. 返答する (配信型と同様、対応するつぶやきのIDを指定したほうがよい)
* デモ
素数Bot
.link https://twitter.com/dpnbot @dpnbot 素数を順番につぶやく
.link https://twitter.com/tpnbot @tpnbot 双子素数を順番につぶやく
.link https://twitter.com/ipnbot @ipnbot @ipnbotに数字をリプライすると、素数かどうかリプライする
* やってみて所感
🔷Twitter APIのアクセス頻度
アクセス頻度オーバーが起きることを考慮する必要あり(リトライ処理など)
.link https://dev.twitter.com/rest/public/rate-limiting API Rate Limits - Twitter Developers
🔷Go Routine & Channel
Channel便利。素数生成→素数ツイートで使用
🔷math/big
immutableではないので扱いに注意が必要
// 例:足し算 (a += b)
a.Add(a, b)
🔷素数判定オンメモリの限界
OOM Killerがやってくる。永続化方法を検討しないと、いずれbot継続できなくなる。
素数判定は、それ以下の全ての素数で割り切れないことを調べる必要あり。高速化のため素数をキャッシュしている。
* まとめ
- Bot楽しい
- Golang楽しい