Class: WeixinPam::PublicAccountReply

Inherits:
Object
  • Object
show all
Includes:
KeyEventMethods, WeixinRailsMiddleware::ReplyWeixinMessageHelper
Defined in:
lib/weixin_pam/public_account_reply.rb

Defined Under Namespace

Modules: KeyEventMethods Classes: KeyEventCallback

Constant Summary collapse

NO_CONTENT =
:no_content

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from KeyEventMethods

#find_key_event

Constructor Details

#initialize(public_account, message, keyword) ⇒ PublicAccountReply

Returns a new instance of PublicAccountReply.



47
48
49
50
51
52
# File 'lib/weixin_pam/public_account_reply.rb', line 47

def initialize(, message, keyword)
  @weixin_public_account = 
  @weixin_user_account = .user_accounts.find_or_create_by!(uid: message.FromUserName)
  @weixin_message = message
  @keyword = keyword
end

Instance Attribute Details

#keywordObject (readonly)

Returns the value of attribute keyword.



43
44
45
# File 'lib/weixin_pam/public_account_reply.rb', line 43

def keyword
  @keyword
end

#weixin_messageObject (readonly)

Returns the value of attribute weixin_message.



43
44
45
# File 'lib/weixin_pam/public_account_reply.rb', line 43

def weixin_message
  @weixin_message
end

#weixin_public_accountObject (readonly)

Returns the value of attribute weixin_public_account.



43
44
45
# File 'lib/weixin_pam/public_account_reply.rb', line 43

def 
  @weixin_public_account
end

#weixin_user_accountObject (readonly)

Returns the value of attribute weixin_user_account.



43
44
45
# File 'lib/weixin_pam/public_account_reply.rb', line 43

def 
  @weixin_user_account
end

Instance Method Details

#handle_click_eventObject

点击菜单拉取消息时的事件推送



159
160
161
162
163
164
165
166
# File 'lib/weixin_pam/public_account_reply.rb', line 159

def handle_click_event
  key_event = find_key_event(@keyword)
  if key_event
    key_event.execute(self)
  else
    reply_with_dev_message(reply_text_message("你点击了: #{@keyword}"))
  end
end

#handle_location_eventObject

上报地理位置事件



151
152
153
154
155
156
# File 'lib/weixin_pam/public_account_reply.rb', line 151

def handle_location_event # 上报地理位置事件
  @lat = @weixin_message.Latitude
  @lgt = @weixin_message.Longitude
  @precision = @weixin_message.Precision
  reply_with_dev_message(reply_text_message("Your Location: #{@lat}, #{@lgt}, #{@precision}"))
end

#handle_masssendjobfinish_eventObject

<xml> <ToUserName><![CDATA]></ToUserName> <FromUserName><![CDATA]></FromUserName> <CreateTime>1394524295</CreateTime> <MsgType><![CDATA]></MsgType> <Event><![CDATA]></Event> <MsgID>1988</MsgID> <Status><![CDATA]></Status> <TotalCount>100</TotalCount> <FilterCount>80</FilterCount> <SentCount>75</SentCount> <ErrorCount>5</ErrorCount> </xml>



208
209
210
211
# File 'lib/weixin_pam/public_account_reply.rb', line 208

def handle_masssendjobfinish_event
  Rails.logger.info("回调事件处理")
  NO_CONTENT
end

#handle_pic_photo_or_album_eventObject

弹出拍照或者相册发图的事件推送



180
181
182
# File 'lib/weixin_pam/public_account_reply.rb', line 180

def handle_pic_photo_or_album_event
  NO_CONTENT
end

#handle_pic_sysphoto_eventObject

弹出系统拍照发图



175
176
177
# File 'lib/weixin_pam/public_account_reply.rb', line 175

def handle_pic_sysphoto_event
  NO_CONTENT
end

#handle_scan_eventObject

扫描带参数二维码事件: 2. 用户已关注时的事件推送



147
148
149
# File 'lib/weixin_pam/public_account_reply.rb', line 147

def handle_scan_event
  reply_with_dev_message(reply_text_message("扫描带参数二维码事件: 2. 用户已关注时的事件推送, keyword: #{@keyword}"))
end

#handle_scancode_push_eventObject

扫码事件



185
186
187
# File 'lib/weixin_pam/public_account_reply.rb', line 185

def handle_scancode_push_event
  NO_CONTENT
end

#handle_subscribe_eventObject

关注公众账号



132
133
134
135
136
137
138
# File 'lib/weixin_pam/public_account_reply.rb', line 132

def handle_subscribe_event
  if @keyword.present?
    # 扫描带参数二维码事件: 1. 用户未关注时,进行关注后的事件推送
    return reply_with_dev_message(reply_text_message("扫描带参数二维码事件: 1. 用户未关注时,进行关注后的事件推送, keyword: #{@keyword}"))
  end
  reply_with_dev_message(reply_text_message("关注公众账号"))
end

#handle_templatesendjobfinish_eventObject



213
214
215
216
# File 'lib/weixin_pam/public_account_reply.rb', line 213

def handle_templatesendjobfinish_event
  Rails.logger.info("回调模板任务")
  NO_CONTENT
