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

Fix group message issues #244

Merged
merged 8 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Other customizable env vars are:
* `PORT`: Which port the line bot server will listen at.
* `GOOGLE_DRIVE_IMAGE_FOLDER`: Google drive folder id is needed when you want to test uploading image.
* `GOOGLE_CREDENTIALS`: will be populated by `authGoogleDrive.js`. See "Upload image/video" section below.
* `GA_ID`: Google analytics universal analytics tracking ID, for tracking events
* `GA_ID`: Google analytics tracking ID, for tracking events. You should also add custom dimensions and metrics, see "Google Analytics Custom dimensions and metrics" section below.
* `IMAGE_MESSAGE_ENABLED`: Default disabled. To enable, please see "Process image message" section below.
* `DEBUG_LIFF`: Disables external browser check in LIFF. Useful when debugging LIFF in external browser. Don't enable this on production.
* `RUMORS_LINE_BOT_URL`: Server public url which is used to generate tutorial image urls and auth callback url of LINE Notify.
Expand Down Expand Up @@ -233,6 +233,10 @@ To use Dialogflow,
- `DAILOGFLOW_LANGUAGE` : Empty to agent's default language, or you can specify a [language](https://cloud.google.com/dialogflow/es/docs/reference/language).
- `DAILOGFLOW_ENV` : Default to draft agent, or you can create different [versions](https://cloud.google.com/dialogflow/es/docs/agents-versions).

### Google Analytics Custom dimensions and metrics

[Create](https://support.google.com/analytics/answer/2709829) a custom (user scope) dimemsion for `Message Source`, and a custom (hit scope) metrix for `Group Members Count`. Both of them default index is 1. If the indexes GA created are not 1, find `cd1` and `cm1` in the code and change them to `cd$theIndexGACreated` and `cm$theIndexGACreated` respectively.

---

## Production Deployment
Expand All @@ -245,7 +249,6 @@ You can deploy the line bot server to your own Heroku account by [creating a Her

Despite the fact that we don't use `Procfile`, Heroku still does detection and installs the correct environment for us.


### Prepare storage services

#### Redis
Expand Down Expand Up @@ -290,6 +293,12 @@ Consult `.env.sample` for other optional env vars.

Sent event format: `Event category` / `Event action` / `Event label`

We use dimemsion `Message Source` (Custom Dimemsion1) to classify different event sources
- `user` for 1 on 1 messages
- `room` | `group` for group messages

### 1 on 1 messages

1. User sends a message to us
- `UserInput` / `MessageType` / `<text | image | video | ...>`
- For the time being, we only process message with "text" type. The following events only applies
Expand Down Expand Up @@ -352,13 +361,41 @@ Sent event format: `Event category` / `Event action` / `Event label`
- `LIFF` / `page_redirect` / `App` is sent on LIFF redirect, with value being redirect count.

12. Tutorial
- If it's triggered by follow event
- If it's triggered by follow event (a.k.a add-friend event)
- `Tutorial` / `Step` / `ON_BOARDING`
- If it's triggered by rich menu
- `Tutorial` / `Step` / `RICH_MENU`
- Others
- `Tutorial` / `Step` / `<TUTORIAL_STEPS>`

### Group messages

1. When chatbot joined/leaved a group or a room
- Join
- `Group` / `Join` / `1` (`Event category` / `Event action` / `Event value`)
- And `Group Members Count` (Custom Metric1) to record group members count when chatbot joined.
- Leave
- `Group` / `Leave` / `-1` (`Event category` / `Event action` / `Event value`)
> Note:
>
> 1. We set ga event value 1 as join, -1 as leave.
> To know total groups count chatbot currently joined, you can directly see the total event value (Details see [Implicit Count](https://support.google.com/analytics/answer/1033068?hl=en)).
> 2. To know a group is currently joined or leaved, you should find the last `Join` or `Leave` action of the `Client Id`.
> 3. Also, you should find the last `Join` action of the `Client Id` to get a more accurate `Group Members Count`.
> `Group Members Count` is only recorded when chatbot joined group, to know the exact count, you should directly get it from [line messaging-api](https://developers.line.biz/en/reference/messaging-api/#get-members-group-count).

2. User sends a message to us
- If we found a articles in database that matches the message:
- `UserInput` / `ArticleSearch` / `ArticleFound`
- `Article` / `Search` / `<article id>` for each article found
- If the article is identical
- `Article` / `Selected` / `<selected article id>`
- If the article has a valid category and the reply is valid (Details see [#238](https://github.com/cofacts/rumors-line-bot/pull/238))
- `Reply` / `Selected` / `<selected reply id>`

3. User trigger chatbot to introduce itself:
- `UserInput` / `Intro` /

## Legal

`LICENSE` defines the license agreement for the source code in this repository.
Expand Down
64 changes: 31 additions & 33 deletions src/webhook/handlers/__fixtures__/groupHandler.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
const joinGroup = {
replyToken: 'nHuyWiB7yP5Zw52FIkcQobQuGDXCTA',
type: 'join',
mode: 'active',
timestamp: 1462629479859,
source: {
type: 'group',
groupId: 'C4a1',
},
};

const leaveGroup = {
replyToken: 'nHuyWiB7yP5Zw52FIkcQobQuGDXCTA',
type: 'leave',
mode: 'active',
timestamp: 1462629479859,
source: {
type: 'group',
groupId: 'C4a1',
groupId: 'C4a1',
otherFields: {
mode: 'active',
timestamp: 1462629479859,
source: {
type: 'group',
groupId: 'C4a1',
},
},
};

const textMessage = {
replyToken: 'nHuyWiB7yP5Zw52FIkcQobQuGDXCTA',
type: 'message',
mode: 'active',
timestamp: 1462629479859,
message: {
type: 'text',
},
source: {
type: 'group',
groupId: 'C4a1',
groupId: 'C4a1',
otherFields: {
mode: 'active',
timestamp: 1462629479859,
message: {
type: 'text',
},
source: {
type: 'group',
groupId: 'C4a1',
},
},
};

const expiredTextMessage = {
replyToken: 'nHuyWiB7yP5Zw52FIkcQobQuGDXCTA',
type: 'message',
mode: 'active',
timestamp: 612921600000,
message: {
type: 'text',
},
source: {
type: 'group',
groupId: 'C4a1',
groupId: 'C4a1',
otherFields: {
mode: 'active',
timestamp: 612921600000,
message: {
type: 'text',
},
source: {
type: 'group',
groupId: 'C4a1',
},
},
};

export default { joinGroup, leaveGroup, textMessage, expiredTextMessage };
export default { joinGroup, textMessage, expiredTextMessage };
9 changes: 8 additions & 1 deletion src/webhook/handlers/__fixtures__/groupMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const validArticleWithOneCategory = {
articleReplies: [
{
reply: {
id: 'fake-reply-id',
type: 'RUMOR',
text: 'It`s rumor. It`s rumor. It`s rumor.',
reference: 'www.who.int',
Expand Down Expand Up @@ -55,6 +56,7 @@ const validArticleWithTwoCategories = {
articleReplies: [
{
reply: {
id: 'fake-reply-id1',
type: 'RUMOR',
text: '這是謠言!這是謠言!這是謠言!這是謠言!',
reference: 'https://taiwanbar.cc/',
Expand All @@ -68,7 +70,7 @@ const validArticleWithTwoCategories = {
{
node: {
text: '請問黑啤愛吃什麼?黑啤愛吃蠶寶寶',
id: '3nbzf064ks60d',
id: '8nbzf064ks87g',
articleCategories: [
{
categoryId: 'covid19',
Expand All @@ -79,6 +81,7 @@ const validArticleWithTwoCategories = {
articleReplies: [
{
reply: {
id: 'fake-reply-id2',
type: 'RUMOR',
text: '這是謠言!這是謠言!這是謠言!這是謠言!',
reference: 'https://taiwanbar.cc/',
Expand All @@ -105,6 +108,7 @@ const validArticleWithTwoCategories = {
articleReplies: [
{
reply: {
id: 'fake-reply-id3',
type: 'RUMOR',
text:
'謠言說進口蘋果會上蠟,所以一定要削皮,但其實不用太擔心。蘋果自己本身就會產生蠟,為了增加保存期限,農家也會將蘋果上蠟。\n蘋果本身就會產生天然蠟來保護果肉,並不讓水分流失,這天然蠟還非常營養,富含花青素、槲皮素等等,能夠抵抗發炎、過敏等反應,而且不是只有蘋果會產生果蠟,還有許多水果,像是甘蔗、檸檬或是李子,也都會產生果蠟。',
Expand Down Expand Up @@ -139,6 +143,7 @@ const invalidCategoryFeedback = {
articleReplies: [
{
reply: {
id: 'fake-reply-id',
type: 'RUMOR',
text: '這是謠言!這是謠言!這是謠言!這是謠言!',
reference: 'https://taiwanbar.cc/',
Expand Down Expand Up @@ -237,6 +242,7 @@ const invalidArticleCategory = {
articleReplies: [
{
reply: {
id: 'fake-reply-id',
type: 'RUMOR',
text:
'謠言說進口蘋果會上蠟,所以一定要削皮,但其實不用太擔心。蘋果自己本身就會產生蠟,為了增加保存期限,農家也會將蘋果上蠟。\n蘋果本身就會產生天然蠟來保護果肉,並不讓水分流失,這天然蠟還非常營養,富含花青素、槲皮素等等,能夠抵抗發炎、過敏等反應,而且不是只有蘋果會產生果蠟,還有許多水果,像是甘蔗、檸檬或是李子,也都會產生果蠟。',
Expand Down Expand Up @@ -271,6 +277,7 @@ const invalidArticleReply = {
articleReplies: [
{
reply: {
id: 'fake-reply-id',
type: 'NOT_RUMOR',
text: '沒錯!正確答案',
reference: '我自己',
Expand Down
Loading