实时推送Hive区块链信息到Telegram客户端(1)

in HIVE CN 中文社区4 years ago (edited)

Hive的网页客户端有一个消息通知功能(Notificaitons),每次登录Hive之后,就会看到和你账户相关的信息,比如:谁给你点赞,留言,转发,提到你等等信息。

image.png

但是,通过这种方式来查看各种通知的体验并不好。首先需要打开hive/peakd的页面,然后需要从菜单中选择查看通知才能够看到。而且,我们不可能总去刷Hive的网页吧。因此,更为便捷和方便的方式就是通过读取Hive区块链的信息,进行分析,找出自己感兴趣的信息,然后发送给自己的Telegram客户端/其他客户端。

要实现这个功能,需要考虑以下这些因素:

1)区块链信息的读取。这部分可以通过调用Hivepy库中的相关功能来实现。
2)定义你感兴趣的规则。比如:所有给你点赞的信息你都想得到通知,某位大鲸发的所有贴子你都想得到通知,任何和你账户相关的转账操作。等等等等。
3)配置一个Telegram机器人,一旦第二步中有符合条件的消息,发送给Telegram客户端。当然,这里非常灵活,你可以配置自己喜欢的客户端,比如Slack,Discord等等,道理都是类似的。

这篇文章主要来说一说如何检测Hive区块链的信息。

基本思路就是:首先获取当前区块,然后检测当前块中是否有自己感兴趣的信息,如果有的话,存储到一个变量中以便后面发送/处理。之后向前回溯上一个区块,再做同样的检测,如此进行下去,一直检测到指定数目的区块后停止。

这里就涉及一个基本的问题:究竟该向前回溯多少个区块呢?这就取决于你这个程序被执行的频率了。举个例子,如果你这个程序每三秒运行一次,而Hive区块链每三秒生成一个块(印象中Steem区块链是每三秒一块,Hive应该没有变),那么你的程序就不需要进行回溯了。如果你的程序每一分钟运行一次的话,那么你的程序需要回溯60/3 - 1次。

下面先看看如何读取当前区块:

from hive import Hive
from hive.blockchain import Blockchain

hblocks = Hive()
b       = Blockchain()
cb      = b.get_current_block()

print(cb)

运行上面程序,可以看到其输出结果为:

{'block_id': '0285ea9524d0cc9609991b4e15f52dfbae4663a5', 'signing_key': 'STM6ZHuTjdyS2vPLsaTyd1bsguGo28jsNcKKASFFTbckBWNKpFqdo', 'timestamp': '2020-04-07T11:07:54', 'extensions': [], 'transaction_merkle_root': 'ce9c9aea30054f975f69be2bbf852b1804c2eee1', 'transaction_ids': ['e2134800e3a04dd519d43242d33b938d446508b1', 'e5a979e66d092b5c4c2936bbdc68fa9287a244d9', 'c2cf361839450ab9f3a0176135edeb6d81b9bd90'], 'witness': 'blocktrades', 'witness_signature': '1fd20adda3b1f85adcb864661fef0d696a626b3eb9f4301b0381e69bd3aded604f4d77bfa409554ba4c0a42e43fe368ff992360039f2714e28f5b2fdb20356ad0d', 'previous': '0285ea94c52b1aeda5ed28063a7c64fb5b881f31', 'transactions': [{'block_num': 42330773, 'ref_block_prefix': 4010214059, 'transaction_num': 0, 'signatures': ['1f116bb1406e8479fd0644e7e8288510f4ea61d65bd1614e03fd67e204c0c2fa0f1a4ebb3d0632131005b0c44f5191b75bec768e96b08c6fc983e049c5d1c61864'], 'extensions': [], 'transaction_id': 'e2134800e3a04dd519d43242d33b938d446508b1', 'expiration': '2020-04-07T11:17:45', 'ref_block_num': 60034, 'operations': [['vote', {'weight': 10000, 'voter': 'news-steem', 'permlink': '20200407t104855949z-post', 'author': 'minimalpris'}]]}, {'block_num': 42330773, 'ref_block_prefix': 3413890307, 'transaction_num': 1, 'signatures': ['205471fe218b0b09b9a0a2ad860286b0a110ebb6d7ef3df6b624b976506054fc645f1cfe2e371a236cd79bb97d7a7a8682ffa00994d3fefd18bf62289ebc3a3e63'], 'extensions': [], 'transaction_id': 'e5a979e66d092b5c4c2936bbdc68fa9287a244d9', 'expiration': '2020-04-07T11:08:21', 'ref_block_num': 60051, 'operations': [['vote', {'weight': 4000, 'voter': 'retinox', 'permlink': 'it-was-all-about-green-a-restful-pleasant-color', 'author': 'priyanarc'}]]}, {'block_num': 42330773, 'ref_block_prefix': 3519458629, 'transaction_num': 2, 'signatures': ['204064b9720aa2aa1a8380e7b81bdb0f332a628608db586fd4a9abf1ddd03fa4137d7db804973b639032a0683f1108a1a1ed0b71c1ec4c8dd0df59fd8a6b192cfe'], 'extensions': [], 'transaction_id': 'c2cf361839450ab9f3a0176135edeb6d81b9bd90', 'expiration': '2020-04-07T11:17:48', 'ref_block_num': 60035, 'operations': [['vote', {'weight': 600, 'voter': 'ctime', 'permlink': 'it-was-all-about-green-a-restful-pleasant-color', 'author': 'priyanarc'}]]}]}

如果向前回溯一次,共读取两个块的代码如下:

NUMBER_OF_BLOCKS_TO_CHECK = 2 

for i in range(NUMBER_OF_BLOCKS_TO_CHECK):
    print("block id: %s" % cb["block_id"])
    print("==========================")
    for transaction in cb["transactions"]:
        for operation in transaction["operations"]:
            print(operation[0])                                                                                                        
    previousid = int(cb["previous"][:8], base=16)
    cb = b.hive.get_block(previousid)

其运行结果如下:

block id: 0285eb1b051eb25a0475e4867499303f48d7d77e
==========================
transfer
vote
custom_json
vote
vote
block id: 0285eb1aff2e699579bdd7136493f3a5ea4dedbf
==========================
vote
vote
vote
vote
vote
vote
vote
vote
vote
vote
vote
vote

至此,我们已经可以读取块中的信息,下一篇文章会说说怎么定义感兴趣的规则。

Sort:  

So it's like notification in steemit? I'm using peakd most of the time..

It's similar, but different. In Hive/Steemit, you pull information from their web application by accessing the notification page, here is talking about pushing notifications to your Telegram/Slack etc.