LOADING...
LOADING...
LOADING...
当前位置: 玩币族首页 > 区块链资讯 > 如何使用 IPFS Cluster 构建 Twitter 机器人 (中)

如何使用 IPFS Cluster 构建 Twitter 机器人 (中)

2020-02-05 爱莉莎 来源:区块链网络


在本教程中,我们将:
1、简单介绍IPFS群集的意义和用例。
2、设置IPFS群集网络。
3、设置Twitter开发者帐户
4、构建一个Twitter机器人

创建一个Twitter开发者帐户

要构建一个Twitter机器人,首先,我们需要在Twitter上设置一个开发人员帐户。

1、前往 Twitter开发人员网站。点击“创建应用”按钮。



2、系统将提示您申请Twitter开发者帐户。



3、选择“制作机器人”选项,这是我们使用Twitter开发人员工具的主要原因,然后单击“下一步”。



4、在下一页,系统将询问您一些个人信息。填写,然后单击“下一步”。

5、在下一页中,您将被问到一些「如何使用Twitter API」相关的问题。说明您正在创建一个有趣的机器人即可。填写所有信息后,单击“下一步”。

6、在最后一页,确认您所提供的详细信息。

7、必须确认您的电子邮件帐户。等一会儿,您的帐户就将获得批准。

帐户获得批准后,就可以进入Twitter应用程序页面,然后单击“创建应用程序”,填写应用程序的详细信息,然后单击“创建”按钮。

恭喜你!您现在有一个Twitter应用程序啦????

接着,返回Twitter应用程序页面,可以在其中查看您的应用程序。单击“详细信息”按钮。


进入“密钥和令牌(keys and tokens)”,复制“API key”, “API secret key”, “Access token” & “Access token secret”。

建立Twitter Bot????

现在,让我们启动代码编辑器!

安装 Golang

我们将使用Golang来构建Twitter机器人。点击从此处下载Golang。

首先,您需要下载Golang。请按照说明安装Golang。安装Golang之后,我们准备开始编写Twitter机器人代码。

编写Twitter Bot代码??????????????????

我们将构建一个Twitter机器人,其工作方式如下:
1、您需要关注 Twitter bot 帐户,以便机器人可以从所有tweets中过滤你的推文,你可以“pin”, “unpin” 或 “add”的数据到IPFS群集网络,通过 @botHandle 一些特定的推文。

//To pin a CID with a name/label to IPFS Cluster Network @botHandle !pin <cid> <name>

//To unpin a CID from IPFS Cluster Network @botHandle !unpin <cid>

//To add a file(via URL) to IPFS Cluster Network @botHandle !add <url-to-single-file>

//To check out what the bot can do @botHandle !help

//Adding photos, memes, videos to IPFS Cluster Network Tweet to @botHandle attaching photos, memes or videos

2、 接下来,下载一些样板代码。
克隆 twitter-pinbot repo 分支:
$ git clone —single-branch —branch boilerplate https://github.com/simpleaswater/twitter-pinbot

$ git clone --single-branch --branch boilerplate https://github.com/simpleaswater/twitter-pinbot

3、在config.json文件中进行一些更改。

{ "twitter_name": "@botHandle", "twitter_id": "@botHandle", "consumer_key": "API key", "consumer_secret": "API secret key", "access_key": "Access token", "access_secret": "Access token secret", "cluster_peer_addr": "cluster peer multiaddress", "cluster_username": "", "cluster_password": "" }

您需要添加@botHandle,以及从twitter开发人员账户获得的API key。

然后将“集群对等多地址(cluster peer multiaddress)”替换为:

/ip4/127.0.0.1/tcp/9696/ipfs/QmcrQZ6RJdpYuGvZqD5QEHAv6qX4BrQLJLQPQUrTrzdcgm

如果仔细观察,您会发现这与我们在更改IPFS群集配置文件时添加的多地址相同。

我们这样做是为了与上面创建的IPFS群集设置进行交互
您可以将“ cluster_username”和“ cluster_password”留空。

4、现在,就像完成config.json文件一样,让我们开始在main.go上工作。

package main import ( "regexp" ) // ConfigFile is the path of the default configuration file var ConfigFile = "config.json" // Gateway var IPFSGateway = "https://ipfs.io" const twittercom = "twitter.com" type Action string // Variables containing the different available actions var ( // (spaces)(action)whitespaces(arguments) actionRegexp = regexp.MustCompile(`^\s*([[:graph:]]+)\s+(.+)`) // (cid)whitespaces(name with whitespaces). [:graph:] does not // match line breaks or spaces. pinRegexp = regexp.MustCompile(`([[:graph:]]+)\s+([[:graph:]\s]+)`) PinAction Action = "!pin" UnpinAction Action = "!unpin" AddAction Action = "!add" HelpAction Action = "!help" ) func main() { //Let's code ? }

该main.go文件中包含一些常量。

首先,添加从config.json读取和管理数据的方法。

// Config is the configuration format for the Twitter Pinbot type Config struct { TwitterID string `json:"twitter_id"` TwitterName string `json:"twitter_name"` AccessKey string `json:"access_key"` AccessSecret string `json:"access_secret"` ConsumerKey string `json:"consumer_key"` ConsumerSecret string `json:"consumer_secret"` ClusterPeerAddr string `json:"cluster_peer_addr"` ClusterUsername string `json:"cluster_username"` ClusterPassword string `json:"cluster_password"` } // Function to read JSON config file func readConfig(path string) *Config { cfg := &Config{} cfgFile, err := ioutil.ReadFile(path) if err != nil { log.Fatal(err) } err = json.Unmarshal(cfgFile, &cfg) if err != nil { log.Fatal(err) } return cfg }

Config 结构管理config.json文件中不同的键值对。还有,我们有 readConfig(path string)函数,它解析 config.json中的 JSON。

接下来,创建 Bot 结构体,用于管理twitter pinbot的不同功能。

// Bot is a twitter bot which reads a user's timeline // and performs actions on IPFS Cluster if the tweets // match, i.e. a tweet with: "@botHandle !pin <cid> <name>" // will pin something. The users with pin permissions are // those who follow the bot. Retweets by users who follow // the bot should also work. The bot will answer // the tweet with a result. type Bot struct { ctx context.Context cancel context.CancelFunc name string id string twClient *twitter.Client clusterClient client.Client followedBy sync.Map die chan struct{} }

您还必须导入以下两个模块:

import ( "github.com/dghubble/go-twitter/twitter" "github.com/ipfs/ipfs-cluster/api/rest/client" )

原文链接:https://hackernoon.com/how-to-build-a-twitter-bot-with-ipfs-cluster-b62b3a2j

今天先到这里,明天继续。



系列课程1《爱莉莎科普区块链》75课 !点击!

系列课程2《一起开发 EOS DAPP系列》 点击!
系列课程3《区块链工具百宝箱》 点击!

微信公众号:竹三七


—-

编译者/作者:爱莉莎

玩币族申明:玩币族作为开放的资讯翻译/分享平台,所提供的所有资讯仅代表作者个人观点,与玩币族平台立场无关,且不构成任何投资理财建议。文章版权归原作者所有。

LOADING...
LOADING...