DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Livecoding: A Door-Answering Slackbot

Livecoding: A Door-Answering Slackbot

Tired of getting up from your computer to see who's at the door. Try building your own Slackbot that can answer the door for you!

Swizec Teller user avatar by
Swizec Teller
·
May. 12, 17 · Web Dev Zone · Tutorial
Like (1)
Save
Tweet
3.37K Views

Join the DZone community and get the full member experience.

Join For Free


aaaaaalmost hacked together a slack bot that can answer the door. there’s one last step to figure out and then we are ready to rock.

buildings in the us, or at least in san francisco, have buzzers that connect to a phone rather than a purpose-built wall-mounted device. you set it up with your phone, and whenever someone wants to come visit, your phone rings, you say hello, and buzz them in.

it’s perfect for the home. it’s really annoying for anyone stuck with buzzer duty at the office.

a slack bot can do that for us. here’s the idea:

  • give the buzzer a twilio phone number.
  • build a small node.js server to answer the phone.
  • send a slack message with an “i am so and so” audio clip.
  • offer deny and let them in buttons.
  • anyone can buzz anyone in.
  • profit.

the profit part comes if slack lets me release this as a, say, $10/month service. we’ll see.

you can also play with it as an open source project on github . but it’s not really ready for prime-time yet. the code is a mess, and there’s no readme file to tell you how to set it up.

the bright side is that you can see all my api keys in the livecoding video. i’m going to change those. probably.

when twilio receives a phone call, it posts to our webhook on <server>/call . this is the start of our user experience. we reply with hello! state your name, then press anything . like this:

router.post('/call', function (req, res, next) {
    const caller = req.body.caller;
    const callsid = req.body.callsid;

    let twiml = new twilio.twimlresponse();

    twiml.say('hello! state your name, then press any key.', {voice: 'alice'});

    twiml.record({
        action: `/call/recording/${callsid}`,
        //transcribe: true,
        //transcribecallback: `/call/recording/${callsid}`,
        maxlength: 60
    });

    res.type('text/xml');
    res.send(twiml.tostring());
});

that twiml stuff constructs a response using twiml, a twilio-extended xml language. twilio can turn <say>text</say> commands into spoken dialogue using various voice synthesizers. you’ve gotta use proper punctuation though. it sounds weird otherwise.

we ask twilio to record a response and send the audio clip to our /call/recording/<id> api. in that api, we send a slack text and put the caller on hold.

router.post('/call/recording/:callsid', (req, res, next) => {
    const callsid = req.params.callsid;
    const twiml = new twilio.twimlresponse();
    const recordingurl = req.body.recordingurl;

    let data = {
        attachments: [{
            fallback: 'somebody is at the door',
            title: 'somebody is at the door',
            title_link: recordingurl,
            text: 'click link to hear the recording',
            callback_id: `door_open:${callsid}`,
            actions: [
                {
                    name: 'open_door',
                    text: 'let them in',
                    type: 'button',
                    value: 'open_door'
                },
                {
                    name: 'deny_access',
                    text: 'no.',
                    type: 'button',
                    value: 'deny_access'
                }
            ]
        }]
    };

    webslack.chat.postmessage('#bot-testing', '', data, () => {
        twiml.say('thank you. please hold.', {voice: 'alice'});
        twiml.pause(240);

        res.type('text/xml');
        res.send(twiml.tostring());
    });
});

we get the audio clip as a url, and we post to slack using their web client thingy. the real-time-messaging client can’t do complex messages.

this is what you get on slack:

image title

now comes the part i haven’t figured out yet:

  1. build the api for those slack buttons – it always throws an error.
  2. figure out how to continue sending to a twilio call outside of the original webhoook, if i know the original call id.

the first bit is just a matter of figuring out my config. slack gives clear instructions for doing that; i just gave up too soon.

the second bit, though… that’s hard. i can’t find any documentation for it, and i haven’t had any luck googling for it either.

gonna talk to twilio’s help team and finish this up next sunday. don’t forget to come watch.

Slack (software)

Published at DZone with permission of Swizec Teller, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Flask vs. Django: Which Python Framework to Choose?
  • 10 Programming Habits a Web Developer Should Embrace
  • My Sentiments, Erm… Not Exactly
  • How to Translate Value to Executives Using an Outcome-Driven Mindset

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo