Skip to content

Instantly share code, notes, and snippets.

@spasiu
spasiu / main.clj
Created March 10, 2020 08:03
fizzbuzz
(ns learnclojure.fizzbuzz)
(defn fun [x]
(if
(and (= (mod x 3) 0) (= (mod x 5) 0))
"fizzbuzz"
(if
(= (mod x 3) 0)
"fizz"
(if
@spasiu
spasiu / convert_conversation_json_to_csv.js
Last active April 12, 2020 20:13
Fetch all log events in browser
const { parse } = require('json2csv');
const fs = require('fs');
const data = require('./conversations.json');
const messages = [];
for (const entry of data) {
for (const message of entry.messages) {
messages.push(Object.assign({
conversation_id: entry.conversation._id,
role: message.role,
@spasiu
spasiu / log_script.js
Created December 9, 2019 02:14
script to collect log events from SunCo dashboard
const get = (appId, before) => fetch(`https://app.smooch.io/webapi/apps/${appId}/logs?before=${before}`, {"credentials":"include","headers":{"accept":"application/json","accept-language":"en-US,en;q=0.9,fr;q=0.8","content-type":"application/json","sec-fetch-mode":"cors","sec-fetch-site":"same-origin","x-requested-with":"XMLHttpRequest"},"referrer":`https://app.smooch.io/apps/${appId}/logs`,"referrerPolicy":"no-referrer-when-downgrade","body":null,"method":"GET","mode":"cors"});
async function getFailedMessages(appId, ts, failedMessages=[]) {
console.log(ts, failedMessages.length);
const data = await get(appId, ts).then(res => res.json());
const messages = data.events
.filter(event => event.type === 'webhook')
.filter(event => event.status === 'failed')
.filter(event => event.details.trigger === 'message:appUser')
@spasiu
spasiu / script.js
Last active November 19, 2019 20:56
Script to rename a CSV full of Smooch apps
/*
How to:
Run this script with node and pass filepath and username + password as command line args:
node script.js "./YOUR_CSV_FILE" ACCOUNT_KEY ACCOUNT_SECRET
e.g.,
node script.js "./apps.csv" act_5964f00fc26cb5516693ebf5 cUQIis8kGtVZNg6eWoZrvk...
@spasiu
spasiu / log_script.js
Last active October 15, 2019 17:09
script for collating all Smooch logs from the dashboard
/*
Run this script by logging into your Smooch dashboard,
navigating to the app for which you want to collect logs,
and pasting the script into the JavaScript console.
The output is a global variable called `results`.
*/
async function getLogs(ts=undefined, hasMore=true, events=[]) {
const appId = location.pathname.split('/')[2];
ts = ts || Date.now() / 1000;
@spasiu
spasiu / index.js
Created October 8, 2019 17:22
append data to new Zendesk tickets
/**
* What is the problem we're trying to solve?
* Sometimes a business needs additional information to be part of the record in Support.
* Solution: use a service to handle events sent from Zendesk via a target, parse some info from the event and insert a comment into the ticket with some additional context.
* The real meat and potatoes, which you have to developm is what happens between receiving the event and inserting the comment.
*/
// imports
const bodyParser = require('body-parser');
const express = require('express');
@spasiu
spasiu / logging_example.js
Created September 30, 2019 13:25
Example of handling delivery events
const express = require('express');
const bodyParser = require('body-parser');
const Smooch = require('smooch-core');
const APP_ID = '';
const KEY_ID = '';
const SECRET = '';
const outboundMessageLogs = [];
@spasiu
spasiu / main.py
Last active September 6, 2019 19:07
end to end Webhooks example in Python 3
from flask import Flask
app = Flask(__name__)
@app.route('/', methods=['HEAD'])
def head():
return 'OK'
@app.route('/events', methods=['POST'])
def post():
return 'OK'
@spasiu
spasiu / increment.js
Last active July 7, 2019 11:32
Simple components and unidirectional data flow
// `createElement` and `createComponent` comprise the entire framework
const createElement = elementName => (props={}, children=[]) => {
const element = document.createElement(elementName);
Object.assign(element, props);
for (const child of children) {
element.appendChild(child);
}
@spasiu
spasiu / get_ids_tail_call.js
Last active July 7, 2019 10:33
Reverse engineer Smooch dashboard logs API to retrieve list of app user IDs (run from browser console)
async function getIds(ts, ids = []) {
const id = location.pathname.split('/')[2];
const query = ts ? `?before=${ts}` : '';
const url = `https://app.smooch.io/webapi/apps/${id}/logs${query}`;
const response = await fetch(url, {
"credentials": "include",
"headers": {
"accept": "application/json",
"accept-language": "en-US,en;q=0.9",