怎么用JS写个召唤机器人? by ericet

View this thread on steempeak.com
· @ericet · (edited)
$2.74
怎么用JS写个召唤机器人?
你是否在STEEM上看到很多只需在回复输入命令就可以召唤来的机器人?

比如小卖部@teamcn-shop的机器人,只需输入!shop就可以召唤来送币或者送美食。

<img src="https://files.steempeak.com/file/steempeak/ericet/yYg6Bsnu-image.png" alt="image.png" /><br/>

看起来是不是很神奇?实现这种召唤机器人其实很简单。

实现只需要3步:
* 实时查看所有发布到STEEM链上的操作
* 查看是否有指定的关键词
* 如果有,自动回复

我们先来第一步,实时查看所有发布到STEEM链上的操作

steemjs 提供了一个function来获取实时发布到STEEM链上的操作:

<pre><code class="">steem.api.streamTransactions(mode, function(err, result) {
    console.log(err, result);
});
</code></pre>

利用这个function我们就可以开始获取实时发布到STEEM上的操作,代码如下:

<pre><code class="">function start() {
    steem.api.streamTransactions("head", function(err, result) {
        if (result &amp;&amp; !err) {
          console.log(result);//显示结果
        } 
    });
}
</code></pre>

运行上面的代码会显示所有发布到STEEM上的操作,比如转账,点赞,发帖,回复等。

但是我们只需要看回复的操作,所以我们需要过滤掉其他的操作,并且只看有发布指定关键词的回复。

具体实现如下:

<pre><code class="">    let txType = result.operations[0][0]; //获取操作类别
    let txData = result.operations[0][1];//获取操作内容
    if (txType == "comment") {//如果操作是回复
        let commentBody = txData.body;
    let mention= '!hello';
        if(commentBody.includes(mention)){
        console.log('hello');
    }
    }
</code></pre>

找到关键词后,你就可以让机器人自动回复了。
Steemjs提供broadcast function来实现回复/发帖:

<pre><code class="">steem.broadcast.comment (
    private_posting_wif,  //发帖密钥
    parent_author,        // 如果是发帖留空
    parent_permlink,      // 主标签
    author,               // 作者
    permlink,             // permlink
    title,                // 标题
    body,                 // 内容
    json_metadata         // json
)
</code></pre>

最后一步是,当有人回复了某个关键词后,机器人自动回复“HELLO”

代码如下:

<pre><code class="">    steem.broadcast.comment(
        "发帖密钥",
        txData.parent_author,
        txData.parent_permlink,
        "steem id",
        txData.permlink,
        "",
        "HELLO",
        '{"app":"bot"}',
        function(err, result) {
            console.log(err, result);
        });
</code></pre>

好了,一个只会回复HELLO的机器人就这样实现了。

把上面的三步骤合在一起的完整代码:

~~~
const steem = require("steem");
const steemid = 'steemid';
const postingKey = 'postingKey';

start();

function start() {
    steem.api.streamTransactions("head", function(err, result) {
        if (result &amp;&amp; !err) {
            let txType = result.operations[0][0];
            let txData = result.operations[0][1];
            let includesMention = checkContentForMention(txType, txData);
            if (includesMention) {
               reply(txData);
            }
        } else {
            console.log("Error found", err);
                start();
        }
    });
}

function checkContentForMention(txType, txData) {
    if (txType == "comment") {
        let commentBody = txData.body;
        let mention= '!hello';
        return (commentBody.includes(mention));
    }
}

function reply(txData) {
    steem.broadcast.comment(
        postingKey,
        txData.parent_author,
        txData.parent_permlink,
        steemid,
        txData.permlink,
        "",
        "text here",
        '{"app":"bot"}',
        function(err, result) {
            console.log(err, result);
        });
}
~~~




是不是很简单?好好利用上面的代码,可以玩出不同的花样,比如写个剪刀石头布机器人,比大小机器人,等等。

只有没想到,没有做不到~

