Koa2 develops WeChat official account: pushing the latest currency message on a regular basis.

Time:2020-11-30

Background: bitcoin’s good segwit2x bifurcation ends up with no branching, but it’s not enough. So, bit cash wants to usurp it. It has risen 10 times in a week. The regret of missing the express is intended to write a WeChat official account that can push the latest news on a regular basis. Since we are using wechat as the platform carrier, we should be familiar with wechat API, so we encapsulate it.

Of course, you can also use the demo below without looking at the wechat-koa2 code. Without a hundred lines of code, you can easily push the latest news of the customized currency circle. Later, it will automatically crawl the latest news of major exchanges around the world. Any currency will be notified in place at the first time

First release the wechat-koa2 API, and then package it according to the API provided by the currency

There is an advertisement: kucoin is a trading platform focusing on blockchain assets created by a group of digital asset enthusiasts. The founding team mainly comes from Internet and financial companies such as ant financial and Guangfa Securities, and is committed to building a world-class blockchain asset trading platform. (the same as Neo for gas, incentive money will be given every day according to KCs held by users) invite registration link – >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)

//Encapsulated KOA bodyparser
app.use(w.bodyParser())

//Wechat server verification
router.get('/', async(ctx) => {
    w.serverVerify(ctx)
})

//Listen for messages sent by users
router.post('/', async (ctx) => {
    await w.listening(ctx)
}

//The specific business can be written here to register and monitor (see the following introduction for details)
// ....
// ....

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

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

app.listen(config.port)

0x01 send currency, query real-time price and increase

The sosobtc interface is used here. Of course, you can also use other interfaces, such aszb.comBanknotesetc.

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)

//Encapsulated KOA bodyparser
app.use(w.bodyParser())

//Wechat server verification
router.get('/', async(ctx) => {
    w.serverVerify(ctx)
})

//Listen for messages sent by users
router.post('/', async (ctx) => {
    await w.listening(ctx)
}

//Specific business specific business specific business
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: 'current query currency: BTC (${websitecn})
                        Latest price: ${close}
                        24h rise: ${change24h}%
                        Highest price: ${high} USD
                        Lowest price: ${low} USD
                        Source: < A=“ http://sosobtc.in/soso/kline_ List? Coin = BTC "> Click here</a>
                `
            })
        } else {
            w.replyText(result_2 => {
                toUser: result_2.FromUserName,
                fromUser: result_2.ToUserName,
                Content: 'current query currency:${ coin.toLowerCase ()} (${webSiteCn})
                        Latest price: ${close}
                        24h rise: ${change24h}%
                        Highest price: ${high} BTC
                        Lowest price: ${low} BTC
                        Source: < A=“ http://sosobtc.in/soso/kline_ list?coin=${ coin.toLowerCase ()} "> Click here</a>
                `
            })
        }
    })
})

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

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

app.listen(config.port)

PS: I used to mix in all kinds of coin circles. I started with BTS at 2:30, and I have been holding it till now (crying…). At present, we are learning blockchain (nodejs / Python / go). If you are also interested, please discuss with us