Skip to content

Commit

Permalink
unfurl and flip support (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
pzduniak committed Jun 3, 2019
1 parent c5f5d0d commit c624abc
Show file tree
Hide file tree
Showing 8 changed files with 397 additions and 46 deletions.
145 changes: 104 additions & 41 deletions README.md
Expand Up @@ -103,39 +103,47 @@ This code is also in [`demos/hello-world.js`](demos/hello-world.js), if you want
- [leaveChannel](#leavechannel)
- [Parameters](#parameters-3)
- [Examples](#examples-5)
- [list](#list)
- [Parameters](#parameters-4)
- [getUnfurlSettings](#getunfurlsettings)
- [Examples](#examples-6)
- [listChannels](#listchannels)
- [Parameters](#parameters-5)
- [setUnfurlSettings](#setunfurlsettings)
- [Parameters](#parameters-4)
- [Examples](#examples-7)
- [read](#read)
- [Parameters](#parameters-6)
- [loadFlip](#loadflip)
- [Parameters](#parameters-5)
- [Examples](#examples-8)
- [send](#send)
- [Parameters](#parameters-7)
- [list](#list)
- [Parameters](#parameters-6)
- [Examples](#examples-9)
- [createChannel](#createchannel)
- [Parameters](#parameters-8)
- [listChannels](#listchannels)
- [Parameters](#parameters-7)
- [Examples](#examples-10)
- [attach](#attach)
- [Parameters](#parameters-9)
- [read](#read)
- [Parameters](#parameters-8)
- [Examples](#examples-11)
- [download](#download)
- [Parameters](#parameters-10)
- [send](#send)
- [Parameters](#parameters-9)
- [Examples](#examples-12)
- [react](#react)
- [Parameters](#parameters-11)
- [createChannel](#createchannel)
- [Parameters](#parameters-10)
- [Examples](#examples-13)
- [delete](#delete)
- [Parameters](#parameters-12)
- [attach](#attach)
- [Parameters](#parameters-11)
- [Examples](#examples-14)
- [watchChannelForNewMessages](#watchchannelfornewmessages)
- [Parameters](#parameters-13)
- [download](#download)
- [Parameters](#parameters-12)
- [Examples](#examples-15)
- [watchAllChannelsForNewMessages](#watchallchannelsfornewmessages)
- [Parameters](#parameters-14)
- [react](#react)
- [Parameters](#parameters-13)
- [Examples](#examples-16)
- [delete](#delete)
- [Parameters](#parameters-14)
- [Examples](#examples-17)
- [watchChannelForNewMessages](#watchchannelfornewmessages)
- [Parameters](#parameters-15)
- [Examples](#examples-18)
- [watchAllChannelsForNewMessages](#watchallchannelsfornewmessages)
- [Parameters](#parameters-16)
- [Examples](#examples-19)
- [Chat Types](#chat-types)
- [ChatChannel](#chatchannel)
- [Properties](#properties-2)
Expand Down Expand Up @@ -163,25 +171,25 @@ This code is also in [`demos/hello-world.js`](demos/hello-world.js), if you want
- [OnError](#onerror)
- [Wallet](#wallet)
- [balances](#balances)
- [Examples](#examples-17)
- [Examples](#examples-20)
- [history](#history)
- [Parameters](#parameters-15)
- [Examples](#examples-18)
- [Parameters](#parameters-17)
- [Examples](#examples-21)
- [details](#details)
- [Parameters](#parameters-16)
- [Examples](#examples-19)
- [Parameters](#parameters-18)
- [Examples](#examples-22)
- [lookup](#lookup)
- [Parameters](#parameters-17)
- [Examples](#examples-20)
- [Parameters](#parameters-19)
- [Examples](#examples-23)
- [send](#send-1)
- [Parameters](#parameters-18)
- [Examples](#examples-21)
- [Parameters](#parameters-20)
- [Examples](#examples-24)
- [batch](#batch)
- [Parameters](#parameters-19)
- [Examples](#examples-22)
- [Parameters](#parameters-21)
- [Examples](#examples-25)
- [cancel](#cancel)
- [Parameters](#parameters-20)
- [Examples](#examples-23)
- [Parameters](#parameters-22)
- [Examples](#examples-26)
- [Wallet Types](#wallet-types)
- [Asset](#asset)
- [Properties](#properties-13)
Expand All @@ -200,14 +208,14 @@ This code is also in [`demos/hello-world.js`](demos/hello-world.js), if you want
- [Properties](#properties-19)
- [Team](#team)
- [addMembers](#addmembers)
- [Parameters](#parameters-21)
- [Examples](#examples-24)
- [Parameters](#parameters-23)
- [Examples](#examples-27)
- [removeMember](#removemember)
- [Parameters](#parameters-22)
- [Examples](#examples-25)
- [Parameters](#parameters-24)
- [Examples](#examples-28)
- [listTeamMemberships](#listteammemberships)
- [Parameters](#parameters-23)
- [Examples](#examples-26)
- [Parameters](#parameters-25)
- [Examples](#examples-29)

### Bot

Expand Down Expand Up @@ -356,6 +364,61 @@ bot.chat.listConvsOnName('team_name').then(async teamConversations => {

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<void>**

#### getUnfurlSettings

Gets current unfurling settings

##### Examples

```javascript
bot.chat.getUnfurlSettings().then(mode => console.log(mode))
```

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<UnfurlMode>**

#### setUnfurlSettings

Sets the unfurling mode
In Keybase, unfurling means generating previews for links that you're sending
in chat messages. If the mode is set to always or the domain in the URL is
present on the whitelist, the Keybase service will automatically send a preview
to the message recipient in a background chat channel.

##### Parameters

- `mode` **UnfurlMode** the new unfurl mode

##### Examples

```javascript
bot.chat
.setUnfurlMode({
mode: 'always',
})
.then(mode => console.log('mode updated!'))
```

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<void>**

#### loadFlip

Loads a flip's details

##### Parameters

- `conversationID` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** conversation ID received in API listen.
- `flipConversationID` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** flipConvID from the message summary.
- `messageID` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** ID of the message in the conversation.
- `gameID` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** gameID from the flip message contents.

##### Examples

```javascript
// check demos/es7/poker-hands.js
```

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<FlipSummary>**

#### list

Lists your chats, with info on which ones have unread messages.
Expand Down
89 changes: 89 additions & 0 deletions demos/es7/poker-hands.js
@@ -0,0 +1,89 @@
#!/usr/bin/env node
const Bot = require('../../index.js')
const Hand = require('pokersolver').Hand

const bot = new Bot()

const deck =
'2s,3s,4s,5s,6s,7s,8s,9s,Ts,Js,Qs,Ks,As,2c,3c,4c,5c,6c,7c,8c,9c,Tc,Jc,Qc,Kc,Ac,2d,3d,4d,5d,6d,7d,8d,9d,Td,Jd,Qd,Kd,Ad,2h,3h,4h,5h,6h,7h,8h,9h,Th,Jh,Qh,Kh,Ah'
const deckI2S = deck.split(',').reduce((prev, curr, ind) => {
prev[ind] = curr
return prev
}, {})

async function main() {
try {
const username = process.env.KB_USERNAME
const paperkey = process.env.KB_PAPERKEY
await bot.init(username, paperkey)
const info = bot.myInfo()
console.log(`Bot initialized with username ${info.username}.`)

console.log(`Listening for all messages...`)
await bot.chat.watchAllChannelsForNewMessages(
async msg => {
try {
console.log(msg)

if (msg.channel.membersType !== 'impteamnative') {
// Only react to direct messages.
console.log(`Invalid type - ${msg.channel.membersType}`)
return
}

if (msg.content.type === 'flip') {
let resolved
while (!resolved) {
try {
const flip = await bot.chat.loadFlip(
msg.conversationId,
msg.content.flip.flipConvId,
msg.id,
msg.content.flip.gameId
)
if (flip.phase === 2) {
resolved = true

if (flip.resultInfo.typ !== 3 || !flip.resultInfo.hands.find(x => x.target === 'me')) {
await bot.chat.send(msg.channel, {
body: `Invalid flip type! Please do a /flip cards 5 me instead.`,
})
return
}

const myHand = flip.resultInfo.hands.find(x => x.target === 'me')
const hand = Hand.solve(myHand.hand.map(card => deckI2S[card]))
await bot.chat.send(msg.channel, {
body: `You got ${hand.descr}.`,
})
}
} catch (err) {
console.log('flip fetch error', err)
}
}
return
}

await bot.chat.send(msg.channel, {
body: `Hi ${msg.sender.username}! Do a /flip cards 5 me and I'll tell what's in your poker hand!`,
})
} catch (err) {
console.error(err)
}
},
e => console.error(e)
)
} catch (error) {
console.error(error)
}
}

async function shutDown() {
await bot.deinit()
process.exit()
}

process.on('SIGINT', shutDown)
process.on('SIGTERM', shutDown)

main()

0 comments on commit c624abc

Please sign in to comment.