发挥自己的想象力吧~
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 138 others
properties (23)
post_id79,949,405
authorericet
permlinkjs-nddsi4obip
categorycn
json_metadata{"community":"steempress","app":"busy\/2.5.4","tags":["cn","cn-reader","whalepower","cn-stem","steemstem","cn-programming","build-it","steemleo","palnet","zzan","mediaofficials","actnearn","marlians","neoxian","lassecash","upfundme","sct","sct-cn","sct-freeboard","busy"],"canonical_url":"https:\/\/tecirechen.000webhostapp.com\/2019\/09\/%e6%80%8e%e4%b9%88%e7%94%a8js%e5%86%99%e4%b8%aa%e5%8f%ac%e5%94%a4%e6%9c%ba%e5%99%a8%e4%ba%ba%ef%bc%9f","users":["teamcn-shop"],"image":["https:\/\/files.steempeak.com\/file\/steempeak\/ericet\/yYg6Bsnu-image.png"],"format":"markdown"}
created2019-09-12 18:28:45
last_update2019-09-12 18:35:09
depth0
children10
net_rshares9,025,731,893,632
last_payout2019-09-19 18:28:45
cashout_time1969-12-31 23:59:59
total_payout_value1.270 SBD
curator_payout_value1.468 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length3,161
author_reputation363,078,054,770,101
root_title怎么用JS写个召唤机器人?
beneficiaries
0.
accountsteempress
weight1,500
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (202)
@team-cn ·
感谢代理1008.96SP 给team-cn! 由于你使用CN作为你的首标签,额外获得2%点赞! 你的帖子获得team-cn 27% 点赞!(如果不想看到这个回复,请回复“取消”)
properties (22)
post_id79,949,496
authorteam-cn
permlinkre-ericet-js-nddsi4obip-20190912t183348653z
categorycn
json_metadata"{"app":"NBC bot"}"
created2019-09-12 18:33:48
last_update2019-09-12 18:33:48
depth1
children0
net_rshares0
last_payout2019-09-19 18:33:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length90
author_reputation41,051,906,712,730
root_title怎么用JS写个召唤机器人?
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@aellly ·
好难,看不懂

