An interesting weekend with Wechaty

Thanks for inviting me to write this article that give me a chance to share my story for each other. And Wechaty is a very awesome library that is powerful and easy to use. The most important point is that is interesting for Wechat users and developers.
Last week, I want to help my friend to make a chat bot. Because she is a manager of a technic community and she is also a owner of a chat group. In her daily life, many people add her to be the bunny on wechat. Then she needs add these persons into wechat group. It is a manual work, right? So I want to help her to lessen workload. So I ready to use wechaty. Firstly, I implemented some basic features. Like below

The chat bot can receive the friend request automatically
const bot = Wechaty.instance({profile: 'secretary'});
bot.on('friend', async function(contact, request){
  if(request){
    await request.accept()
    await contact.say('您好巷送,我是 FCC(freeCodeCamp成都社區(qū))的姜姜姜屯远,很高興認(rèn)識(shí)你*^_^*回復(fù)暗號(hào)”FCC成都社區(qū)”, 加入FCC成都社區(qū)群套鹅。直接聊天瘾杭,請(qǐng)  隨意…')
  }
})
Input keyword then add him/her into the chat group
  bot.on('message', async function(m){
    const fromContact = m.from()
    const fromContent = m.content()
    const room = m.room()
    if(/FCC成都社區(qū)/.test(fromContent)){
      let keyroom = await Room.find({topic: 'FreeCodeCamp-成都'});
      if(keyroom){
        await keyroom.add(fromContact);
        await keyroom.say(`歡迎 @${fromContact.name()} 加入FCC(freecodecamp)成都社區(qū)*^_^*`)
      }
    }
  })

