Skip to content

Commit

Permalink
Merge pull request #15 from valgaze/chipfix
Browse files Browse the repository at this point in the history
add feature to make chips disappear on tap
  • Loading branch information
valgaze authored Nov 16, 2021
2 parents 49e874e + c6176ef commit 4c876fa
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 15 deletions.
Binary file added docs/assets/chip_tap_disappear.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/chip_tap_persist.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 30 additions & 3 deletions docs/superpowers.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,25 @@ export default {
// $bot.send({markdown: snippet, roomId:trigger.message.roomId, text: 'Your client does not render markdown :('}) // send to a specific room
// $bot.send({markdown: snippet, toPersonEmail:'[email protected]', text: 'Your client does not render markdown :('}) // send to a specific person

// ## 3) Conversation "chips"

// ## 3) Save data between conversation "runs"
// Set all chips to disappear after tap (defaults to false)
$bot.setChipsConfig({disappearOnTap: true})

// Send chip with custom handler
const customChip = {
label: 'custom chip',
handler(bot:BotInst, trigger: Trigger) {
$bot.sendSnippet(trigger, `**The 'custom chip' was tapped** `)
$bot.$trigger('chips', trigger) // re-render chips
}
}

// Add optional title to chips
$bot.sendChips(['hey', 'ping', '$', 'pong', customChip], 'These chips will disappear on tap')


// ## 4) Save data between conversation "runs"

interface SpecialUserData {
specialValue: string;
Expand All @@ -98,14 +115,14 @@ export default {
$bot.deleteData('userData')
}

// ## 4) Integrate with 3rd-parties: $bot.get, $bot.post, etc
// ## 5) Integrate with 3rd-parties: $bot.get, $bot.post, etc

// ex. get external data
// Opts are axios request config (for bearer tokens, proxies, unique config, etc)
const res = await $bot.get('https://randomuser.me/api/')
bot.say({markdown: $bot.snippet(res.data)})

// ## 4) Files & attachments
// ## 6) Files & attachments

// Send a local file
// Provide a path/filename, will be attached to message
Expand Down Expand Up @@ -186,3 +203,13 @@ export default {
helpText: `Special handler that's fired when the user uploads a file to your bot (by default supports json/csv/txt)`
}
```

## Chips Disappear


![image](./assets/chip_tap_persist.gif)

![image](./assets/chip_tap_disappear.gif)


## Chips Persist
20 changes: 13 additions & 7 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios, { Method, AxiosRequestConfig, AxiosResponse } from 'axios'
import { createReadStream, unlink, writeFileSync } from 'fs'
import { SpeedyCard, chipLabel } from './index'
import { SpeedyCard, chipLabel, chipConfigLabel } from './index'
import { BotInst, Trigger, ToMessage, Message } from './framework'
import { log, loud } from './logger'
import { resolve } from 'path'
Expand Down Expand Up @@ -478,7 +478,7 @@ export class $Botutils {
})
}

public async sendChips(chipPayload: ChipPayload, heading?:string) {
public async sendChips(chipPayload: ChipPayload, title = '') {
// Register 'n Render chips
const newChips:Chip[] = []
if (Array.isArray(chipPayload)) {
Expand All @@ -499,7 +499,6 @@ export class $Botutils {
}
})
}

const chips = await this.getData(chipLabel) || []
const keys = newChips.map(({label}) => label)
const writeChips = chips.filter(chip => !keys.includes(chip.label)).concat(newChips)
Expand All @@ -510,10 +509,14 @@ export class $Botutils {
return label
})
const card = new SpeedyCard().setChips(labels)
if (heading) {
card.setSubtitle(heading)
if (title) {
card.setSubtitle(title)
}
this.botRef.sendCard(card.render(), heading ? heading : ' ')
this.botRef.sendCard(card.render(), title)
}

public async setChipsConfig(config: ChipConfig) {
return this.saveData(chipConfigLabel, config)
}

public async $trigger(text: string, trigger: Trigger) {
Expand Down Expand Up @@ -545,4 +548,7 @@ export interface Chip {
// args?: () => Promise<string[]>
}

export type ChipPayload = string[] | Chip[] | (string | Chip)[]
export type ChipPayload = string[] | Chip[] | (string | Chip)[]
export interface ChipConfig {
disappearOnTap?: boolean;
}
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export { FrameworkInst, BotHandler,WebhookHandler, Message, ToMessage, BotInst,
export { bad, help, ascii_art, log, good, askQuestion, loud } from './logger'
// helpers
export { fillTemplate, pickRandom, snippet, Storage, Locker, $ } from './helpers'
export { Chip, ChipPayload, ChipConfig } from './helpers'
// make adaptive cards less painful w/ base templates
export { SpeedyCard } from './cards'
export const placeholder = '__REPLACE__ME__'
export const chipLabel = '___$CHIPS'
export const chipLabel = '___$CHIPS'
export const chipConfigLabel = `${chipLabel}_$config`
16 changes: 12 additions & 4 deletions src/speedybot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import {
BotInst,
Trigger,
WebhookHandler,
Message
} from "./framework";
import { ValidatewebhookUrl, pickRandom, snippet } from "./helpers";
import { placeholder, chipLabel, ascii_art, SpeedyCard } from "./";
import { placeholder, chipLabel, chipConfigLabel, ChipConfig, ascii_art, SpeedyCard } from "./";
// TODO: make peer dependency
import Botframework from "webex-node-bot-framework";
import BotWebhook from "webex-node-bot-framework/webhook";
Expand Down Expand Up @@ -185,8 +186,8 @@ import {
this.addHandler(this.defaultHealthcheck());
}

const rootSubmitHandler = async (bot, trigger) => {
const getData = async (key: string): Promise<any | null> => {
const rootSubmitHandler = async (bot: BotInst, trigger: Trigger) => {
const getData = async <T extends unknown = any>(key: string): Promise<T | null> => {
return new Promise(async (resolve) => {
try {
const res = await bot.recall(key);
Expand All @@ -196,6 +197,13 @@ import {
}
});
};

const { disappearOnTap } = await getData<ChipConfig>(chipConfigLabel) || { disappearOnTap: false }
if (disappearOnTap) {
const msgId = trigger.attachmentAction.messageId
await bot.censor(msgId as string)
}

const registeredChips = (await getData(chipLabel)) || [];
const { chip_action } = trigger.attachmentAction
? trigger.attachmentAction.inputs
Expand All @@ -213,7 +221,7 @@ import {
text: trigger.attachmentAction.inputs.chip_action,
}
// HACK: pass the button-tap value through the handler system
bot.framework.onMessageCreated(payload);
bot.framework.onMessageCreated(payload as Message);
}
}
}
Expand Down

0 comments on commit 4c876fa

Please sign in to comment.