end

#handle_unsubscribe_eventObject

取消关注



141
142
143
144
# File 'lib/weixin_pam/public_account_reply.rb', line 141

def handle_unsubscribe_event
  Rails.logger.info("取消关注")
  NO_CONTENT
end

#handle_view_eventObject

点击菜单跳转链接时的事件推送



169
170
171
172
# File 'lib/weixin_pam/public_account_reply.rb', line 169

def handle_view_event
  Rails.logger.info("你点击了: #{@keyword}")
  NO_CONTENT
end

#replyObject



54
55
56
# File 'lib/weixin_pam/public_account_reply.rb', line 54

def reply
  send("response_#{@weixin_message.MsgType}_message")
end

#reply_with_dev_message(msg) ⇒ Object



218
219
220
# File 'lib/weixin_pam/public_account_reply.rb', line 218

def reply_with_dev_message(msg)
  Rails.env.development? ? msg : NO_CONTENT
end

#response_event_messageObject



120
121
122
123
124
125
126
127
128
129
# File 'lib/weixin_pam/public_account_reply.rb', line 120

def response_event_message
  event_type = @weixin_message.Event
  case event_type.downcase
  when 'unsubscribe'
    @weixin_public_account.user_accounts.where(uid: @weixin_message.FromUserName).limit(1).update_all(subscribed: false)
  when 'subscribe'
    @weixin_public_account.user_accounts.where(uid: @weixin_message.FromUserName).limit(1).update_all(subscribed: true)
  end
  send("handle_#{event_type.downcase}_event")
end

#response_image_messageObject

<PicUrl><![CDATA[this is a url]]></PicUrl> <MediaId><![CDATA]></MediaId>



76
77
78
79
80
# File 'lib/weixin_pam/public_account_reply.rb', line 76

def response_image_message
  @media_id = @weixin_message.MediaId # 可以调用多媒体文件下载接口拉取数据。
  @pic_url  = @weixin_message.PicUrl  # 也可以直接通过此链接下载图片, 建议使用carrierwave.
  reply_with_dev_message(generate_image(@media_id))
end

<Title><![CDATA]></Title> <Description><![CDATA]></Description> <Url><![CDATA]></Url>



85
86
87
88
89
90
# File 'lib/weixin_pam/public_account_reply.rb', line 85

def response_link_message
  @title = @weixin_message.Title
  @desc  = @weixin_message.Description
  @url   = @weixin_message.Url
  reply_with_dev_message(reply_text_message("回复链接信息"))
end

#response_location_messageObject

<Location_X>23.134521</Location_X> <Location_Y>113.358803</Location_Y> <Scale>20</Scale> <Label><![CDATA]></Label>



66
67
68
69
70
71
72
# File 'lib/weixin_pam/public_account_reply.rb', line 66

def response_location_message
  @lx    = @weixin_message.Location_X
  @ly    = @weixin_message.Location_Y
  @scale = @weixin_message.Scale
  @label = @weixin_message.Label
  reply_with_dev_message(reply_text_message("Your Location: #{@lx}, #{@ly}, #{@scale}, #{@label}"))
end

#response_shortvideo_messageObject

<MediaId><![CDATA]></MediaId> <ThumbMediaId><![CDATA]></ThumbMediaId>



113
114
115
116
117
118
# File 'lib/weixin_pam/public_account_reply.rb', line 113

def response_shortvideo_message
  @media_id = @weixin_message.MediaId # 可以调用多媒体文件下载接口拉取数据。
  # 视频消息缩略图的媒体id,可以调用多媒体文件下载接口拉取数据。
  @thumb_media_id = @weixin_message.ThumbMediaId
  reply_with_dev_message(reply_text_message("回复短视频信息"))
end

#response_text_messageObject



58
59
60
# File 'lib/weixin_pam/public_account_reply.rb', line 58

def response_text_message
  reply_with_dev_message(reply_text_message("Your Message: #{@keyword}"))
end

#response_video_messageObject

<MediaId><![CDATA]></MediaId> <ThumbMediaId><![CDATA]></ThumbMediaId>



104
105
106
107
108
109
# File 'lib/weixin_pam/public_account_reply.rb', line 104

def response_video_message
  @media_id = @weixin_message.MediaId # 可以调用多媒体文件下载接口拉取数据。
  # 视频消息缩略图的媒体id,可以调用多媒体文件下载接口拉取数据。
  @thumb_media_id = @weixin_message.ThumbMediaId
  reply_with_dev_message(reply_text_message("回复视频信息"))
end

#response_voice_messageObject

<MediaId><![CDATA]></MediaId> <Format><![CDATA]></Format>



94
95
96
97
98
99
100
# File 'lib/weixin_pam/public_account_reply.rb', line 94

def response_voice_message
  @media_id = @weixin_message.MediaId # 可以调用多媒体文件下载接口拉取数据。
  @format   = @weixin_message.Format
  # 如果开启了语音翻译功能,@keyword则为翻译的结果
  # reply_text_message("回复语音信息: #{@keyword}")
  reply_with_dev_message(generate_voice(@media_id))
end