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

in #cn6 years ago (edited)

你是否在STEEM上看到很多只需在回复输入命令就可以召唤来的机器人?

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

image.png

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

实现只需要3步:

  • 实时查看所有发布到STEEM链上的操作
  • 查看是否有指定的关键词
  • 如果有,自动回复

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

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

steem.api.streamTransactions(mode, function(err, result) {
    console.log(err, result);
});

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

function start() {
    steem.api.streamTransactions("head", function(err, result) {
        if (result && !err) {
          console.log(result);//显示结果
        } 
    });
}

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

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

具体实现如下:

    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');
    }
    }

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

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

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

代码如下:

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

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

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

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

start();

function start() {
    steem.api.streamTransactions("head", function(err, result) {
        if (result && !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);
        });
}

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

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

发挥自己的想象力吧~

Sort:  

你好鸭,村长!
@john371911赠送1枚SHOP币给你!

目前你总共有: 41枚SHOP币

查看或者交易 SHOP币 请到 steem-engine.com.

无聊吗?跟我猜拳吧! **石头,剪刀,布~**

好难,看不懂

Posted using Partiko Android

你好鸭,瓜叔!

@dappcoder给您叫了一份外卖!

@honoru 米高 迎着大雪 开着宝马 给您送来
蔬菜培根卷

吃饱了吗?跟我猜拳吧! 石头,剪刀,布~

如果您对我的服务满意,请不要吝啬您的点赞~
@onepagex

要不要改一下規則,改成用"!shop*10" 如果持有足夠的shop幣,可以一次召喚Shop給10枚shop幣,給原本十倍的讚?

准备去掉点赞了,因为hf后,给评论点赞变得很鸡肋。一次给多个shop也想过,这样方便的赠币者,但是失去了原来shop币的目的,就是多和不同的人交流

Posted using Partiko iOS

厲害呀村長,中秋節快樂😊

Posted using Partiko Android

三妞中秋快乐~

感谢代理1008.96SP 给team-cn! 由于你使用CN作为你的首标签,额外获得2%点赞! 你的帖子获得team-cn 27% 点赞!(如果不想看到这个回复,请回复“取消”)

Du erhieltest aufgrund deiner LanaCharleenToken Miner ein Upvote von @sebescen81 alt-Text
Vielen lieben Dank für euren Support. Der Account meiner Tochter wächst und gedeiht.