Class: WeChat::Bot::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/wechat/bot/core.rb

Overview

机器人的核心类

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Core

Returns a new instance of Core.



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/wechat/bot/core.rb', line 50

def initialize(&block)
  # defaults_logger
  @logger = Logger.new(STDOUT, self)
  @config = Configuration.new
  @handlers = HandlerList.new
  @callback = Callback.new(self)

  @client = Client.new(self)
  @profile = Contact.new(self)
  @contact_list = ContactList.new(self)

  instance_eval(&block) if block_given?
end

Instance Attribute Details

#callbackCallback (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



48
49
50
# File 'lib/wechat/bot/core.rb', line 48

def callback
  @callback
end

#clientClient (readonly)

微信 API 客户端

Returns:



25
26
27
# File 'lib/wechat/bot/core.rb', line 25

def client
  @client
end

#configConfiguration (readonly)

Returns:



44
45
46
# File 'lib/wechat/bot/core.rb', line 44

def config
  @config
end

#contact_listContactList (readonly)

联系人列表

Returns:



35
36
37
# File 'lib/wechat/bot/core.rb', line 35

def contact_list
  @contact_list
end

#handlersHandlerList (readonly)

Returns:



41
42
43
# File 'lib/wechat/bot/core.rb', line 41

def handlers
  @handlers
end

#loggerLogger

Returns:



38
39
40
# File 'lib/wechat/bot/core.rb', line 38

def logger
  @logger
end

#profileContact (readonly)

当前登录用户信息

Returns:



30
31
32
# File 'lib/wechat/bot/core.rb', line 30

def profile
  @profile
end

Instance Method Details

#configure {|config| ... } ⇒ void

This method returns an undefined value.

用于设置 WeChat::Bot 的配置 默认无需配置,需要定制化 yield #config 进行配置

Yield Parameters:

  • config (Struct)


98
99
100
# File 'lib/wechat/bot/core.rb', line 98

def configure
  yield @config
end

#on(event, regexp = %r{}, *args) {|| ... } ⇒ Handler

消息触发器

Parameters:

  • event (String, Symbol, Integer)
  • regexp (Regexp, Pattern, String) (defaults to: %r{})
  • args (Array<Object>)

Yield Parameters:

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/wechat/bot/core.rb', line 71

def on(event, regexp = %r{}, *args, &block)
  event = event.to_s.to_sym

  pattern = case regexp
            when Pattern
              regexp
            when Regexp
              Pattern.new(nil, regexp, nil)
            else
              if event == :ctcp
                Pattern.generate(:ctcp, regexp)
              else
                Pattern.new(/^/, /#{Regexp.escape(regexp.to_s)}/, /$/)
              end
            end

  handler = Handler.new(self, event, pattern, {args: args, execute_in_callback: true}, &block)
  @handlers.register(handler)

  handler
end

#startvoid

This method returns an undefined value.

运行机器人



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
# File 'lib/wechat/bot/core.rb', line 105

def start
  @client.
  
  @client.contacts

  @contact_list.each do |c|
    @logger.debug "Contact: #{c}"
  end

  @client.start_runloop_thread

  while true
    break unless @client.logged? || @client.alive?
    sleep 1
  end
rescue Interrupt => e
  message = "你使用 Ctrl + C 终止了运行"
  @logger.warn(message)
  @client.send_text(@config.fireman, "[告警] 意外下线\n#{message}") if @client.logged? && @client.alive?
rescue Exception => e
  message = e.message
  @logger.fatal(e)
  @client.send_text(@config.fireman, "[告警] 意外下线\n#{message}\n#{e.backtrace.join("\n")}") if @client.logged? && @client.alive?
ensure
  @client.logout if @client.logged? && @client.alive?
end