Class: WeChat::Bot::Core
- Inherits:
-
Object
- Object
- WeChat::Bot::Core
- Defined in:
- lib/wechat/bot/core.rb
Overview
机器人的核心类
Instance Attribute Summary collapse
- #callback ⇒ Callback readonly private
-
#client ⇒ Client
readonly
微信 API 客户端.
- #config ⇒ Configuration readonly
-
#contact_list ⇒ ContactList
readonly
联系人列表.
- #handlers ⇒ HandlerList readonly
- #logger ⇒ Logger
-
#profile ⇒ Contact
readonly
当前登录用户信息.
Instance Method Summary collapse
-
#configure {|config| ... } ⇒ void
用于设置 WeChat::Bot 的配置 默认无需配置,需要定制化 yield #config 进行配置.
-
#initialize(&block) ⇒ Core
constructor
A new instance of Core.
-
#on(event, regexp = %r{}, *args) {|| ... } ⇒ Handler
消息触发器.
-
#start ⇒ void
运行机器人.
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
#callback ⇒ Callback (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.
48 49 50 |
# File 'lib/wechat/bot/core.rb', line 48 def callback @callback end |
#client ⇒ Client (readonly)
微信 API 客户端
25 26 27 |
# File 'lib/wechat/bot/core.rb', line 25 def client @client end |
#config ⇒ Configuration (readonly)
44 45 46 |
# File 'lib/wechat/bot/core.rb', line 44 def config @config end |
#contact_list ⇒ ContactList (readonly)
联系人列表
35 36 37 |
# File 'lib/wechat/bot/core.rb', line 35 def contact_list @contact_list end |
#handlers ⇒ HandlerList (readonly)
41 42 43 |
# File 'lib/wechat/bot/core.rb', line 41 def handlers @handlers end |
#profile ⇒ Contact (readonly)
当前登录用户信息
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 进行配置
98 99 100 |
# File 'lib/wechat/bot/core.rb', line 98 def configure yield @config end |
#on(event, regexp = %r{}, *args) {|| ... } ⇒ Handler
消息触发器
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 |
#start ⇒ void
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.login @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 = "你使用 Ctrl + C 终止了运行" @logger.warn() @client.send_text(@config.fireman, "[告警] 意外下线\n#{}") if @client.logged? && @client.alive? rescue Exception => e = e. @logger.fatal(e) @client.send_text(@config.fireman, "[告警] 意外下线\n#{}\n#{e.backtrace.join("\n")}") if @client.logged? && @client.alive? ensure @client.logout if @client.logged? && @client.alive? end |