应用
BaseRoBot
¶
Bases: object
BaseRoBot 是整个应用的核心对象,负责提供 handler 的维护,消息和事件的处理等核心功能。
:param logger: 用来输出 log 的 logger,如果是 None
,将使用 zgrobot.logger
:param config: 用来设置的 :class:zgrobot.config.Config
对象 \
.. note:: 对于下面的参数推荐使用 :class:~zgrobot.config.Config
进行设置, 并且以下参数均已 deprecated。
:param token: 微信公众号设置的 token (deprecated)
:param enable_session: 是否开启 session (deprecated)
:param session_storage: 用来储存 session 的对象,如果为 None
, 将使用 zgrobot.session.sqlitestorage.SQLiteStorage (deprecated)
:param app_id: 微信公众号设置的 app id (deprecated)
:param app_secret: 微信公众号设置的 app secret (deprecated)
:param encoding_aes_key: 用来加解密消息的 aes key (deprecated)
Source code in zgrobot/robot.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 |
|
__init__(token=None, logger=None, enable_session=None, session_storage=None, app_id=None, app_secret=None, encoding_aes_key=None, config=None, **kwargs)
¶
Source code in zgrobot/robot.py
add_filter(func, rules)
¶
为 BaseRoBot 添加一个 filter handler
。
:param func: 如果 rules 通过,则处理该消息的 handler。 :param rules: 一个 list,包含要匹配的字符串或者正则表达式。 :return: None
Source code in zgrobot/robot.py
add_handler(func, type='all')
¶
为 BaseRoBot 实例添加一个 handler。
:param func: 要作为 handler 的方法。 :param type: handler 的种类。 :return: None
Source code in zgrobot/robot.py
card_not_pass_check(f)
¶
card_pass_check(f)
¶
card_pay_order(f)
¶
card_sku_remind(f)
¶
为某个card_id的初始库存数大于200且当前库存小于等于100 (card_sku_remind_event)
事件添加一个 handler 方法的装饰器。
check_signature(timestamp, nonce, signature)
¶
根据时间戳和生成签名的字符串 (nonce) 检查签名。
:param timestamp: 时间戳
:param nonce: 生成签名的随机字符串
:param signature: 要检查的签名
:return: 如果签名合法将返回 True
,不合法将返回 False
Source code in zgrobot/robot.py
click(f)
¶
error_page(f)
¶
为 robot 指定 Signature 验证不通过时显示的错误页面。
Usage::
@robot.error_page
def make_error_page(url):
return "<h1>喵喵喵 %s 不是给麻瓜访问的快走开</h1>" % url
filter(*args)
¶
为文本 (text)
消息添加 handler 的简便方法。
使用 @filter("xxx")
, @filter(re.compile("xxx"))
或 @filter("xxx", "xxx2")
的形式为特定内容添加 handler。
Source code in zgrobot/robot.py
get_encrypted_reply(message)
¶
对一个指定的 ZgRoBot Message ,获取 handlers 处理后得到的 Reply。 如果可能,对该 Reply 进行加密。 返回 Reply Render 后的文本。
:param message: 一个 ZgRoBot Message 实例。 :return: reply (纯文本)
Source code in zgrobot/robot.py
get_reply(message)
¶
根据 message 的内容获取 Reply 对象。
:param message: 要处理的 message :return: 获取的 Reply 对象
Source code in zgrobot/robot.py
handler(f)
¶
image(f)
¶
key_click(key)
¶
为自定义菜单 (click)
事件添加 handler 的简便方法。
@key_click('KEYNAME') 用来为特定 key 的点击事件添加 handler 方法。
Source code in zgrobot/robot.py
link(f)
¶
location(f)
¶
location_event(f)
¶
location_select(f)
¶
parse_message(body, timestamp=None, nonce=None, msg_signature=None)
¶
解析获取到的 Raw XML ,如果需要的话进行解密,返回 ZgRoBot Message。 :param body: 微信服务器发来的请求中的 Body。 :return: ZgRoBot Message
Source code in zgrobot/robot.py
pic_photo_or_album(f)
¶
pic_sysphoto(f)
¶
pic_weixin(f)
¶
scan(f)
¶
scancode_push(f)
¶
scancode_waitmsg(f)
¶
shortvideo(f)
¶
submit_membercard_user_info(f)
¶
为用户通过一键激活的方式提交信息并点击激活或者用户修改会员卡信息 (submit_membercard_user_info_event)
事件添加一个 handler 方法的装饰器。
subscribe(f)
¶
templatesendjobfinish_event(f)
¶
text(f)
¶
unknown(f)
¶
unknown_event(f)
¶
unsubscribe(f)
¶
update_member_card(f)
¶
user_consume_card(f)
¶
user_del_card(f)
¶
user_enter_session_from_card(f)
¶
为用户卡券里点击查看公众号进入会话 (user_enter_session_from_card_event)
事件添加一个 handler 方法的装饰器。
user_get_card(f)
¶
user_gifting_card(f)
¶
user_pay_from_pay_cell(f)
¶
user_scan_product(f)
¶
user_scan_product_async(f)
¶
为地理位置信息异步推送 (user_scan_product_async_event)
事件添加一个 handler 方法的装饰器。
user_scan_product_enter_session(f)
¶
为进入公众号事件推送 (user_scan_product_enter_session_event)
事件添加一个 handler 方法的装饰器。
user_scan_product_verify_action(f)
¶
为商品审核结果推送 (user_scan_product_verify_action_event)
事件添加一个 handler 方法的装饰器。
user_view_card(f)
¶
video(f)
¶
view(f)
¶
ZgRoBot
¶
Bases: BaseRoBot
ZgRoBot 是一个继承自 BaseRoBot 的对象,在 BaseRoBot 的基础上使用了 bottle 框架, 提供接收微信服务器发来的请求的功能。
Source code in zgrobot/robot.py
run(server=None, host=None, port=None, enable_pretty_logging=True)
¶
运行 ZgRoBot。
:param server: 传递给 Bottle 框架 run 方法的参数,详情见 bottle 文档 <https://bottlepy.org/docs/dev/deployment.html#switching-the-server-backend>
_
:param host: 运行时绑定的主机地址
:param port: 运行时绑定的主机端口
:param enable_pretty_logging: 是否开启 log 的输出格式优化