Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle group messages #238

Merged
merged 23 commits into from
Feb 17, 2021
Merged

Handle group messages #238

merged 23 commits into from
Feb 17, 2021

Conversation

nonumpa
Copy link
Member

@nonumpa nonumpa commented Jan 26, 2021

Fixes #13

Description

Not ready

  • (done)Change validCategories from categoryName to categoryId
  • (done)Should filter out the category which negative vote > positive vote
  • Self-introduction wording
  • (done)Should disable job queue rate limit and add concurrency to env variable
  • Maybe add a leave group command, it seems people can't kick others in the room

Flow

webhook/index (get group message event) -> GroupHandler(jobQueue) -> processGroupEvent -> groupMessage

Response rule

  • Chat-bot self-introduction : Input Hi cofacts or Hi confacts .
  • Ignore message : Input length less than 10 won't reply and post to cofacts-api.
  • Reply :
    • The article found should be in validCategories and be Identical enough.
      • validCategories: 疾病、醫藥🆕, COVID-19 疫情🆕, 科技、資安、隱私, 保健秘訣、食品安全, 免費訊息詐騙
    • The reply of the article should:
      1. Candidate's positiveFeedbackCount > candidate's negativeFeedbackCount.
      2. Candidate's positiveFeedbackCount > non-rumors' positiveFeedbackCount.
      3. If replyCount > 2, check if rumor count >> non-rumor count (2:1).

Job queue

  • jobQueue will process events which replytoken are not expired.
  • If the event which jobQueue is going to process is expired, it will be skipped and added to expiredJobQueue.
  • expiredJobQueue start only when there's no jobs in jobQueue(drained).
  • expiredJobQueue pause when there's jobs adding to jobQueue.

GA

Add two user scope custom dimension

  1. Message Source: user | group | room will set every time sends the event.
  2. NumberOfGroupMembers: It will only set when bot join group.

Article event in group

Category / Action / Label

  • If user trigger chat-bot to introduce self:
    • UserInput / Intro /
  • If we found a articles in database that matches the message:
    • UserInput / ArticleSearch / ArticleFound
    • Article / Search / <article id> for each article found
  • We don't send event If nothing found in database

To calculate total group the bot joined, we set ga event value 1 as join, -1 as leave

Category / Action / Value

  • Join Event
    • 'Group' / 'Join' / 1
  • Leave Event
    • 'Group' / 'Leave' / -1

Snapshot shows where to create custom dimension in ga.

截圖 2021-01-26 下午7 24 26

- Add createGroupReplyMessages function and extracts common reply from createReplyMessages which is for single user
- Add TimeoutError which will be throw in groupHandler if event replyToken expired
…`uuid`

Should also create a user scope custom dimension from web :  Setting -> Account -> Property -> Custom Dimension
Avoid processing expired event if there may be other active events
@nonumpa nonumpa marked this pull request as ready for review January 29, 2021 18:01
@MrOrz MrOrz temporarily deployed to rumors-line-bot-staging February 3, 2021 04:45 Inactive
Copy link
Member

@MrOrz MrOrz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still reviewing, just send out 2 comments here

src/lib/__tests__/ga.js Show resolved Hide resolved
src/webhook/handlers/utils.js Outdated Show resolved Hide resolved
Copy link
Member

@MrOrz MrOrz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the implementation!

I have added some comments and some hints on GA. Happy Lunar New Year!

src/webhook/handlers/processGroupEvent.js Outdated Show resolved Hide resolved
src/webhook/handlers/processGroupEvent.js Show resolved Hide resolved
src/webhook/handlers/groupMessage.js Outdated Show resolved Hide resolved
src/webhook/handlers/groupMessage.js Outdated Show resolved Hide resolved
src/webhook/handlers/groupMessage.js Outdated Show resolved Hide resolved
src/webhook/handlers/groupMessage.js Outdated Show resolved Hide resolved
src/webhook/handlers/__tests__/groupMessage.test.js Outdated Show resolved Hide resolved
src/webhook/handlers/__tests__/groupMessage.test.js Outdated Show resolved Hide resolved
gh.addJob(param, { jobId: 'testExpiredId1' });
gh.addJob(param, { jobId: 'testExpiredId2' });
gh.addJob(param, { jobId: 'testExpiredId3' });
gh.addJob(param, { jobId: 'testExpiredId4' });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confused by the 4 jobs and the changing processGroupEvent mock. May I know what would happen to the 4 jobs here?

