Skip to content

Instantly share code, notes, and snippets.

@spasiu
spasiu / qs.js
Created March 8, 2017 03:55
Quicksort in JavaScript
const qs = (compare, list) => {
if (list.length < 2) {
return list
}
const pivot = list[0]
const left = []
const right = []
for (const item of list.slice(1)) {
compare(pivot, item) ? left.push(item) : right.push(item)
@spasiu
spasiu / randomize_array.js
Created February 13, 2017 22:02
Randomize an array
function randomize(list, randomized=[]) {
if (list.length < 1) {
return randomized;
}
const index = Math.floor(Math.random()*list.length);
const pick = list[index];
const remaining = list.slice(0, index).concat(list.slice(index + 1));
return randomize(remaining, randomized.concat([pick]));
}
@spasiu
spasiu / client-schema.md
Last active March 14, 2017 15:43
Smooch Client info as of Feb 6th 2017 (updated March 14th)

client info schema

Client info is highly variable, but when available can be found in the following fields.

Field Type Description
sdkVersion optional String Available on "web", "ios", and "android" platforms
currentTitle optional String Available on "web" platform
currentUrl optional String Available on "web" platform
browserLanguage optional String Available on "web" platform
@spasiu
spasiu / init-message.html
Created January 18, 2017 00:06
Example of an embedded Smooch Web Messenger instance that opens in full width, and sends an initial message
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.sk-msg, .sk-msg-image {
font-size: 18px !important;
@spasiu
spasiu / initiate-contact-over-sms-with-smooch.md
Last active April 27, 2017 04:16
Pre-create a Smooch user and link them to SMS in order to initiate contact with a user.

Linking Users to SMS

Smooch provides a REST API endpoint for linking user's to SMS. Here are a couple ways you might want to use this API:

  • If you have a user's mobile number, you might want to initiate contact with them by SMS
  • If you're able to identify an existing user's mobile number, you might want to add SMS as an alternate channel

Below, we'll provide a recipe for creating a user, and initiating contact with them over SMS.

Generate an app scoped token

Before we can call the REST API we will need an app scoped token. If you're not sure how to do that yet, check out the authorization section of the docs.

@spasiu
spasiu / evented-message.md
Last active May 9, 2018 08:56
Use the Smooch Web Messenger and REST API to send messages to visitors of your Website when they take specific actions.

Easy evented chat messages for the Web

You'll need a Website, a Server, and a Smooch app.

Step 1: On the server

On your server expose a POST /events endpoint. It should accept an event type, and a Smooch user ID as JSON data. When this endpoint is called the server should call the Smooch REST API with an appropriate message for the user. Something like this:

curl https://api.smooch.io/v1/appusers/{{userId}}/messages \
     -X POST \
@spasiu
spasiu / hash.js
Created December 5, 2016 19:17
command line tool to generate hash from string
process.stdout.write(require('crypto').createHash('md5').update(process.argv[2]).digest("hex"));
@spasiu
spasiu / smooch_webhook_payload.json
Created November 25, 2016 20:06
Sample Smooch Webhook payload with Facebook as a client
{
"trigger": "message:appUser",
"app": {
"_id": "58384af1c88e9558007a6a91"
},
"messages": [
{
"authorId": "2f1c2db75a79a99116ac99d3",
"received": 1480104343.829,
"text": "hey",
@spasiu
spasiu / smooch_server.js
Last active November 29, 2016 22:18
A server for receiving user messages from Smooch and responding via the REST API.
'use strict';
// Imports
const express = require('express');
const bodyParser = require('body-parser');
const jwt = require('jsonwebtoken');
const superagent = require('superagent');
// Config
const PORT = process.env.PORT || 8000;
@spasiu
spasiu / beginner_smooch.md
Created November 23, 2016 17:43
how to start with Smooch and NodeJS

API Quick Start

Introduction

This quickstart will teach you to use Smooch's REST API, a Web messenger widget on a Website, and and some server-side code to create an auto response for when people send your business messages.

Prerequisites

You should be able to follow along in node.js, or another programming language; you should also have ngrok (which creates secure tunnels to localhost), or be otherwise able to deploy a server; and you should have already signed up for a Smooch account.

Setting up a Web server

First we'll set up a Web server: