Skip to content
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
5 changes: 4 additions & 1 deletion ds-bot/src/host/im/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,10 @@ export function install(rt) {
return
}
armed.set(armKey(conn, chat.key), cardKey)
enqueue(conn, chat.key, { chat: { key: chat.key, kind: chat.kind, title: chat.title }, text: text('typePrompt'), forceReply: true })
// Only a group needs the reply box: there an unaddressed message is dropped
// before the armed check, and a reply to the bot counts as addressed. In a
// direct chat the armed map alone matches the answer, so the box is noise.
enqueue(conn, chat.key, { chat: { key: chat.key, kind: chat.kind, title: chat.title }, text: text('typePrompt'), forceReply: chat.kind === 'group' })
}
// The typed answer for the card this chat armed. False when that card is no
// longer open, so the text goes on as an ordinary message.
Expand Down
24 changes: 22 additions & 2 deletions ds-bot/test/im.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,8 @@ test('the typed-answer button arms a reply prompt and the next text answers that
await press(s, 't:q:q1')
await s.flush()
const prompt = s.fake.sent.at(-1)
assert.equal(prompt.forceReply, true, 'the prompt asks Telegram for a reply box')
assert.equal(prompt.text, 'Type your answer and send it.')
assert.equal(prompt.forceReply, false, 'a direct chat gets no reply box: the arm alone matches the answer')
assert.equal(prompt.buttons, undefined, 'a reply prompt carries no inline keyboard')
assert.ok(s.rt.state.questions.a !== undefined, 'arming answers nothing yet')
s.prompts.length = 0
Expand All @@ -724,6 +725,25 @@ test('the typed-answer button arms a reply prompt and the next text answers that
assert.equal(s.prompts.length, 1, 'the arm is spent: the next text is an ordinary message')
})

test('in a group the typed-answer prompt keeps the reply box, and the reply answers the card', async (t) => {
const s = await start()
t.after(s.stop)
const conn = await pairedChat(s)
await gm(s, 'u1', '@fakebot hello')
await s.rt.imEndpoint('im-chat', { id: conn.id, chatKey: 'g1', target: { botId: 'a' } })
s.fake.sent.length = 0
await askQuestion(s)
assert.ok(s.fake.sent.some(entry => entry.chat?.key === 'g1' && entry.buttons !== undefined), 'the card reaches the group')
const got = followups(s)
await s.fake.handlers.onAction({ chat: { key: 'g1', kind: 'group', title: 'Group One' }, sender: { id: 'u1', name: 'U1' }, data: 't:q:q1', messageId: 1 })
await s.flush()
const prompt = s.fake.sent.at(-1)
assert.equal(prompt.chat.key, 'g1')
assert.equal(prompt.forceReply, true, 'a group needs the reply box, or the unaddressed answer is dropped')
await gm(s, 'u1', 'Ship to Acme only')
assert.deepEqual(answerOf(got[0]), { id: 'q1', selected: [], custom: 'Ship to Acme only' })
})

test('ordinary chat text with no armed card is untouched', async (t) => {
const s = await start()
t.after(s.stop)
Expand Down Expand Up @@ -773,7 +793,7 @@ test('a second arm replaces the first, and a typed view answer reaches the Bot',
await press(s, 't:v:v1')
await press(s, 't:v:v1')
await s.flush()
assert.equal(s.fake.sent.filter(entry => entry.forceReply === true).length, 2)
assert.equal(s.fake.sent.filter(entry => entry.text === 'Type your answer and send it.').length, 2)
s.prompts.length = 0
await dm(s, 'u1', 'neither, ask me tomorrow')
assert.equal(s.prompts.length, 1, 'the typed text goes to the Bot once')
Expand Down
Loading