Posted using [Partiko Android](https://partiko.app/referral/aellly)
👍  ,
properties (23)
post_id79,949,607
authoraellly
permlinkaellly-re-ericet-js-nddsi4obip-20190912t183938963z
categorycn
json_metadata{"app":"partiko","client":"android"}
created2019-09-12 18:39:39
last_update2019-09-12 18:39:39
depth1
children2
net_rshares5,703,198,992
last_payout2019-09-19 18:39:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length75
author_reputation394,053,832,336,323
root_title怎么用JS写个召唤机器人?
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (2)
@dappcoder ·
!shop
👍  
properties (23)
post_id79,958,575
authordappcoder
permlinkpxr033
categorycn
json_metadata{"tags":["cn"],"app":"steemit\/0.1"}
created2019-09-13 02:25:51
last_update2019-09-13 02:25:51
depth2
children0
net_rshares512,033,440
last_payout2019-09-20 02:25:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length5
author_reputation71,889,699,236,580
root_title怎么用JS写个召唤机器人?
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@teamcn-shop ·
你好鸭,瓜叔!

@dappcoder给您叫了一份外卖!

由 @honoru 米高 迎着大雪 开着宝马 给您送来
**蔬菜培根卷** <br> ![](https://cdn.steemitimages.com/DQmaaPPNjLFnmdJEsMLZXwodwjxwfD2bKcZmjjRaCB4gKZx/image.png)
吃饱了吗?跟我猜拳吧! **石头,剪刀,布~**

如果您对我的服务满意,请不要吝啬您的点赞~
@onepagex
👍  , , ,
properties (23)
post_id79,958,576
authorteamcn-shop
permlinkpxr033
categorycn
json_metadata"{"app":"teamcn-shop bot\/1.0"}"
created2019-09-13 02:26:00
last_update2019-09-13 02:26:00
depth2
children0
net_rshares38,704,136,417
last_payout2019-09-20 02:26:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length224
author_reputation65,900,527,192,856
root_title怎么用JS写个召唤机器人?
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (4)
@sebescen81 ·
Du erhieltest aufgrund deiner [LanaCharleenToken Miner](https://steem-engine.com/?p=market&t=LCTM) ein Upvote von @sebescen81  <a href="https://steem-engine.com/?p=market&t=LCTM"><img src="https://files.steempeak.com/file/steempeak/sebescen81/MrJu8F3O-lct-225.jpg" width="xyz" height="xyz" alt="alt-Text"></a>
Vielen lieben Dank für euren Support. Der Account meiner Tochter wächst und gedeiht.
properties (22)
post_id79,949,713
authorsebescen81
permlinkre-js-nddsi4obip-20190912t184445z
categorycn
json_metadata{"app":"rewarding\/0.1.5"}
created2019-09-12 18:44:51
last_update2019-09-12 18:44:51
depth1
children0
net_rshares0
last_payout2019-09-19 18:44:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length395
author_reputation26,777,977,837,039
root_title怎么用JS写个召唤机器人?
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@sunai ·
厲害呀村長,中秋節快樂😊

Posted using [Partiko Android](https://partiko.app/referral/sunai)
👍  
properties (23)
post_id79,949,926
authorsunai
permlinksunai-re-ericet-js-nddsi4obip-20190912t185504064z
categorycn
json_metadata{"app":"partiko","client":"android"}
created2019-09-12 18:55:03
last_update2019-09-12 18:55:03
depth1
children1
net_rshares50,037,851
last_payout2019-09-19 18:55:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length80
author_reputation82,751,862,601,752
root_title怎么用JS写个召唤机器人?
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@ericet ·
三妞中秋快乐~
👍  
properties (23)
post_id79,959,080
authorericet
permlinkre-sunai-pxr1i0
categorycn
json_metadata{"tags":["cn"],"app":"steempeak\/1.16.1"}
created2019-09-13 02:56:24
last_update2019-09-13 02:56:24
depth2
children0
net_rshares550,422,048
last_payout2019-09-20 02:56:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length7
author_reputation363,078,054,770,101
root_title怎么用JS写个召唤机器人?
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@john371911 ·
要不要改一下規則,改成用"!shop*10" 如果持有足夠的shop幣,可以一次召喚Shop給10枚shop幣,給原本十倍的讚?
👍  
properties (23)
post_id79,959,082
authorjohn371911
permlinkre-ericet-pxr1i0
categorycn
json_metadata{"tags":["cn"],"app":"steempeak\/1.16.1"}
created2019-09-13 02:56:24
last_update2019-09-13 02:56:24
depth1
children1
net_rshares538,633,982
last_payout2019-09-20 02:56:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length64
author_reputation66,408,278,506,348
root_title怎么用JS写个召唤机器人?
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@ericet ·
$0.04
准备去掉点赞了,因为hf后,给评论点赞变得很鸡肋。一次给多个shop也想过,这样方便的赠币者,但是失去了原来shop币的目的,就是多和不同的人交流

Posted using [Partiko iOS](https://partiko.app/referral/ericet)
👍  ,
properties (23)
post_id79,959,152
authorericet
permlinkericet-re-john371911-re-ericet-pxr1i0-20190913t030027955z
categorycn
json_metadata{"app":"partiko","client":"ios"}
created2019-09-13 03:00:27
last_update2019-09-13 03:00:27
depth2
children0
net_rshares223,245,427,958
last_payout2019-09-20 03:00:27
cashout_time1969-12-31 23:59:59
total_payout_value0.024 SBD
curator_payout_value0.013 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length138
author_reputation363,078,054,770,101
root_title怎么用JS写个召唤机器人?
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (2)
@teamcn-shop ·
你好鸭,村长!
@john371911赠送1枚SHOP币给你!
![](https://ipfs.busy.org/ipfs/QmXmMvqUag8xsUmrwWzYiqwBojWR4ijaGet8xS9m3znLtJ)

目前你总共有: 41枚SHOP币
<p><sup>查看或者交易 <code>SHOP币</code> 请到 <a href="https://steem-engine.com/?p=market&t=SHOP">steem-engine.com</a>.</sup></p>
无聊吗?跟我猜拳吧! **石头,剪刀,布~**
👍  , ,
properties (23)
post_id79,959,088
authorteamcn-shop
permlinkre-ericet-pxr1i0
categorycn
json_metadata"{"app":"teamcn-shop bot\/1.0"}"
created2019-09-13 02:56:33
last_update2019-09-13 02:56:33
depth1
children0
net_rshares38,993,162,420
last_payout2019-09-20 02:56:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length273
author_reputation65,900,527,192,856
root_title怎么用JS写个召唤机器人?
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (3)