The above feature has already help her lessen some work. But my friend think it is not enough to satisfy her requirement. She is a very beautiful girl, so there is someone always asked her if she has boyfriend. That makes her boyfriend a little annoyed. So she want me to implement a feature that is if someone in this chat group ask her something about her bf the chat bot can send a photo of her bf.
Wow I’m honoured to develop this feature. So I started to read the doc of wechaty. But I can’t get how to send a media message. I continued reading all of the issues that let me know wechaty can send a media message but I don’t know the details. Then I try to use another methods. I try to use node.js to resolve. I want to change the images to be the buffer of base64. But it didn’t work, users just receive some strange string…..Finally I ask @zixia directly. He sent me an article (https://blog.chatie.io/2017/04/13/support-message-type-of-image-and-video.html). This article analysis the principle about how the wechat sends images. It says wechat use “MediaId” to store the information of media and blablablabla……Finally the Wechaty contributors got the “MediaId”. WoW so cool! They integrate this function in Wechaty which use say(MediaMessage(filename)). If someone interested in this principle can read this article.
In the end of the article says ding-dong-bot.ts has already implement it. So I found this file that use like below:

import { MediaMessage } wechaty
await m.say(new MediaMessage(__dirname + '/../image/BotQrcode.png'))

I tried immediately, it does work. hahaperfect
Then I use api.ai. If someone say: ‘Does @姜姜姜 have boy friend?’ or say some synonym the chat bot will send a photo of her bf automatically. Well that was I did in last weekend. I think wechaty is an interesting library.

At the end, I help the author of Wechaty add this api to the Wechaty documentation.

屏幕快照 2017-06-23 上午9.44.27.png
852290473.jpg

Well, the complete codes like below:

import 'babel-polyfill'
import { Wechaty, Room, MediaMessage, log } from 'wechaty'
import apiai from 'apiai'

const app = apiai('46a33e7a9cb741fb96e0dcc3d2d03a6c');
const bot = Wechaty.instance({profile: 'secretary'});

bot.on('scan', (url, code)=>{
  log.info(url);
})
.on('login', user => {
  log.info(`${user} is login`)
})
.on('friend', async function(contact, request){
  if(request){
    await request.accept();
    await contact.say('您好诅病,我是 FCC(freeCodeCamp成都社區(qū))的姜姜姜,很高興認(rèn)識(shí)你*^_^*回復(fù)暗號(hào)”FCC成都社區(qū)”, 加入FCC成都社區(qū)群贤笆。直接聊天蝇棉,請(qǐng)隨意…')
  }
})
.on('message', async function(m){
  if(m.self()){
    return;
  }
  const fromContact = m.from();
  const fromContent = m.content();
  const room = m.room();
  const noAtMention = fromContent.replace(/@\w+/ig, '');
  let roomTopic;

  const request = app.textRequest(noAtMention, {
    sessionId: '1234567890'
  });

  request.on('error', function(error) {
    log.error(error);
  });

  request.on('response', async function(response) {
    const speech = response.result.fulfillment.speech;
    if(/FCC成都社區(qū)/.test(fromContent)){
      let keyroom = await Room.find({topic: 'FreeCodeCamp-成都'});
      if(keyroom){
        await keyroom.add(fromContact);
        await keyroom.say(`歡迎 @${fromContact.name()} 加入FCC(freecodecamp)成都社區(qū)*^_^*`)
      }
    }
    m.type() == 10000 && m.say('@Helen') // Umm.. shame on me.... If someone give out a Red packet the chat bot will @ myself
    if(room && room.rawObj.NickName == 'FreeCodeCamp-成都'){
      if(/jiangjiangjiang/.test(speech)){
        await m.say(new MediaMessage('images/test.jpg'))
      }else{
        m.say(speech)
      }
    }else if(!room){
      m.say(speech)
    }
  })
  request.end();
})
.init()

Finally, I want to thank our teammate @拖拉機(jī)@姜姜姜@glowd @myself

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市芥永,隨后出現(xiàn)的幾起案子篡殷,更是在濱河造成了極大的恐慌,老刑警劉巖埋涧,帶你破解...
    沈念sama閱讀 222,183評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件板辽,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡棘催,警方通過(guò)查閱死者的電腦和手機(jī)劲弦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)醇坝,“玉大人邑跪,你說(shuō)我怎么就攤上這事『糁恚” “怎么了画畅?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,766評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)宋距。 經(jīng)常有香客問(wèn)我夜赵,道長(zhǎng),這世上最難降的妖魔是什么乡革? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,854評(píng)論 1 299
  • 正文 為了忘掉前任寇僧,我火速辦了婚禮,結(jié)果婚禮上沸版,老公的妹妹穿的比我還像新娘嘁傀。我一直安慰自己,他們只是感情好视粮,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布细办。 她就那樣靜靜地躺著,像睡著了一般蕾殴。 火紅的嫁衣襯著肌膚如雪笑撞。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,457評(píng)論 1 311
  • 那天钓觉,我揣著相機(jī)與錄音茴肥,去河邊找鬼。 笑死荡灾,一個(gè)胖子當(dāng)著我的面吹牛瓤狐,可吹牛的內(nèi)容都是我干的瞬铸。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼础锐,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼嗓节!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起皆警,我...
    開(kāi)封第一講書(shū)人閱讀 39,914評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤拦宣,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后信姓,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體鸵隧,經(jīng)...
    沈念sama閱讀 46,465評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評(píng)論 3 342
  • 正文 我和宋清朗相戀三年财破,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了掰派。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,675評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡左痢,死狀恐怖靡羡,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情俊性,我是刑警寧澤略步,帶...
    沈念sama閱讀 36,354評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站定页,受9級(jí)特大地震影響趟薄,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜典徊,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評(píng)論 3 335
  • 文/蒙蒙 一杭煎、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧卒落,春花似錦羡铲、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,514評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至腰湾,卻和暖如春雷恃,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背费坊。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,616評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工倒槐, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人葵萎。 一個(gè)月前我還...
    沈念sama閱讀 49,091評(píng)論 3 378
  • 正文 我出身青樓导犹,卻偏偏與公主長(zhǎng)得像唱凯,于是被迫代替她去往敵國(guó)和親羡忘。 傳聞我的和親對(duì)象是個(gè)殘疾皇子谎痢,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評(píng)論 2 360

推薦閱讀更多精彩內(nèi)容