Skip to content

Commit

Permalink
refactor: ts support
Browse files Browse the repository at this point in the history
  • Loading branch information
shfshanyue committed May 13, 2020
1 parent 5706b29 commit d542742
Show file tree
Hide file tree
Showing 16 changed files with 224 additions and 78 deletions.
3 changes: 3 additions & 0 deletions a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"a": 4
}
4 changes: 2 additions & 2 deletions event/friend-ship.js → event/friend-ship.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { Friendship } = require("wechaty")
import { Friendship } from 'wechaty'

const frienddShipRe = /疫情|我是|面试|领课/

// 添加好友
exports.handleFriendShip = async (friendship) => {
export const handleFriendShip = async (friendship) => {
console.log(friendship)
if (friendship.type() === Friendship.Type.Receive) {
if (frienddShipRe.test(friendship.hello())) {
Expand Down
14 changes: 6 additions & 8 deletions event/message.js → event/message.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { Message } = require('wechaty')
const _ = require('lodash')
import { Message } from 'wechaty'
import _ from 'lodash'

const covid = require('../message/covid')
const fund = require('../message/fund')
const interview = require('../message/interview')
import * as covid from '../message/covid'
import * as fund from '../message/fund'
import * as interview from '../message/interview'

const routes = [
{ keyword: '疫情', handle: covid.ncov },
Expand All @@ -21,7 +21,7 @@ async function reply (msg, _data) {
}
}

async function handleMessage (msg) {
export async function handleMessage (msg) {
if (msg.type() == Message.Type.Text) {
if (!msg.room() || await msg.mentionSelf()) {
const self = await msg.to()
Expand All @@ -32,5 +32,3 @@ async function handleMessage (msg) {
}
}
}

exports.handleMessage = handleMessage
4 changes: 2 additions & 2 deletions event/room-join.js → event/room-join.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _ = require('lodash')
import _ from 'lodash'

const me = ['wxid_fw1ohd5c982222', 'wxid_1qizr97llbta22']

Expand All @@ -12,7 +12,7 @@ const hello = contact => _.trim(`
github: https://github.com/shfshanyue
`)

exports.handleRoomJoin = (room, inviteeList) => {
export const handleRoomJoin = (room, inviteeList) => {
if (me.includes(room.owner().id)) {
inviteeList.map(c => {
room.say(hello(c), c)
Expand Down
32 changes: 0 additions & 32 deletions index.js

This file was deleted.

32 changes: 32 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Wechaty } from 'wechaty'
import { PuppetPadplus } from 'wechaty-puppet-padplus'
import Qrterminal from 'qrcode-terminal'

import * as message from './event/message'
import * as friendShip from './event/friend-ship'
import * as roomJoin from './event/room-join'

import { schedule } from './schedule'

const bot = new Wechaty({
puppet: new PuppetPadplus(),
name: 'daxiange'
})

function handleScan (qrcode: string) {
Qrterminal.generate(qrcode, { small: true })
}

bot
.on('scan', handleScan)
.on('room-join', roomJoin.handleRoomJoin)
.on('friendship', friendShip.handleFriendShip)
.on('message', message.handleMessage)
.start()

// 需要保证 bot start 之后调用
// 然而 bot.start().then() 无法触达,只好使用这种笨办法
setTimeout(() => {
schedule(bot)
}, 3000)

20 changes: 8 additions & 12 deletions message/covid.js → message/covid.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const _ = require('lodash')
const countries = require('../data/countries.json')
const { FileBox } = require('wechaty')
const stat = require('../data/stat.json')
import _ from 'lodash'
import { FileBox } from 'wechaty'

import countries from '../data/countries.json'
import stat from '../data/stat.json'

function format ({ deadCount, curedCount, confirmedCount, currentConfirmedCount}) {
return _.trim(`
Expand All @@ -15,26 +16,21 @@ function format ({ deadCount, curedCount, confirmedCount, currentConfirmedCount}
}

// 根据关键字(国家名),返回该国家疫情信息
exports.keyword = (keyword) => {
export const keyword = (keyword) => {
const country = countries.find(c => keyword === c.provinceName)
if (!country) {
return
}
const charts = stat.importantForeignTrendChart
const chart = charts.find(chart => chart.title.startsWith(country.provinceName))
const data = [format(country)]
if (chart) {
data.push(FileBox.fromUrl(chart.imgUrl))
}
return data
}

// 根据关键字疫情,返回所有疫情信息
exports.ncov = (keyword) => {
export const ncov = (keyword) => {
const data = [[
'国家, 确诊, 死亡, 病死率',
..._.sortBy(countries, x => -x.currentConfirmedCount).slice(0, 30).map(country => {
const { deadCount, confirmedCount, currentConfirmedCount } = country
const { deadCount, confirmedCount } = country
return `${country.provinceName}${confirmedCount}】【${deadCount}】【${_.round(deadCount / confirmedCount * 100, 2)}%】`
})
].join('\n')]
Expand Down
4 changes: 2 additions & 2 deletions message/fund.js → message/fund.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const axios = require('axios')
import axios from 'axios'

const url = 'http://fund.eastmoney.com/data/FundGuideapi.aspx?dt=0&rs=1y,100_1n,100_3n,100_2n,100'

Expand All @@ -8,7 +8,7 @@ function getFunds () {
})
}

exports.topFund = async () => {
export const topFund = async () => {
const funds = await getFunds()
return funds.map(fund => `${fund[0]}: ${fund[1]}`).join('\n')
}
2 changes: 1 addition & 1 deletion message/interview.js → message/interview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function getQuestion () {
})
}

exports.randomQuestion = async () => {
export const randomQuestion = async () => {
const q = await getQuestion()
return `今日面试题:${q.title.slice(6)}
Expand Down
66 changes: 66 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"ncu": "ncu",
"dev": "nodemon"
"dev": "nodemon index.ts"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/cron": "^1.7.2",
"@types/lodash": "^4.14.150",
"cron": "^1.8.2",
"lodash": "^4.17.15",
"npm-check-updates": "^5.0.0",
"qrcode-terminal": "^0.12.0",
"ts-node": "^8.10.1",
"wechaty": "^0.38.4",
"wechaty-puppet-padplus": "^0.6.3"
},
"devDependencies": {
"axios": "^0.19.2",
"nodemon": "^2.0.3"
"nodemon": "^2.0.3",
"typescript": "^3.9.2"
}
}
13 changes: 0 additions & 13 deletions schedule/index.js

This file was deleted.

7 changes: 7 additions & 0 deletions schedule/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Wechaty } from 'wechaty'

import inteviewBot from './interview'

export async function schedule (bot: Wechaty) {
await inteviewBot(bot)
}
15 changes: 15 additions & 0 deletions schedule/interview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Wechaty } from 'wechaty'
import { CronJob } from 'cron'
import { randomQuestion } from '../message/interview'

// 找到前端面试,及前端进阶开头的群名,每天 9:45 定时推送
export default async (bot: Wechaty) => {
return new CronJob('45 9 * * *', async () => {
const rooms = await bot.Room.findAll({ topic: /前端面试|前端进阶/ })
const q = await randomQuestion()
for (const room of rooms) {
// const alias = await room.
await room.say(q)
}
}, null, true, 'Asia/Shanghai')
}
Loading

0 comments on commit d542742

Please sign in to comment.