Copy link
Member Author

@nonumpa nonumpa Feb 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有點難描述,我用中文好了 哈哈

這裡要測試 expireQueue 執行時,如果有新的 event 進來,expireQueue 要被暫停,並且執行完 jobQueue 後再繼續,利用監聽 expiredJobQueue 的 active event 加上 concurrency 設成 1,我們可以確保在 expiredJobQueue 執行期間,插入一個沒過期的 job。

  1. 因為這裡把 GroupHandlerconcurrency 設成 1,所以四個 expiredJob 會在 jobQueue 的 process function processJob 一個一個(不保證順序)處理並加進 expiredJobQueue,這裡並不會執行到 processJob 裡面的 processGroupEvent

  2. 當所有(四個) jobQueue 的 job 被執行完之後(onDrained),expiredJobQueue 會開始(resume)執行。

  3. 當第一個 expired job 開始執行時,會同時送出一個 expiredJobQueue 的 job active event。

  4. 在收到 expired job active event 的時候加入一個沒過期的 job (到 jobQueue),並且為了讓它成功,把 processGroupEvent 改成 resolve,在 jobQueue on('completed' 改回來。
    其實這邊我期望是可以 mockImplementationOnce(expired x 4 + success x 1),但是 processGroupEvent 被執行的順序可能是

    • testExpiredId1 -> (expiredJobQueue active event) -> successJobId -> testExpiredId2 -> ...
    • testExpiredId1 -> (expiredJobQueue active event) testExpiredId2 (幾乎同時)-> successJobId -> ...

    因為我們不能保證 active event 收到的時機,不知道 success mock 要放在第幾個順位才能正確讓 successJobId 是 resolve 的,所以就在收到第一個 expiredJobQueue (job) 的 active event 時把它改成 resolve,在它完成時再改回 reject,這樣就會造成一個問題是:把 processGroupEvent 改成 resolve 時剛好 testExpiredId2 開始了 expiredJobQueue 還沒 pause,所以 testExpiredId2 的結果就是 complete 的(理論上 expiredJobQueue 的 job 要在 processGroupEvent 裡面全被 reject)
    這裡只 expect jobQueue 的 success/fail count,因為 expiredJobQueue 的結果可能每次都不一樣

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感謝感謝
我在想如果「這裡要測試 expireQueue 執行時,如果有新的 event 進來,expireQueue 要被暫停,並且執行完 jobQueue 後再繼續」

若把下面這兩塊拆成兩個分開的 test case,會不會比較簡單

  1. expireQueue 執行時,如果有新的 event 進來,expireQueue 要被暫停 --> 先往 expireQueue 塞東西,再 gh.addJob,然後 expect expireQueue 的 status
  2. 執行完 jobQueue 後再繼續 --> 先準備一個暫停的 expireQueue,然後 gh.addJob,然後在該 job 完成之後 expect expireQueue 的 status 是否不是 pause

@coveralls
Copy link

Pull Request Test Coverage Report for Build 1270

  • 116 of 132 (87.88%) changed or added relevant lines in 8 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.3%) to 86.709%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/webhook/handlers/groupHandler.js 28 32 87.5%
src/webhook/index.js 5 10 50.0%
src/webhook/lineClient.js 0 7 0.0%
Totals Coverage Status
Change from base Build 1240: 0.3%
Covered Lines: 918
Relevant Lines: 1045

💛 - Coveralls

Copy link
Member

@MrOrz MrOrz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems that most issues are resolved. Things left are

  1. documentation regarding the custom dimensions; and if we should use custom metric instead of custom dimension for user count in chat groups
  2. excessive comment
  3. simplify test case

I think we can discuss on 1 because setting custom metric / dimension cannot be undone in the future. Documentations, comments & tests can wait.

@MrOrz
Copy link
Member

MrOrz commented Feb 17, 2021

We will merge first and just setup 1st custom dimension. Leave 2nd dimension / metric for the future.

@MrOrz MrOrz merged commit 2db29f6 into dev Feb 17, 2021
@MrOrz MrOrz deleted the feature/group-message branch February 17, 2021 12:27
@nonumpa nonumpa mentioned this pull request Feb 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

支援 LINE chatbot 群組聊天
3 participants