Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vercel): Telegram notification support #83

Merged
merged 9 commits into from
Dec 21, 2020
Merged

feat(vercel): Telegram notification support #83

merged 9 commits into from
Dec 21, 2020

Conversation

leirock
Copy link
Contributor

@leirock leirock commented Dec 21, 2020

实现了 Telegram 机器人的评论通知功能,文档那边也相应修改了。

不过实现的过程个人感觉十分简陋。评论通知这里读取的变量 {{self.comment | safe}} 已经被渲染成 HTML 了,如果直接发送该段内容,Telegram 有可能不会接受而报错,因为其只支持部分 HTML 标签

Telegram 机器人是可以接受 MarkDown 内容 的,所以我这里干脆把发送消息的内容写成了 MarkDown,然而因为变量 {{self.comment | safe}} 依然是 HTML,我这里只能做了一点简单的处理:把 <p>、换行符、表情这几个剔除,然后让评论内容变成代码格式。这样发送消息时候就不会报错,消息内容也会简洁一些。

当然最好的方式是,直接把用户在评论框中的 MarkDown 评论传递给 Telegram 机器人,这样就不会出现上面这样评论经过了 MarkDown-HTML-MarkDown 几个阶段转换的过程了。技术有限,我是搞不定了😂😂 如果可以的话可以进一步改进~

评论通知效果如下:

image

@CCKNBC
Copy link

CCKNBC commented Dec 21, 2020

喜欢

@lizheming
Copy link
Collaborator

Markdown 的话可以把这里修改一下,改成 await this.hook('postSave',{...resp, rawComment: comment}, pComment) 就可以了。你要不改改试试?

https://github.com/lizheming/waline/blob/master/packages/server/src/controller/comment.js#L230

@leirock
Copy link
Contributor Author

leirock commented Dec 21, 2020

Markdown 的话可以把这里修改一下,改成 await this.hook('postSave',{...resp, rawComment: comment}, pComment) 就可以了。你要不改改试试?

https://github.com/lizheming/waline/blob/master/packages/server/src/controller/comment.js#L230

其实作为代码小白,并不是很懂,前面那些都是一点点探索出来的 😂 话说,这样修改以后通知这边 Telegram 要引用哪个变量作为 Markdown 的评论呢, rawComment ? 这样原本保存的 HTML 评论内容应该不变喽?

@lizheming
Copy link
Collaborator

嗯,用 self.rawComment 即可

@leirock
Copy link
Contributor Author

leirock commented Dec 21, 2020

按照你说的把那行改了,然后 Telegram 的评论内容设置如下,但是发过来的通知没有内容

    const contentTG = `
💬 *[{{site.name}}]({{site.url}}) 上有新评论啦*

*{{self.nick}}* 回复说:

{{self.rawComment | safe}}

您可以点击[查看回复的完整內容]({{site.postUrl}})`;

image

@lizheming lizheming merged commit e95470f into walinejs:master Dec 21, 2020
telegramNotify = await this.telegram({contentTG}, comment, parent);
}

if(!isAuthorComment && !isReplyAuthor && (think.isEmpty(wechatNotify) || think.isEmpty(telegramNotify)) ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有个问题,为啥我这里已经设置了,但是刚才我部署后,还是会出现 Telegram 和邮件都收到通知的情况?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你收到的是新评论通知,还是子评论回复的邮件通知?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最新评论通知,就是非博主的邮箱地址用户评论会同时收到通知

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那应该是你这条件写的有问题了,应该是 && 不是 ||,微信和 Telegram 通知都没有设置的时候才需要,现在你是其一不设置就会走邮件通知。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

啊,好像是弄混了 😂😂 你改一下吧

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,等下个 feat 我带上去吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外之前提到那个 self.rawComment 没搞定,你有空可以再看看,不过目前也不影响 Telegram 通知

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我已经改了

@leirock
Copy link
Contributor Author

leirock commented Dec 21, 2020

有问题,用了你修改后的版本,提交评论会弹窗报错 Assignment to constant variable.,然后无法收到评论通知。

@lizheming
Copy link
Collaborator

@waline/vercel@0.15.1 发了

@leirock
Copy link
Contributor Author

leirock commented Dec 21, 2020

docker 这边似乎没更新? npm 那边有看到更新的~

@lizheming
Copy link
Collaborator

Docker 要等 CI 自动构建,等半个小时再看看吧

@leirock
Copy link
Contributor Author

leirock commented Dec 21, 2020

似乎你 GitHub 上 @waline/vercel 的版本号也没改到 @0.15.1 呀

@eallion
Copy link
Contributor

eallion commented Apr 20, 2021

@lei2rock https://github.com/easychen/telechan 可以看看这个喔,用这个 API ,CloudBase 版本也可以用 Telegram 通知了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants