Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / qs.py
Created March 8, 2017 04:10
Quicksort in python
def qs(compare, items):
if len(items) < 2: return items
pivot = items[0]
left = []
right = []
for item in items[1:]:
if compare(pivot, item): left.append(item)
else: right.append(item)
@spasiu
spasiu / markdown-file-splitter.js
Created March 9, 2017 20:42
Script to split Markdown files
const fs = require('fs');
const PATH = process.argv[2] || __dirname;
const LEVEL = parseInt(process.argv[3] || 1);
const TARGET = process.argv[4];
const DELETE = process.argv[5];
fs.readdirSync(PATH)
.filter(isOptionalTarget)
.filter(isMarkdownFile)
@spasiu
spasiu / smooch-account-jwt.js
Created March 10, 2017 17:58
Get account scope jwt
const authToken = require('jsonwebtoken').sign({ scope: 'account' }, 'your_secret_key', {
header: {
typ: 'JWT',
kid: 'your_key_id',
alg: 'HS256'
}
});
@spasiu
spasiu / app.js
Created March 16, 2017 01:49
real time app with polling example for JS101
const catPut = document.getElementById('cat-input');
const catList = document.getElementById('cat-list');
document.getElementById('cat-button').onclick = () => {
const cat = catPut.value;
superagent.post('/cats').send({ cat }).end((err, res) => {
err ? alert(err.message) : catPut.value = '';
});
}