golang微信机器人
by Kofo Okesola
由Kofo Okesola
如何使用Golang从头开始创建Twitter机器人 How to create a Twitter bot from scratch with GolangSo a little background: I recently picked up Golang and decided to create a Twitter bot as a side project. Then came the problem. There is little to no documentation on using the Twitter API with Golang ?(particularly the oauth1 and CRC encryption parts of it). So after some days of trial and error and finally completing it, I want to share the process. Hopefully, this helps someone out there.
有一点背景知识:我最近选择了Golang并决定创建一个Twitter机器人作为辅助项目。 然后出现了问题。 几乎没有关于将Twitter API与Golang一起使用的文档(尤其是其中的oauth1和CRC加密部分)。 因此,经过几天的试验和错误并最终完成了它,我想分享一下过程。 希望这可以帮助某个人。
我们要建造什么? (What are we going to build?)
We will build a Twitter bot that will be served from our local machine. It will respond to any tweet that is tagged in with a “hello world”.
我们将构建一个Twitter机器人,该机器人将通过我们的本地计算机提供服务。 它将响应标记有“ hello world”的任何推文。
Here’s a brief explanation as to what this go program will do. It will:
这是有关此go程序将执行的操作的简要说明。 它会:
Listen and respond to webhook CRC validation.
侦听并响应webhook CRC验证 。
- Register a webhook URL that points to it. 注册指向它的webhook URL。
- Listen for tweets and respond with “hello world”. 听推文并用“ hello world”回复。
你需要什么? (What do you need?)
准备? 我们走吧 (Ready? Let’s Go)
$GOPATH/src/hellobot/hellobot.go
$GOPATH/src/hellobot/hellobot.go
The first thing we need to do is to create an endpoint for our app to listen to CRC checks and respond. Twitter sums up the requirements for the check pretty well.
我们需要做的第一件事是为我们的应用程序创建一个端点,以侦听CRC检查并做出响应。 Twitter很好地总结了检查的要求。
设置服务器 (Setting up a server)
.env
.env
CONSUMER_KEY=CONSUMER_SECRET=ACCESS_TOKEN_KEY=ACCESS_TOKEN_SECRET=WEBHOOK_ENV=devAPP_URL=
go getgo get
webhook/twittergo installhellobot
webhook/twittergo installhellobotgo install
CRC验证 (CRC validation)
CrcCheck()
CrcCheck()
Here what we do in the function is:
这里我们在函数中做的是:
- Set the header to ‘application/json’ 将标题设置为“ application / json”
- Get the crc_token URL parameter 获取crc_token URL参数
- Encrypt it using Hmac sha256 and encode it 使用Hmac sha256对其进行加密并进行编码
- Print it to the response writer 将其打印到响应编写器
CONSUMER_SECRETlocalhost:9090/webhook/twitter?crc_token=test
CONSUMER_SECRETlocalhost:9090/webhook/twitter?crc_token=test
localhost
localhost
ngrok http 9090
You should see a response similar to this:
您应该看到类似于以下内容的响应:
file A
file A
注册网络挂钩 (Registering the webhook)
register
register
-register registerWebhook()registerWebhook()client.go
-registerregisterWebhook()client.goregisterWebhook()
CreateClient()http.Clientgo getregisterWebhook
CreateClient()http.Clientgo getregisterWebhook
url.Valuesurl.Valuesurl.Values
接下来,我们需要我们的代码以将webhook订阅events 。
client.go
client.go
204go installhellobot -register
204go installhellobot -register
Starting ServerRegistering webhook...Webhook id of <hook_id> has been registeredSubscribing webapp...Subscribed successfully
听事件 (Listening for events)
Now we need our webhook to actually to listen for events once the URL is called. Update your files as shown below:
现在,我们需要Webhook在URL调用后实际监听事件。 如下所示更新文件:
hellobot.devclient.go
hellobot.devhellobot.devclient.go
Now update your code so it sends the tweet on the tag.
现在更新您的代码,以便它在标签上发送推文。
SendTweet()client.go
client.goSendTweet()
Note: Any tweet being sent as a reply needs to include the handle of the user it is replying to as the initial content of the reply
注意:作为回复发送的任何推文都必须包含正在回复的用户的句柄,作为回复的初始内容
Now if you run this with the appropriate credentials your bot should respond to tags and reply with “Hello World”.
现在,如果您使用适当的凭据运行此程序,您的机器人应响应标签并回复“ Hello World”。
结论 (Conclusion)
Now that’s done, and since this is an extremely basic version of a bot, you can try adding a few things:
现在已经完成了,并且由于它是机器人的一个非常基本的版本,因此您可以尝试添加一些内容:
- Checking and ignoring retweet events 检查和忽略转发事件
- Adding a name to the response 在响应中添加名称
- Responding to the tweet in the case of an error anywhere on the app. 在应用程序中任何地方出现错误的情况下响应推文。
The code for this walkthrough is on Github here. Feel free to fork and play around with it
此演练的代码在此处的Github上 。 随意分叉并玩转它
Cheers!
干杯!
golang微信机器人