action_message_texter 0.0.8 → 0.0.9
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 +4 -4
- data/README.md +96 -85
- data/lib/action_message_texter/base.rb +31 -3
- data/lib/action_message_texter/delivery_methods.rb +2 -0
- data/lib/action_message_texter/log_subscriber.rb +2 -0
- data/lib/action_message_texter/message.rb +2 -0
- data/lib/action_message_texter/message_delivery.rb +2 -0
- data/lib/action_message_texter/version.rb +3 -1
- data/lib/generators/action_message_texter/texter_generator.rb +2 -0
- data/lib/sms_provider/base.rb +2 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 52d4c99126aee66a02300b6615a12480d51f4c20681cf9c273b9ec020e2e0cf2
|
4
|
+
data.tar.gz: 9f88237c239af14edae7e6175434823e3bded0ff388425ca1b059257ee26c50d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a0f88ab4802086adbd69588b76659e0045fcc28ff6a0dfb0a923ffd94bdb2afe984252fc60d097922ba114b247eadb0b0ee8c13021b04250ffa1e6c91071724f
|
7
|
+
data.tar.gz: '09d0a2b7f4942bd4983f109aea575629b70b377c4838db9325829bf9c73f59297730d79ca91b940b3f306b284a7c161af2bf2b66679f01dcfbb5ecab5243da2a'
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# ActionMessageTexter
|
2
2
|
|
3
|
-
|
3
|
+
以 Rails ActionMailer 為參考打造的簡訊寄送模組,提供與 ActionMailer 一致的開發體驗
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
Add this line to your application's Gemfile:
|
@@ -19,120 +19,126 @@ Or install it yourself as:
|
|
19
19
|
$ gem install action_message_texter
|
20
20
|
```
|
21
21
|
|
22
|
-
##
|
23
|
-
|
24
|
-
1. 載入設定
|
25
|
-
先到`config/application.rb` 新增 `require action_message_texter/engine`
|
26
|
-
|
27
|
-
2. 簡訊寄送方式設定
|
22
|
+
## Usage
|
28
23
|
|
29
|
-
由於簡訊沒有類似SMTP的標準寄送方式,全依照簡訊提供商的API,目前我有包一個基於**三竹簡訊**的API,未來有計畫開放大家自行建立自己的寄送方式
|
30
|
-
|
31
|
-
```ruby
|
32
|
-
# config/application.rb
|
33
|
-
config.action_message_texter.mitake_settings= :mitake, MitakeApi::SMSProvider, url: "三竹發給你的網域名稱", username: "三竹的使用者名稱", password: "三竹的密碼"
|
34
|
-
```
|
35
24
|
|
36
|
-
|
37
|
-
在`app/application.rb`裡,可以預設所有簡訊要使用哪一個寄送方式
|
38
|
-
``` ruby
|
39
|
-
# app/application.rb
|
40
|
-
|
41
|
-
#預設值為 :mitake
|
42
|
-
config.action_message_texter.delivery_method = :mitake
|
43
|
-
```
|
44
|
-
若要依照不同環境使用不同的簡訊寄送方式 請在不同環境的設定檔(如`config/environments/development.rb`)內覆蓋這個設定,也可以在Texter中使用`delivery_method = :method`直接指定
|
45
|
-
|
46
|
-
### 完整設定如下
|
47
|
-
```ruby
|
48
|
-
# config/applications.rb
|
49
|
-
|
50
|
-
# 引用ActionMessageTexter
|
51
|
-
require 'action_message_texter/engine'
|
52
|
-
....
|
53
|
-
|
54
|
-
class Application < Rails::Application
|
55
|
-
...
|
56
|
-
# 加入三竹科技登入方式
|
57
|
-
config.action_message_texter.mitake_settings(:url: ENV['URL'], username: ENV['USERNAME'], password: ENV['password'])
|
58
|
-
# 設定預設的寄送方式
|
59
|
-
config.action_message_texter.delivery_method = :mitake
|
60
|
-
...
|
61
|
-
end
|
62
|
-
```
|
63
|
-
|
64
|
-
## Generator
|
25
|
+
請先執行`generator`
|
65
26
|
|
66
27
|
```bash
|
67
28
|
rails g action_message_texter:texter ModuleName action_name action_name ....
|
68
29
|
```
|
69
|
-
|
70
|
-
若有給定`function_name` 會自動生成對應的 function 以及字典檔
|
30
|
+
這將會產生以下文件
|
71
31
|
|
32
|
+
```
|
33
|
+
root
|
34
|
+
├─ app
|
35
|
+
│ └─ texters
|
36
|
+
│ ├─ application_texter.rb
|
37
|
+
│ └─ module_name_texter.rb
|
38
|
+
│
|
39
|
+
└─ config
|
40
|
+
└─ locales
|
41
|
+
└─ module_name.yml
|
42
|
+
```
|
43
|
+
|
44
|
+
使用上大致上跟Mailer差不多
|
72
45
|
|
73
46
|
```ruby
|
74
47
|
# app/texter/my_texter.rb
|
75
48
|
class MyTexter
|
76
|
-
def
|
77
|
-
|
49
|
+
def my_ubereats(phone)
|
50
|
+
@order = "林東芳的半筋半肉牛肉麵"
|
51
|
+
@notes = "不要牛肉不要麵"
|
52
|
+
text(to: phone)
|
78
53
|
end
|
79
54
|
end
|
80
55
|
```
|
56
|
+
與Mailer不同的是 簡訊內容不從View Render出來,請使用I18n,或是直接給 `content`
|
57
|
+
|
81
58
|
```yaml
|
82
59
|
# config/locales/texter/my_texter.yaml
|
83
60
|
zh-TW:
|
84
61
|
my_texter:
|
85
|
-
|
86
|
-
action_name2:
|
62
|
+
my_action: 今晚我想來點%{order},%{note}
|
87
63
|
```
|
88
64
|
|
89
65
|
|
90
|
-
## Texter
|
91
|
-
|
92
|
-
與Mailer雷同,但因為簡訊是比較單純的文字,因此render view了,簡訊生成的部分將使用`I18n`,同時會自動帶入參數
|
93
|
-
|
94
|
-
#### Example
|
95
|
-
假設要送出簡訊,並帶入訂單
|
96
66
|
```ruby
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
end
|
67
|
+
# app/texter/my_texter.rb
|
68
|
+
def my_ubereats(phone)
|
69
|
+
...
|
70
|
+
# 也可以直接給文字,不走I18n
|
71
|
+
text(to: phone, content: "今晚我想來點#{order},#{note}")
|
103
72
|
end
|
104
73
|
```
|
105
74
|
|
106
|
-
|
107
|
-
zh-TW:
|
108
|
-
my_texter:
|
109
|
-
uber_eat_order: "今晚,我想來%{order}"
|
110
|
-
# 記得插值不要加 `@` 喔
|
111
|
-
```
|
112
|
-
|
113
|
-
若不同的`Texter`想使用不同的method 可以直接加入 `delivery_method = :method`
|
75
|
+
跟 ActionMailer 一樣,提供 `deliver_now` 跟 `deliver_later` 兩種寄送方式,`deliver_now` 會直接寄出,`deliver_later`會調用Job做寄出。
|
114
76
|
|
115
77
|
```ruby
|
116
|
-
|
117
|
-
|
118
|
-
|
78
|
+
# 直接寄出,後續的動作需等待這個動作完成
|
79
|
+
MyTexter.my_ubereats("0987654321").deliver_now
|
80
|
+
# 調用Job,後續的動作繼續執行
|
81
|
+
MyTexter.my_ubereats("0987654321").deliver_latter
|
82
|
+
|
119
83
|
```
|
120
84
|
|
121
|
-
|
85
|
+
**於是 `0987654321` 就會收到這樣的簡訊**
|
86
|
+
> 今晚我想來點林東芳的半筋半肉牛肉麵,不要牛肉不要麵
|
122
87
|
|
123
|
-
跟 Mailer 一樣,提供 `deliver_now` 跟 `deliver_later` 兩種寄送方式,`deliver_now` 會直接寄出,`deliver_later`會調用Job做寄出。
|
124
88
|
|
125
|
-
```ruby
|
126
|
-
# 直接寄出,後續的動作需等待這個動作完成
|
127
|
-
MyTexter.uber_eat_order("0987654321", "林東芳牛肉麵").deliver_now
|
128
|
-
# 調用Job,後續的動作繼續執行
|
129
|
-
MyTexter.uber_eat_order("0987654321", "林東芳牛肉麵").deliver_latter
|
130
89
|
|
131
|
-
|
90
|
+
## Configuration
|
91
|
+
|
92
|
+
### 設定簡訊傳送方式: 三竹簡訊 API
|
93
|
+
|
94
|
+
設定方式基本跟 `ActionMailer` 一模一樣
|
95
|
+
|
96
|
+
預設內建三竹簡訊,首先請設定`網域`,`帳號`,`密碼`,如果有需要`Callback`的話,請準備一下`Callback Url`
|
97
|
+
|
98
|
+
> 有計劃提供一個介面讓大家可以自己包自己的簡訊Api模組
|
99
|
+
> 到時候只需使用 `add_delivery_method` 就可以加入自己的 Api 模組
|
100
|
+
|
101
|
+
|
102
|
+
* Require
|
103
|
+
麻煩請先到`config/application.rb` 新增 `require action_message_texter/engine`
|
104
|
+
|
105
|
+
* 設定三竹API
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
# config/application.rb
|
109
|
+
config.action_message_texter.mitake_settings= {
|
110
|
+
url: "三竹發給你的網域名稱 ex: https://smsapi.mitake.com.tw",
|
111
|
+
username: "三竹的使用者名稱",
|
112
|
+
password: "三竹的密碼",
|
113
|
+
callback_url: "https://foo.bar.com/api/v1/callback" # 這行非必填
|
114
|
+
}
|
115
|
+
```
|
116
|
+
|
117
|
+
* 設定預設寄送方式
|
118
|
+
目前只有 `mitake` 一種寄送方式,因此預設就是這個,但若有需要的話
|
119
|
+
|
120
|
+
在`app/application.rb`裡,可以預設所有簡訊要使用哪一個寄送方式
|
121
|
+
``` ruby
|
122
|
+
# app/application.rb
|
123
|
+
|
124
|
+
#預設值為 :mitake
|
125
|
+
config.action_message_texter.delivery_method = :mitake
|
126
|
+
```
|
127
|
+
基本上的設定方式都與ActionMailer相同,如果有不知道如何設定的,可以依照設定Mailer的經驗試試看喔
|
128
|
+
|
129
|
+
|
130
|
+
## Message Object
|
131
|
+
|
132
|
+
`Message` 物件相當於 `ActionMailer` 中的 `Message` 物件,是簡訊的本體,`Message` 物件包含了這些東西
|
133
|
+
- uuid: 簡訊的ID
|
134
|
+
- to: 收件者
|
135
|
+
- content: 簡訊內容
|
136
|
+
- response: 簡訊 Api 的回覆 (此部分可以參考[三竹簡訊API文件](https://sms.mitake.com.tw/common/header/download.jsp#))
|
137
|
+
|
132
138
|
|
133
139
|
## Callbacks
|
134
140
|
|
135
|
-
可以在Tester中加入 `before_action` 與 `after_action
|
141
|
+
可以在Tester中加入 `before_action` 與 `after_action`,在這兩個方法中可以取得 `message` 物件。
|
136
142
|
```ruby
|
137
143
|
class MyTexter
|
138
144
|
before_action :do_before
|
@@ -158,6 +164,10 @@ class MyTexter
|
|
158
164
|
class TexterObserver
|
159
165
|
def self.delivered_message(message)
|
160
166
|
# 請實作此方法
|
167
|
+
|
168
|
+
# 如傳送後儲存傳送結果
|
169
|
+
message_history = MessageHistory.find_by(uuid: message.uuid)
|
170
|
+
message_history.update(response_message: message.response.response_message)
|
161
171
|
end
|
162
172
|
end
|
163
173
|
|
@@ -178,6 +188,9 @@ end
|
|
178
188
|
class TexterInspection
|
179
189
|
def self.delivering_message(message)
|
180
190
|
# 請實作此方法
|
191
|
+
|
192
|
+
# 如測試站時加入測試站專屬訊息
|
193
|
+
message.content = "[test]#{message.content}" unless Rails.env.production
|
181
194
|
end
|
182
195
|
end
|
183
196
|
|
@@ -185,17 +198,15 @@ end
|
|
185
198
|
class MyTexter
|
186
199
|
self.register_inspection(TexterObserver)
|
187
200
|
|
188
|
-
#
|
201
|
+
# 您也可以註冊多組攔截器
|
189
202
|
self.register_inspection(OtherObserver)
|
190
203
|
end
|
191
204
|
```
|
192
|
-
## Test
|
193
205
|
|
194
|
-
|
206
|
+
## Test
|
195
207
|
|
208
|
+
小弟初次寫Gem 還不會寫測試 還請各位神人協助交流 感謝
|
196
209
|
|
197
|
-
## Contributing
|
198
|
-
Contribution directions go here.
|
199
210
|
|
200
211
|
## License
|
201
212
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -1,4 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActionMessageTexter
|
4
|
+
#
|
5
|
+
# 所有Texter的父類別
|
6
|
+
#
|
2
7
|
class Base < AbstractController::Base
|
3
8
|
include Rescuable
|
4
9
|
include DeliveryMethods
|
@@ -15,28 +20,51 @@ module ActionMessageTexter
|
|
15
20
|
}
|
16
21
|
|
17
22
|
class << self
|
23
|
+
##
|
24
|
+
# 註冊觀察者Observer
|
25
|
+
#
|
26
|
+
# +Observer+ 需要包含一個Class Method +delivered_message(message)+
|
27
|
+
# 此方法會在訊息傳送後被呼叫 並取得 +message+ instance
|
28
|
+
# @params [Class|Symbel|String] observer
|
18
29
|
def register_observer(observer)
|
19
30
|
Message.register_observer(observer_class_for(observer))
|
20
31
|
end
|
21
32
|
|
33
|
+
##
|
34
|
+
# 移除已被註冊的 Observer
|
35
|
+
# @params [Class|Symbel|String] observer
|
22
36
|
def unregister_observer(observer)
|
23
37
|
Message.unregister_observer(observer_class_for(observer))
|
24
38
|
end
|
25
39
|
|
40
|
+
##
|
41
|
+
# 註冊攔截器Interceptor
|
42
|
+
#
|
43
|
+
# +Interceptor+ 需要包含一個Class Method +delivering_message(message)+
|
44
|
+
# 此Interceptor被註冊後 將於簡訊寄出前觸發delivering_message並取得 +message+ instance
|
45
|
+
# @params [Class|Symbel|String] interceptor
|
26
46
|
def register_interceptor(interceptor)
|
27
47
|
Message.register_interceptor(observer_class_for(interceptor))
|
28
48
|
end
|
29
49
|
|
50
|
+
##
|
51
|
+
# 移除已被註冊的 Interceptor
|
52
|
+
# @params [Class|Symbel|String] observer
|
30
53
|
def unregister_interceptor(interceptor)
|
31
54
|
Message.unregister_observer(observer_class_for(interceptor))
|
32
55
|
end
|
33
56
|
|
57
|
+
##
|
58
|
+
# 可在 +Texter+ 中更改設定
|
59
|
+
#
|
60
|
+
# @param [Hash] value
|
34
61
|
def default(value)
|
35
62
|
self.default_params = default_params.merge(value).freeze if value
|
36
63
|
default_params
|
37
64
|
end
|
38
65
|
|
39
|
-
|
66
|
+
##
|
67
|
+
# 提供初始化時的 Configruation 使用
|
40
68
|
alias default_options= default
|
41
69
|
|
42
70
|
def texter_name
|
@@ -44,8 +72,9 @@ module ActionMessageTexter
|
|
44
72
|
end
|
45
73
|
attr_writer :texter_name
|
46
74
|
|
75
|
+
##
|
76
|
+
# 寄送訊息 發出Log Notification
|
47
77
|
def deliver_message(message)
|
48
|
-
# Notification
|
49
78
|
ActiveSupport::Notifications.instrument('deliver.action_message_texter') do |payload|
|
50
79
|
payload[:content] = message.content
|
51
80
|
payload[:to] = message.to
|
@@ -57,7 +86,6 @@ module ActionMessageTexter
|
|
57
86
|
|
58
87
|
private
|
59
88
|
|
60
|
-
# connect to MessageDelivery through method_missing
|
61
89
|
def method_missing(method_name, *args)
|
62
90
|
if action_methods.include?(method_name.to_s)
|
63
91
|
ActionMessageTexter::MessageDelivery.new(self, method_name, *args)
|
data/lib/sms_provider/base.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_message_texter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arthur Li
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mitake_api
|