来了来了一步一步教如何写hive的机器人

Words
340
Reading
2 min
Listen
Play
2y

image.png

题记

  • Ai agent 最近不是AI agent。动不动就是造小人(小机器人)。既然hive活跃的社区。为什么不能造发帖机器人。接入AI不是很nice。说说干就干。

开工

  • 搭建开发环境

    • 首先需要安装nodejs。这个在网上搜就行。
    • 初始化项目
    npm init -y
    
    • 安装dhive包与dotenv本地环境包
    npm install @hiveio/dhive  dotenv
    
  • 构建文件

    hivebot/
    ├── src/
    │   ├── index.js       # 应用的入口文件
    ├── .env               # 环境变量文件
    ├── .gitignore         # Git 忽略文件配置
    ├── package.json       # Node.js 项目配置文件
    ├── package-lock.json  # 锁定依赖版本的文件
    ├── README.md          # 项目说明文档
    └── node_modules/      # 依赖库目录(由 npm 安装自动生成)
    
  • 构建账户配置信息

    • 在.env文件中,写入用户名与posting_key
    POSTING_KEY="5Kxxxxxxxxxxxxxxxxxxxxxxxxxx"
    HIVE_USERNAME="dxxxxxxxxxxxxxxx"
    
  • 代码
    在index.js文件中写入代码

import { Client, PrivateKey } from '@hiveio/dhive';
import dotenv from 'dotenv';
dotenv.config();



const post_key=PrivateKey.fromString(process.env.POSTING_KEY);

const postData = {
  author: process.env.HIVE_USERNAME, // 发帖者用户名
  title: "Hello world from Hivebot!", // 帖子标题
  body: "![from pixabay](https://cdn.pixabay.com/photo/2019/01/22/10/58/pixel-cells-3947912_1280.png)\n ## Hello World!\n This is an automated **bot** posting test.", // 帖子内容
  parent_author: "", // 空字符串表示为新帖
  parent_permlink: "hive",   // 分类标签,到具体板块
  permlink: 'hello-world-from-hivebot', // 帖子的永久链接,需唯一
  json_metadata:JSON.stringify( {                     // Hive 帖子的额外元数据
    tags: ["hive", "nodejs", "world","bot","test"], // 帖子的标签,用于分类和搜索
    app: "dhive-nodejs-hivebot"        // 表示发帖使用的应用程序,通常是客户端的名称
  })

};


const client = new Client(["https://api.hive.blog", "https://api.hivekings.com", "https://anyx.io", "https://api.openhive.network"]);

async function createPost(postData) {
  try {
    // 使用 client.broadcast.comment 进行发帖
    const result = await client.broadcast.comment(postData, post_key);
    console.log('Post successfully created:', result);
  } catch (error) {
    console.error('Error creating post:', error);
  }
}

createPost(postData);
  • 执行
    • 使用nodejs命令进行执行
    node ./src/index.js
    

image.png

End

  • 学废了么 其实在hive自动发帖还是很简单的 。本来是想在测试网上面进行测试。但是突然发现没找到什么公开测试网。官网提供的信息都是4年前的。这点还是挺遗憾的。
  • 资源匮乏 本来准备自己搭建一个测试网。结果发现需要4U 8G内存 500GB硬盘的机器才行。自己手里又没合适机器,只能看以后了。索性就是主网测试了。自己又没多余的账户。只能用本尊上。等以后hive便宜点。再充值一个账户出来。有不有大牛知道什么可用的hive测试网。
来了来了一步一步教如何写hive的机器人 | Ecency