import * as cloud from "@pulumi/cloud" ; import * as aws from "./aws" ; import { Digest } from "./digest" ; import * as mailgun from "./mailgun" ; import { poll } from "./poll" ; import * as salesforce from "./salesforce" ; import * as twitter from "./twitter" ; let sendEmail = mailgun . send ; let salesforceQueryAll = salesforce . queryAll ; let sendSESEmail = aws . sendEmail ; let salesforceInsert = salesforce . insert ; export function exampleTwitter1 ( ) { let tweets = twitter . search ( "pulumi" , "vscode" ) ; tweets . subscribe ( "tweetlistener" , async ( tweet ) => { await sendEmail ( { to : "cyrus@pulumi.com" , subject : `Tweets from ${ new Date ( ) . toDateString ( ) }` , body : `@${ tweet . user . screen_name }: ${ tweet . text }\n` , } ) ; } ) ; } export function exampleTwitter2 ( ) { console . log ( "Running Twitter example 2..." ) ; let tweets = twitter . search ( "pulumi" , "vscode" ) ; let digest = new Digest ( "tweetdigest" , tweets ) ; cloud . timer . daily ( "nightly" , { hourUTC : 7 } , async ( ) => { await digest . collect ( ) ; } ) ; digest . subscribe ( "digest" , async ( dailyTweets ) => { if ( dailyTweets . length === 0 ) { console . log ( "No new tweets..." ) ; return ; } console . log ( `Received ${ dailyTweets . length } new tweets.  Sending email...` ) ; let text = "Tweets:\n" ; for ( let tweet of dailyTweets ) { text += `@${ tweet . user . screen_name }: ${ tweet . text }\n` ; } await sendEmail ( { to : "cyrus@pulumi.com" , subject : `Tweets from ${ new Date ( ) . toDateString ( ) }` , body : text , } ) ; } ) ; } export function exampleSalesforce1 ( ) { let contactsStream = poll ( "contactspolling" , { minutes : 1 } , async ( timestamp ) => { if ( timestamp === undefined ) { timestamp = "2017-01-01T00:00:00.000Z" ; } let records = await salesforceQueryAll ( `SELECT Id,Name,LastModifiedDate FROM Contact WHERE LastModifiedDate > ${ timestamp }` ) ; let newTimestamp = records . reduce ( ( ts , record ) => { let newts = record [ "LastModifiedDate" ] ; return newts > ts ? newts : ts ; } , timestamp ) ; return { items : records , nextToken : newTimestamp , } ; } ) ; contactsStream . subscribe ( "contactlistener" , async ( contact ) => { console . log ( contact ) ; } ) ; } export function exampleSalesforce2 ( ) { let contacts = salesforce . query ( "contacts" , ( timestamp ) => `SELECT Id,Name,LastModifiedDate FROM Contact WHERE LastModifiedDate > ${ timestamp }` , "2017-01-01T00:00:00.000Z" , "LastModifiedDate" , ( a , b ) => a > b ? a : b ) ; contacts . subscribe ( "contactlistener" , async ( contact ) => { console . log ( contact ) ; } ) ; } export function exampleSalesforce3 ( ) { let contacts = salesforce . allObjectModifications ( "contacts" , "Contact" , "Id,Name" ) ; contacts . subscribe ( "contactlistener" , async ( contact ) => { console . log ( contact ) ; } ) ; } export function exampleSendSESEmail ( ) { let api = new cloud . API ( "sadsad" ) ; api . get ( "/" , async ( req , res ) => { try { await sendSESEmail ( { to : [ "luke@pulumi.com" ] , bcc : [ "inquiries@pulumi.com" ] , replyTo : [ "inquiries@pulumi.com" ] , source : "\"Pulumi\" <inquiries@pulumi.com>" , subject : "Hi from Pulumi" , bodyHtml : "Hello, welcome to <b>Pulumi</b>." , } ) ; res . json ( { } ) ; } catch ( err ) { res . status ( 500 ) . json ( err ) ; } } ) ; api . get ( "/insertSalesforce" , async ( req , res ) => { try { await salesforceInsert ( "Lead" , { Email : "lukehoban@gmail.com" , LastName : "Hoban" , Company : "Pulumi" } ) ; res . json ( { } ) ; } catch ( err ) { res . status ( 500 ) . json ( err ) ; } } ) ; api . publish ( ) . url . apply ( url => console . log ( `URL: ${ url }` ) ) ; }