Isso是一个使用Python和JavaScript编写的轻量级的开源评论系统,它旨在成为Disqus的替代产品。

其特点:

  • 使用 Markdown 评论

  • SQLite 后端

  • Disqus & WordPress 导入

  • 可配置的 JS 客户端,支持 Firefox, Safari, Chrome 和 IE10。

我之前写了Isso的文章,包括部署过程记录、使用经验总结,以及我fork的isso-cn版本,此版本主要增加了微信审核的功能,鉴于一些情况,现将微信审核部分代码单独拎出来,这样“按图索骥”,可以实现原isso微信审核功能。

以下细节可以参考我的这个commit实现。

前言

下载isso,可以用git克隆master最新代码,或者直接下载某个发行版,当然建议直接用pip安装。

注意:从源码安装还是挺复杂,需要构建静态资源。

不过本文这不是重点,可以查阅中文文档或Google...

NO.1 修改isso/__init__.py文件

1. 大概 75L

from isso.ext.notifications import Stdout, SMTP

改为

from isso.ext.notifications import Stdout, SMTP, Wechat

2. 大概 114L

elif backend in ("smtp""SMTP"):
    smtp_backend = True

后面添加

elif backend in ("wechat""WECHAT"):
    subscribers.append(Wechat(self))

NO.2 修改isso/ext/notifications.py文件

1. 大概 32L

添加 from isso.utils.http import curl

2. 大概 34L

原本if PY2K更改如下:

if PY2K:
    from thread import start_new_thread
    from urllib import urlencode
else:
    from _thread import start_new_thread
    from urllib.parse import urlencode

3. 在末尾增加Wechat审核类

以下内容也许非最新,建议查看这个文件

class Wechat(object):

    def __init__(self, isso):

        self.isso = isso
        self.conf = isso.conf.section("wechat")
        self.public_endpoint = isso.conf.get("server""public-endpoint") or local("host")
        self.admin_notify = any((n in ("wechat""WECHAT")) for n in isso.conf.getlist("general""notify"))
        self.moderated = isso.conf.getboolean("moderation""enabled")

    def __iter__(self):
        yield "comments.new:after-save", self.notify_new

    def format(self, thread, comment):

        md = []

        author = comment["author"] or "Anonymous"
        if comment["email"]:
            author += " <%s>" % comment["email"]

        md.append(author + " wrote:\n")
        md.append("\n")
        md.append(comment["text"] + "\n")
        md.append("\n")

        if comment["website"]:
            md.append("User's URL: %s\n" % comment["website"])
        md.append("IP address: %s\n" % comment["remote_addr"])

        view_uri = local("origin") + thread["uri"] + "#isso-%i" % comment["id"]
        if not self.moderated:
            md.append("Link to comment: %s\n" % view_uri)
            md.append("\n")
            md.append("---\n")

        uri = self.public_endpoint + "/id/%i" % comment["id"]
        key = self.isso.sign(comment["id"])
        delete_uri = uri + "/delete/" + key
        activate_uri = uri + "/activate/" + key
        if not self.moderated:
            md.append("Delete comment: %s\n" % delete_uri)
            if comment["mode"] == 2:
                md.append("Activate comment: %s\n" % activate_uri)

        rv = "\n".join(md)
        if self.moderated:
            return dict(rv=rv, v=view_uri, d=delete_uri, a=activate_uri)
        else:
            return rv

    def notify_new(self, thread, comment):
        if self.admin_notify:
            body = self.format(thread, comment)
            self.sendmsg(thread["title"], body)

    def sendmsg(self, title, body):
        if self.moderated:
            TA_view = body["v"]
            TA_delete = body["d"]
            TA_activate = body["a"]
            body = body["rv"]
        if PY2K:
            if title:
                title = title.encode("utf-8")
            body = body.encode("utf-8")
        if self.moderated:
            # Need to review comments, use the TalkAdmin interface.
            host = "http://sc.ftqq.com"
            path = "/webhook/%s?%s" % (self.conf.get("takey"), urlencode(dict(
                TA_action_on=1,
                TA_title=title,
                TA_content=body,
                TA_view=TA_view,
                TA_delete=TA_delete,
                TA_activate=TA_activate
            )))
        else:
            host = "https://sc.ftqq.com"
            path = "/%s.send?%s" % (self.conf.get("sckey"), urlencode(dict(
                text=title, desp=body
            )))
        with curl("POST", host, path, 5) as resp:
            if resp:
                try:
                    result = json.loads(resp.read())
                except (TypeError, ValueError):
                    logger.error("Illegal response structure, request error.")
                else:
                    if result.get("errno") != 0:
                        logger.warn(result)

NO.3 修改share/isso.conf文件

在末尾添加

[wechat]
# Isso can notify you on new comments via wechat. In the wechat notification,
# you also can moderate (=activate or delete) comments.

# ServerChan api credentials (similar to token).
# Enabled when moderation is not enabled.
sckey =

# TalkAdmin WebHook Key.
# Enabled when moderation is enabled.
takey =

这是微信审核所需要的配置信息,根据模式不同,选填sckey或takey,参考Wechat文档

END


·End·