微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

koa2开发微信公众号: 不定期推送最新币圈消息

背景:比特币说好的segwit2x分叉最后却分叉不成,如今算力又不够,于是比特现金想篡位? 没一个星期就涨了快10倍,错过这趟快车甚是后悔,于是打算写一个可不定期推送最新消息的微信公众号。既然是利用微信这个平台载体,当然要熟悉微信的api,遂封装了一下。

@H_502_7@

当然你也可以不看wechat-koa2的代码,直接使用下面的demo,不用百行代码便可轻松实现定制的币圈最新消息推送,后期会推出自动爬取全球各大交易所的最新消息,任何币种上线第一时间通知到位

先放出wechat-koa2 API,后续有时间再根据库币提供的api作封装

插播一个广告: 库币是由一群数字资产爱好者创建而成的一个专注区块链资产的交易平台,创始团队主要来自蚂蚁金服、广发证券等互联网和金融公司,致力于打造世界级的区块链资产交易平台。(跟NEO发放GAS一样,定期每天会根据用户持有的KCS发放鼓励金) 邀请注册链接 -> https://www.kucoin.com/#/?r=E...

0x00 wechat-koa2

const config = require('./config')

const Koa = require('koa')
const app = new Koa()

const Router = require('koa-router')
const router = new Router()

const Wechat = require('wechat-koa2')

const w = new Wechat(config)

// 封装过后的koa-bodyparser
app.use(w.bodyParser())

// 微信服务器校验
router.get('/',async(ctx) => {
    w.serverVerify(ctx)
})

// 监听用户发送过来的消息
router.post('/',async (ctx) => {
    await w.listening(ctx)
}

// 具体业务在可全写在这里注册监听(具体看后文介绍)
// ....
// ....

app.use(router.routes()).use(router.allowedMethods())

console.log('START: ',`wechat server is listening at ${config.port} ...`)

app.listen(config.port)

0x01 发送币种,查询实时价格和涨幅

这里使用的是sosobtc的接口,当然你也可以使用其他接口,如 zb.com库币

const config = require('./config')

const Koa = require('koa')
const app = new Koa()

const Router = require('koa-router')
const router = new Router()

const Wechat = require('wechat-koa2')

const w = new Wechat(config)

// 封装过后的koa-bodyparser
app.use(w.bodyParser())

// 微信服务器校验
router.get('/',async (ctx) => {
    await w.listening(ctx)
}

// 具体业务 具体业务 具体业务
w.onText(data => {
    const text = data.Content.toLowerCase()
    const coinURL = `http://sosobtc.in/api/vi/analysis/coinAllWeb?coin=${text}`
    
    w.get(coinURL).then(result => {
        result = result[0]
        const coin = result.coin
        const webSiteCn = result.webSiteCn || result.webSite
        const change24H = result.change24H
        const close = result.close
        const high = result.high
        const low = result.low
        
        if(coin.toLowerCase() === 'btc') {
            w.replyText(result_2 => {
                toUser: result_2.FromUserName,fromUser: result_2.ToUserName,content: `当前查询币种: BTC (${webSiteCn})
                        最新价格: ${close}
                        24H涨幅: ${change24H}%
                        最高价格: ${high} USD
                        最低价格: ${low} USD
                        来源: <a href="http://sosobtc.in/soso/kline_list?coin=btc">点击这里</a>
                `
            })
        } else {
            w.replyText(result_2 => {
                toUser: result_2.FromUserName,content: `当前查询币种: ${coin.toLowerCase()} (${webSiteCn})
                        最新价格: ${close}
                        24H涨幅: ${change24H}%
                        最高价格: ${high} BTC
                        最低价格: ${low} BTC
                        来源: <a href="http://sosobtc.in/soso/kline_list?coin=${coin.toLowerCase()}">点击这里</a>
                `
            })
        }
    })
})

app.use(router.routes()).use(router.allowedMethods())

console.log('START: ',`wechat server is listening at ${config.port} ...`)

app.listen(config.port)

PS: 本人原混于各种币圈,bts 2块3时入手靠信仰一直持到现在(哭...)。目前正在学习区块链(nodejs/python/go),若你也有兴趣,欢迎一起交流探讨

@H_502_7@

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。