line_message_creator 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CHANGELOG.md +4 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.ja.md +285 -0
- data/README.md +288 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/line_message_creator.rb +128 -0
- data/lib/line_message_creator/version.rb +3 -0
- data/line_message_creator.gemspec +28 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6a66f2a12349d4959dd96601a8f47462892a160a5a6b60c41ed689c7ebf73dee
|
4
|
+
data.tar.gz: a5662a6ee977566d294db575f801b25913d47fe01ea48e9ac9a58f28cb052834
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7e5e66fc378adfe6879bce46970d0f471a1c09e258acfb92e162ebc52688c56f74865157ef8137d4aba8fab225f7a7b9bc2ffcf5a4b113c0af6c86a54de11994
|
7
|
+
data.tar.gz: 9298a2838a0a82b018eb3b453f4b041a96d676b6750d5ad0cc7d063888815e68af18d1f4f109a9b06d1ee1b2d645b99d570704b9773aa7bada3253bd26a71e03
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at hiroto100114@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Hiroto Ohira
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.ja.md
ADDED
@@ -0,0 +1,285 @@
|
|
1
|
+
# LineMessageCreator
|
2
|
+
|
3
|
+
## Overview
|
4
|
+
|
5
|
+
LineMessageCreator は、Rails向けのViewライクなLINEメッセージ作成ツールです。
|
6
|
+
LINE Messaging API に対応したメッセージを簡単に作成することができます。
|
7
|
+
`line-bot-sdk-ruby` gem を使用することを前提としています。
|
8
|
+
|
9
|
+
以下のドキュメントを読んで下さい。
|
10
|
+
|
11
|
+
- https://developers.line.biz/ja/docs/messaging-api/overview/ <br>
|
12
|
+
- https://github.com/line/line-bot-sdk-ruby
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
次のように Gemfile に追加して下さい。
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'line_message_creator'
|
20
|
+
```
|
21
|
+
|
22
|
+
更に、次のコマンドを実行して下さい。
|
23
|
+
|
24
|
+
$ bundle install
|
25
|
+
|
26
|
+
もしくは、以下のコマンドでインストールできます。
|
27
|
+
|
28
|
+
$ gem install line_message_creator
|
29
|
+
|
30
|
+
## Set Up
|
31
|
+
|
32
|
+
最初に、LineMessageCreatorの設定を行う必要があります。
|
33
|
+
2つのパラメータを設定します。
|
34
|
+
もし、あなたが Rails を使っているなら、次のコードを書いて下さい。
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
# config/initializer/line_message_creator.rb
|
38
|
+
|
39
|
+
LineMessageCreator.line_message_dir = Rails.root.join('app/line_messages')
|
40
|
+
LineMessageCreator.helper_dir = Rails.root.join('app/line_messages/helpers')
|
41
|
+
```
|
42
|
+
|
43
|
+
- `LineMessageCreator.line_message_dir` は、LINE メッセージを格納するディレクトリです。
|
44
|
+
LineMessageCreatorは、ここで設定されたディレクトリ以下からLINEメッセージのファイルを探索します。
|
45
|
+
(e.g. `app/line_messages/**/*.*`)
|
46
|
+
|
47
|
+
- `LineMessageCreator.helper_dir` は、ヘルパーファイルを格納するディレクトリです。
|
48
|
+
LineMessageCreatorは、ここで設定されたディレクトリ以下からヘルパーファイルを探索します。
|
49
|
+
(e.g. `app/line_messages/helpers/**/*.rb`)
|
50
|
+
|
51
|
+
次に、上記で設定したパスに実際にディレクトリを作成する必要があります。
|
52
|
+
あなたが Rails を使っているなら、`your/rails_root/app/line_messages` と `your/rails_root/app/line_messages/helpers` です。
|
53
|
+
以下のコマンドを実行して下さい。(あなたのワーキングディレクトリが Rails のルートディレクトリだと仮定します。)
|
54
|
+
|
55
|
+
$ mkdir -p app/line_messages/helpers
|
56
|
+
|
57
|
+
これで準備は完了です。
|
58
|
+
|
59
|
+
## Usage
|
60
|
+
|
61
|
+
実際にLINEメッセージを書いて、送信する手順を示します。
|
62
|
+
`lien_messages` ディレクトリの中に、LINE のメッセージファイルを作成します。
|
63
|
+
|
64
|
+
例えば、次のようになります。
|
65
|
+
|
66
|
+
```text
|
67
|
+
# app/line_messages/sample_line_message.txt
|
68
|
+
|
69
|
+
ここに、好きなLINEのメッセージを書きます。
|
70
|
+
```
|
71
|
+
|
72
|
+
そのファイルを読み込むには、次のメソッドを呼び出します。
|
73
|
+
引数はLINEメッセージのファイル名です。
|
74
|
+
引数には、ファイルの拡張子を除いたファイル名を渡していることに注意して下さい。
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
LineMessageCreator.create_from('sample_line_message')
|
78
|
+
|
79
|
+
#=> [{ type: "text", text: "ここに、好きなLINEのメッセージを書きます。" }]
|
80
|
+
```
|
81
|
+
|
82
|
+
実際にLINEメッセージを送信するには、以下のように書きます。
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
class YourController < ApplicationController
|
86
|
+
|
87
|
+
# 'line-bot-sdk-ruby' gem が必要です。
|
88
|
+
def client
|
89
|
+
@client ||= Line::Bot::Client.new { |config|
|
90
|
+
config.channel_id = credentials.line_api[:message][:channel_id]
|
91
|
+
config.channel_secret = credentials.line_api[:message][:channel_secret_id]
|
92
|
+
config.channel_token = credentials.line_api[:message][:channel_token]
|
93
|
+
}
|
94
|
+
end
|
95
|
+
|
96
|
+
# 応答メッセージを送信するには、リプライトークンが必要です。
|
97
|
+
# この例では、リプライトークンを取得するコードは省略しています。
|
98
|
+
# 重要なことは、LineMessageCreator.create_from の実行結果を、
|
99
|
+
# そのまま client.reply_message の引数に渡すことができる点です。
|
100
|
+
def reply(reply_token, *file_names, **locals)
|
101
|
+
messages = LineMessageCreator.create_from(*file_names, **locals)
|
102
|
+
client.reply_message(reply_token, messages)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
## Variation
|
108
|
+
|
109
|
+
対応しているLINEメッセージファイルは、以下の通りです。
|
110
|
+
|
111
|
+
| 拡張子 | 内容 |
|
112
|
+
| :----: | :---- |
|
113
|
+
| txt | テキストファイルです。 |
|
114
|
+
| erb | ERBファイルです。<br>ファイルの内容をERBで評価した結果をメッセージとして送信できます。 |
|
115
|
+
| json | JSONファイルです。<br>クイックリプライの内容を記述します。 |
|
116
|
+
|
117
|
+
対応している LINE Messaging API の種類は、以下の通りです。
|
118
|
+
|
119
|
+
- 応答メッセージ
|
120
|
+
- プッシュメッセージ
|
121
|
+
- マルチキャストメッセージ
|
122
|
+
- ナローキャストメッセージ
|
123
|
+
- ブロードキャストメッセージ
|
124
|
+
- クイックリプライ
|
125
|
+
|
126
|
+
## ERB
|
127
|
+
|
128
|
+
RailsのViewのように、メッセージの内容をERBで記述できます。
|
129
|
+
ERB内で使用する変数は、`LineMessageCreator.create_form` の引数で渡すことができます。
|
130
|
+
|
131
|
+
例えば、次のERBファイルを作成したとします。
|
132
|
+
|
133
|
+
```erbruby
|
134
|
+
# app/line_messages/greet_message.txt.erb
|
135
|
+
|
136
|
+
こんにちは、<%= user.name %>さん。
|
137
|
+
```
|
138
|
+
|
139
|
+
上記のファイルを読み込むには、次のようにをメソッドを呼び出します。
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
sample_user = User.first
|
143
|
+
LineMessageCreator.craete_form('greet_message', user: sample_user)
|
144
|
+
```
|
145
|
+
|
146
|
+
ERBファイルでは、後述するヘルパーを使用する事ができます。
|
147
|
+
|
148
|
+
## Helper
|
149
|
+
|
150
|
+
ヘルパーを使用することで、ERBファイル内で使用する変数を簡単に定義できます。
|
151
|
+
|
152
|
+
次のERBファイルがあるとします。
|
153
|
+
|
154
|
+
```erbruby
|
155
|
+
# app/line_messages/greet_message.txt.erb
|
156
|
+
|
157
|
+
日付:<%= current_data %>
|
158
|
+
こんにちは、<%= user.name %>さん。
|
159
|
+
```
|
160
|
+
|
161
|
+
ここで、 `greet_message.txt.erb` で使用するヘルパーファイルを作成します。
|
162
|
+
ファイル名は、 `<message_file_name>_heler.rb`にする必要があります。
|
163
|
+
|
164
|
+
この例では、次のようになります。
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
# app/line_messages/helpers/greet_message_helper.rb
|
168
|
+
|
169
|
+
Module GreetMessageHelper
|
170
|
+
def current_data
|
171
|
+
current_time = Time.current
|
172
|
+
month = current_time.month
|
173
|
+
day = current_time.day
|
174
|
+
|
175
|
+
"#{month}/#{day}"
|
176
|
+
end
|
177
|
+
end
|
178
|
+
```
|
179
|
+
|
180
|
+
この状態で、以下のように呼び出すことができます。
|
181
|
+
`LineMessageCreator`は自動的にヘルパーファイルを読み込みます。
|
182
|
+
|
183
|
+
```ruby
|
184
|
+
sample_user = User.first
|
185
|
+
LineMessageCreator.craete_form('greet_message', user: sample_user) # { current_data: object} を渡していない点に注目して下さい。
|
186
|
+
```
|
187
|
+
|
188
|
+
このヘルパーファイルの仕組みは、RailsのViewヘルパーの仕組みを目指しましたが、完璧ではありません。
|
189
|
+
ヘルパーファイルの注意点を以下にまとめます。
|
190
|
+
|
191
|
+
- ファイル: ヘルパーファイルは、LINEメッセージファイル毎に用意する必要があります。
|
192
|
+
|
193
|
+
- 探索: LINEメッセージファイルを読み込む際に、自動的にヘルパーファイルも探します。
|
194
|
+
ヘルパーファイルが見つかった場合、`LineMessageCreator`はヘルパーファイルを読み込み、ERBファイルを評価します。
|
195
|
+
ヘルパーファイルが見つからなかった場合、`LineMessageCreator`はヘルパーファイルを読み込まず、ERBファイルを評価します。
|
196
|
+
|
197
|
+
- 命名: ヘルパーファイル名は、 `<message_file_name>_heler.rb`にする必要があります。
|
198
|
+
また、モジュール名は、ヘルパーファイル名のキャメルケースである必要があります。
|
199
|
+
|
200
|
+
- メソッド: ヘルパーファイルでは、引数を持つメソッドを定義することはできません。
|
201
|
+
代わりに、プロックオブジェクトを返すメソッドを定義し、`proc_method.call(args)`で呼び出して下さい
|
202
|
+
|
203
|
+
## 複数のメッセージ
|
204
|
+
|
205
|
+
複数のメッセージを送信することができます。
|
206
|
+
|
207
|
+
例えば、次のようになります。
|
208
|
+
|
209
|
+
```ruby
|
210
|
+
messages = LineMessageCreator.create_from('first_message', 'second_message', 'third_message')
|
211
|
+
client.reply_message(reply_token, messages)
|
212
|
+
```
|
213
|
+
|
214
|
+
メッセージはファイル毎に送信されます。
|
215
|
+
複数のメッセージが統合されることはありません。
|
216
|
+
|
217
|
+
複数のERBファイルを送信する場合、使用する全てのオブジェクトをハッシュで渡す必要があります。
|
218
|
+
|
219
|
+
例えば、次のようになります。
|
220
|
+
|
221
|
+
```ruby
|
222
|
+
messages = LineMessageCreator.create_from('first_erb', 'second_erb', hoge: first_obj, fuga: second_obj)
|
223
|
+
client.reply_message(reply_token, messages)
|
224
|
+
```
|
225
|
+
|
226
|
+
## クイックリプライ
|
227
|
+
|
228
|
+
クイックリプライを送信することができます。
|
229
|
+
クイックリプライファイルを`LineMessageCreator.line_message_dir` で設定したディレクトリ以下に作成します。
|
230
|
+
拡張子は、 `.json`にする必要があります。
|
231
|
+
|
232
|
+
例えば、次のようになります。
|
233
|
+
|
234
|
+
```json
|
235
|
+
// app/line_messages/quick_reply.json
|
236
|
+
|
237
|
+
{
|
238
|
+
"items": [
|
239
|
+
{
|
240
|
+
"type": "action",
|
241
|
+
"imageUrl": "https://example.com/sushi.png",
|
242
|
+
"action": {
|
243
|
+
"type": "message",
|
244
|
+
"label": "Sushi",
|
245
|
+
"text": "Sushi"
|
246
|
+
}
|
247
|
+
},
|
248
|
+
{
|
249
|
+
"type": "action",
|
250
|
+
"action": {
|
251
|
+
"type": "location",
|
252
|
+
"label": "Send location"
|
253
|
+
}
|
254
|
+
}
|
255
|
+
]
|
256
|
+
}
|
257
|
+
```
|
258
|
+
|
259
|
+
クイックリプライファイルは、メッセージファイルの1つとして読み込むことができます。
|
260
|
+
クイックリプライを送信するには、次のようにします。
|
261
|
+
|
262
|
+
```ruby
|
263
|
+
messages = LineMessageCreator.create_from('line_massage', 'quick_reply')
|
264
|
+
client.reply_message(reply_token, messages)
|
265
|
+
```
|
266
|
+
|
267
|
+
クイックリプライファイルを必ず最後に指定する必要はありません。
|
268
|
+
|
269
|
+
もちろん、ERBファイルと併用することもできます。
|
270
|
+
|
271
|
+
```ruby
|
272
|
+
sample_user = USer.first
|
273
|
+
messages = LineMessageCreator.create_from('erb_massage', 'quick_reply', user: sample_user)
|
274
|
+
client.reply_message(reply_token, messages)
|
275
|
+
```
|
276
|
+
|
277
|
+
複数のクイックリプライファイルが指定された場合、最後に指定されたクイックリプライファイルが有効になります。
|
278
|
+
|
279
|
+
## License
|
280
|
+
|
281
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
282
|
+
|
283
|
+
## Code of Conduct
|
284
|
+
|
285
|
+
Everyone interacting in the LineMessageCreator project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/line_message_creator/blob/master/CODE_OF_CONDUCT.md).
|
data/README.md
ADDED
@@ -0,0 +1,288 @@
|
|
1
|
+
# LineMessageCreator
|
2
|
+
|
3
|
+
## Overview
|
4
|
+
|
5
|
+
Are you japanese? => [Japanese README](https://github.com/HirotoOhria/line_message_creator/blob/master/README.ja.md)
|
6
|
+
|
7
|
+
LineMessageCreator is a View-like line for Rails Message Creation Tool.
|
8
|
+
It allows you to create messages for LINE Messaging API easily.
|
9
|
+
It is based on the `line-bot-sdk-ruby` gem.
|
10
|
+
|
11
|
+
Please read the following documentation.
|
12
|
+
|
13
|
+
- https://developers.line.biz/en/docs/messaging-api/overview/
|
14
|
+
- https://github.com/line/line-bot-sdk-ruby
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your application's Gemfile:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem 'line_message_creator'
|
22
|
+
```
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
$ bundle install
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
|
30
|
+
$ gem install line_message_creator
|
31
|
+
|
32
|
+
## Set Up
|
33
|
+
|
34
|
+
First, we need to configure LineMessageCreator.
|
35
|
+
We need to set up two parameters.
|
36
|
+
If you're using Rails, you'll write the following code
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
# config/initializer/line_message_creator.rb
|
40
|
+
|
41
|
+
LineMessageCreator.line_message_dir = Rails.root.join('app/line_messages')
|
42
|
+
LineMessageCreator.helper_dir = Rails.root.join('app/line_messages/helpers')
|
43
|
+
```
|
44
|
+
|
45
|
+
- The `LineMessageCreator.line_message_dir` is an extension to the Line This is the directory where messages are stored.
|
46
|
+
LineMessageCreator will create a line message from the directory set here. Searches for files in the message.
|
47
|
+
(e.g. `app/line_messages/**/*.*`)
|
48
|
+
|
49
|
+
- The `LineMessageCreator.helper_dir` sets the helper file to This is the directory to store the messages.
|
50
|
+
LineMessageCreator is a helper Search for files.
|
51
|
+
(e.g. `app/line_messages/helpers/**/*.rb`)
|
52
|
+
|
53
|
+
Next, you'll need to actually create the directory in the path you've set up above.
|
54
|
+
If you're using Rails, you'll need to create a directory in `your/rails_root/app/line _messages` and `your/rails_root/app/line_ messages/helpers`.
|
55
|
+
Run the following command (We'll assume your working directory is the root directory of Rails.)
|
56
|
+
|
57
|
+
$ mkdir -p app/line_messages/helpers
|
58
|
+
|
59
|
+
Now you're ready to go.
|
60
|
+
|
61
|
+
## Usage
|
62
|
+
|
63
|
+
Here's how to actually write and send a LINE message.
|
64
|
+
Create a LINE message file in the `lien_messages` directory and put it in the For example
|
65
|
+
|
66
|
+
For example, you can do the following.
|
67
|
+
|
68
|
+
```text
|
69
|
+
# app/line_messages/sample_line_message.txt
|
70
|
+
|
71
|
+
Write your favorite line message here.
|
72
|
+
```
|
73
|
+
|
74
|
+
To read the file, we call the following method.
|
75
|
+
The argument is the file name of the LINE message.
|
76
|
+
Note that we are passing the file name without the file extension in the argument.
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
LineMessageCreator.create_from('sample_line_message')
|
80
|
+
|
81
|
+
#=> [{ type: "text", text: "Write your favorite line message here." }]
|
82
|
+
```
|
83
|
+
|
84
|
+
To actually send a line message, write the following
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
class YourController < ApplicationController
|
88
|
+
|
89
|
+
# You need the 'line-bot-sdk-ruby' gem.
|
90
|
+
def client
|
91
|
+
@client ||= Line::Bot::Client.new { |config|
|
92
|
+
config.channel_id = credentials.line_api[:message][:channel_id]
|
93
|
+
config.channel_secret = credentials.line_api[:message][:channel_secret_id]
|
94
|
+
config.channel_token = credentials.line_api[:message][:channel_token]
|
95
|
+
}
|
96
|
+
end
|
97
|
+
|
98
|
+
# In order to send a response message, you need a reply token.
|
99
|
+
# In this example, the code to get the ripple token is omitted.
|
100
|
+
# It is important to note that the result of the LineMessageCreator.create_from
|
101
|
+
# as an argument to client.reply_message.
|
102
|
+
def reply(reply_token, *file_names, **locals)
|
103
|
+
messages = LineMessageCreator.create_from(*file_names, **locals)
|
104
|
+
client.reply_message(reply_token, messages)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
## Variation
|
110
|
+
|
111
|
+
The supported LINE message files are as follows.
|
112
|
+
|
113
|
+
| Extension | Description |
|
114
|
+
| :----: | :---- |
|
115
|
+
| txt | Text file. |
|
116
|
+
| erb | ERB file. <br> You can send a message with the results of the ERB evaluation of the contents of the file. |
|
117
|
+
| json | JSON file. <br> Describes the contents of the quick reply. |
|
118
|
+
|
119
|
+
対応している LINE Messaging API の種類は、以下の通りです。
|
120
|
+
|
121
|
+
- Response Message
|
122
|
+
- Push Message
|
123
|
+
- Multicast Message
|
124
|
+
- Narrowcast Message
|
125
|
+
- Broadcast Message
|
126
|
+
- Quick reply
|
127
|
+
|
128
|
+
## ERB
|
129
|
+
|
130
|
+
Just like the View in Rails, we can write the message content in an ERB.
|
131
|
+
The variables we use in the ERB are `LineMessageCreator.create_ You can pass in a `form` argument.
|
132
|
+
|
133
|
+
For example, suppose you create the following ERB file
|
134
|
+
|
135
|
+
```erbruby
|
136
|
+
# app/line_messages/greet_message.txt.erb
|
137
|
+
|
138
|
+
Hi, <%= user.name %>.
|
139
|
+
```
|
140
|
+
|
141
|
+
To load the above file, call the following method.
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
sample_user = User.first
|
145
|
+
LineMessageCreator.craete_form('greet_message', user: sample_user)
|
146
|
+
```
|
147
|
+
|
148
|
+
In the ERB file, you can use the helpers described below.
|
149
|
+
|
150
|
+
## Helper
|
151
|
+
|
152
|
+
The helper makes it easy to define the variables to be used in the ERB file.
|
153
|
+
|
154
|
+
Suppose you have the following ERB file.
|
155
|
+
|
156
|
+
```erbruby
|
157
|
+
# app/line_messages/greet_message.txt.erb
|
158
|
+
|
159
|
+
Data:<%= current_data %>
|
160
|
+
Hi, <%= user.name %>.
|
161
|
+
```
|
162
|
+
|
163
|
+
Now, create a helper file for use in `greet_message.txt.erb`.
|
164
|
+
The file name must be `<message_file_name>_heler.rb`.
|
165
|
+
|
166
|
+
Here's what happens.
|
167
|
+
|
168
|
+
```ruby
|
169
|
+
# app/line_messages/helpers/greet_message_helper.rb
|
170
|
+
|
171
|
+
Module GreetMessageHelper
|
172
|
+
def current_data
|
173
|
+
current_time = Time.current
|
174
|
+
month = current_time.month
|
175
|
+
day = current_time.day
|
176
|
+
|
177
|
+
"#{month}/#{day}"
|
178
|
+
end
|
179
|
+
end
|
180
|
+
```
|
181
|
+
|
182
|
+
In this state, you can call the following.
|
183
|
+
The `LineMessageCreator` reads the helper file automatically.
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
sample_user = User.first
|
187
|
+
LineMessageCreator.craete_form('greet_message', user: sample_user) # { current_data: object} を渡していない点に注目して下さい。
|
188
|
+
```
|
189
|
+
|
190
|
+
The mechanics of this helper file were intended to be like the View helper in Rails, but not as perfect as None.
|
191
|
+
The following are some notes on helper files
|
192
|
+
|
193
|
+
- File: A helper file must be provided for each LINE message file.
|
194
|
+
|
195
|
+
- Search: When loading a LINE message file, the system automatically looks for a helper file as well.
|
196
|
+
If a helper file is found, `LineMessageCreator` looks for a helper Reads the file and evaluates the ERB file.
|
197
|
+
If the helper file is not found, the `LineMessageCreator` calls Evaluate an ERB file without reading a helper file.
|
198
|
+
|
199
|
+
- Naming: The helper file name must be `<message_file_name>_heler. rb`.
|
200
|
+
And the module name must be a camel case of the helper file name.
|
201
|
+
|
202
|
+
- Methods: It is not possible to define methods with arguments in a helper file.
|
203
|
+
Instead, you can define a method that returns a procs object and call(args)`proc_method. call(args)`proc_method.
|
204
|
+
|
205
|
+
## Multiple messages
|
206
|
+
|
207
|
+
You can also send multiple messages.
|
208
|
+
|
209
|
+
For example, you can send the following message.
|
210
|
+
|
211
|
+
```ruby
|
212
|
+
messages = LineMessageCreator.create_from('first_message', 'second_message', 'third_message')
|
213
|
+
client.reply_message(reply_token, messages)
|
214
|
+
```
|
215
|
+
|
216
|
+
Messages are sent file by file.
|
217
|
+
Multiple messages will not be merged.
|
218
|
+
|
219
|
+
If you send more than one ERB file, you must pass in a hash of all the objects you are using There are.
|
220
|
+
|
221
|
+
For example:
|
222
|
+
|
223
|
+
```ruby
|
224
|
+
messages = LineMessageCreator.create_from('first_erb', 'second_erb', hoge: first_obj, fuga: second_obj)
|
225
|
+
client.reply_message(reply_token, messages)
|
226
|
+
```
|
227
|
+
|
228
|
+
## Quick Reply
|
229
|
+
|
230
|
+
You can send a quick reply.
|
231
|
+
You can add a quick reply file to `LineMessageCreator.line_ Create the file under the directory specified in `Message_dir`.
|
232
|
+
The extension should be `.json`.
|
233
|
+
|
234
|
+
For example:
|
235
|
+
|
236
|
+
```json
|
237
|
+
// app/line_messages/quick_reply.json
|
238
|
+
|
239
|
+
{
|
240
|
+
"items": [
|
241
|
+
{
|
242
|
+
"type": "action",
|
243
|
+
"imageUrl": "https://example.com/sushi.png",
|
244
|
+
"action": {
|
245
|
+
"type": "message",
|
246
|
+
"label": "Sushi",
|
247
|
+
"text": "Sushi"
|
248
|
+
}
|
249
|
+
},
|
250
|
+
{
|
251
|
+
"type": "action",
|
252
|
+
"action": {
|
253
|
+
"type": "location",
|
254
|
+
"label": "Send location"
|
255
|
+
}
|
256
|
+
}
|
257
|
+
]
|
258
|
+
}
|
259
|
+
```
|
260
|
+
|
261
|
+
The Quick Reply file can be loaded as one of the message files.
|
262
|
+
To send a quick reply, do the following.
|
263
|
+
|
264
|
+
```ruby
|
265
|
+
messages = LineMessageCreator.create_from('line_massage', 'quick_reply')
|
266
|
+
client.reply_message(reply_token, messages)
|
267
|
+
```
|
268
|
+
|
269
|
+
You don't always have to specify the quick reply file at the end.
|
270
|
+
|
271
|
+
Of course, it can be used in conjunction with an ERB file.
|
272
|
+
|
273
|
+
```ruby
|
274
|
+
sample_user = USer.first
|
275
|
+
messages = LineMessageCreator.create_from('erb_massage', 'quick_reply', user: sample_user)
|
276
|
+
client.reply_message(reply_token, messages)
|
277
|
+
```
|
278
|
+
|
279
|
+
If more than one quick reply file is specified, the last one specified is A quick reply file is enabled.
|
280
|
+
|
281
|
+
## License
|
282
|
+
|
283
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
284
|
+
|
285
|
+
## Code of Conduct
|
286
|
+
|
287
|
+
Everyone interacting in the LineMessageCreator project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/line_message_creator/blob/master/CODE_OF_CONDUCT.md).
|
288
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "line_message_creator"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'line_message_creator/version'
|
2
|
+
require 'erb'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module LineMessageCreator
|
6
|
+
class Error < StandardError; end
|
7
|
+
|
8
|
+
class << self
|
9
|
+
# @param [Pathname] line_message_dir Your line_message dir
|
10
|
+
# @param [Pathname] helper_dir Your helper dir
|
11
|
+
attr_accessor :line_message_dir, :helper_dir
|
12
|
+
|
13
|
+
# @param [Array] file_names File name to read (Exclude extension).
|
14
|
+
# @param [Hash] locals Local variable used in erb file.
|
15
|
+
# @return [Array] [ { type: 'text', text: 'message', quickReply: '...' }, ... ]
|
16
|
+
def create_from(*file_names, **locals)
|
17
|
+
check_initial_settings
|
18
|
+
|
19
|
+
messages = file_names.map { |file_name| read_message(file_name, **locals) }
|
20
|
+
text_messages = messages.select { |message| message.is_a?(String) }
|
21
|
+
quick_reply = messages.select { |message| message.is_a?(Hash) }.last
|
22
|
+
|
23
|
+
check_text_message(text_messages)
|
24
|
+
|
25
|
+
if quick_reply
|
26
|
+
text_messages.map { |text_message| { type: 'text', text: text_message, quickReply: quick_reply } }
|
27
|
+
else
|
28
|
+
text_messages.map { |text_message| { type: 'text', text: text_message } }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def read_message(file_name, **locals)
|
35
|
+
file_path_str = find_message_file(file_name)
|
36
|
+
|
37
|
+
case File.extname(file_path_str)
|
38
|
+
when '.txt'
|
39
|
+
read_text_message(file_path_str)
|
40
|
+
when '.erb'
|
41
|
+
read_erb_message(file_path_str, **locals)
|
42
|
+
when '.json'
|
43
|
+
read_quick_reply(file_path_str)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def find_message_file(file_name)
|
48
|
+
search_path = line_message_dir.join("**/#{file_name}.*")
|
49
|
+
Dir[search_path].first || raise(LoadError, "No such file '#{search_path}' .")
|
50
|
+
end
|
51
|
+
|
52
|
+
def find_helper_file(helper_name)
|
53
|
+
search_path = helper_dir.join("**/#{helper_name}.rb")
|
54
|
+
Dir[search_path].first
|
55
|
+
end
|
56
|
+
|
57
|
+
def read_text_message(file_path_str)
|
58
|
+
File.open(file_path_str).read
|
59
|
+
end
|
60
|
+
|
61
|
+
def read_erb_message(file_path_str, **locals)
|
62
|
+
erb_file = File.open(file_path_str)
|
63
|
+
variables = {}
|
64
|
+
|
65
|
+
if (helper_module = find_helper_module(file_path_str))
|
66
|
+
extend helper_module
|
67
|
+
method_names = helper_module.public_instance_methods(false)
|
68
|
+
variables = method_names.map { |method_name| [method_name, send(method_name)] }.to_h
|
69
|
+
end
|
70
|
+
|
71
|
+
ERB.new(erb_file.read)
|
72
|
+
.result_with_hash(**variables, **locals)
|
73
|
+
.gsub(/^\s+/, '')
|
74
|
+
end
|
75
|
+
|
76
|
+
def find_helper_module(file_path_str)
|
77
|
+
retry_counter = 0
|
78
|
+
helper_name = file_path_str.split('/').last.split('.').first + '_helper'
|
79
|
+
return unless find_helper_file(helper_name)
|
80
|
+
|
81
|
+
begin
|
82
|
+
to_const(helper_name)
|
83
|
+
rescue NameError
|
84
|
+
retry_counter += 1
|
85
|
+
if retry_counter == 1
|
86
|
+
require_helper(helper_name) && retry
|
87
|
+
else
|
88
|
+
false
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def require_helper(helper_name)
|
94
|
+
helper_path = find_helper_file(helper_name)
|
95
|
+
require helper_path
|
96
|
+
end
|
97
|
+
|
98
|
+
def to_const(str)
|
99
|
+
camel_str = to_camel(str)
|
100
|
+
Object.const_get(camel_str)
|
101
|
+
end
|
102
|
+
|
103
|
+
def to_camel(str)
|
104
|
+
str.split(/_/).map(&:capitalize).join
|
105
|
+
end
|
106
|
+
|
107
|
+
def read_quick_reply(file_path_str)
|
108
|
+
json_file = File.open(file_path_str)
|
109
|
+
JSON.parse(json_file.read)
|
110
|
+
end
|
111
|
+
|
112
|
+
def check_initial_settings
|
113
|
+
if line_message_dir.nil?
|
114
|
+
raise(TypeError, "Set LineMessageCreator.line_message_dir= e.g. Rails.root.join('app/line_messages').")
|
115
|
+
end
|
116
|
+
|
117
|
+
if helper_dir.nil?
|
118
|
+
raise(TypeError, "Set LineMessageCreator.helper_dir= e.g. Rails.root.join('app/line_messages/helpers').")
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def check_text_message(text_messages)
|
123
|
+
if text_messages.empty?
|
124
|
+
raise(ArgumentError, 'Specify a text message file.')
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'lib/line_message_creator/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "line_message_creator"
|
5
|
+
spec.version = LineMessageCreator::VERSION
|
6
|
+
spec.authors = ["Hiroto Ohira"]
|
7
|
+
spec.email = ["hiroto100114@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Simple Line message creator for Rails.}
|
10
|
+
spec.homepage = "https://github.com/HirotoOhria/line_message_creator"
|
11
|
+
spec.license = "MIT"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
|
14
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = "https://github.com/HirotoOhria/line_message_creator"
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/HirotoOhria/line_message_creator/blob/master/CHANGELOG.md"
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: line_message_creator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hiroto Ohira
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-18 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- hiroto100114@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".rspec"
|
22
|
+
- ".travis.yml"
|
23
|
+
- CHANGELOG.md
|
24
|
+
- CODE_OF_CONDUCT.md
|
25
|
+
- Gemfile
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.ja.md
|
28
|
+
- README.md
|
29
|
+
- Rakefile
|
30
|
+
- bin/console
|
31
|
+
- bin/setup
|
32
|
+
- lib/line_message_creator.rb
|
33
|
+
- lib/line_message_creator/version.rb
|
34
|
+
- line_message_creator.gemspec
|
35
|
+
homepage: https://github.com/HirotoOhria/line_message_creator
|
36
|
+
licenses:
|
37
|
+
- MIT
|
38
|
+
metadata:
|
39
|
+
homepage_uri: https://github.com/HirotoOhria/line_message_creator
|
40
|
+
source_code_uri: https://github.com/HirotoOhria/line_message_creator
|
41
|
+
changelog_uri: https://github.com/HirotoOhria/line_message_creator/blob/master/CHANGELOG.md
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 2.3.0
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubygems_version: 3.1.2
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: Simple Line message creator for Rails.
|
61
|
+
test_files: []
|