src/bot.js
/*
   * Initializes the Cloud Pub/Sub Subscriber,
   * establishes connection to the Subscription
   * and sets up handler to handle events.
   */
Initializes the Cloud Pub/Sub Subscriber,
establishes connection to the Subscription
and sets up handler to handle events.
Initializes the Cloud Pub / Sub Subscriber establishes connection to the Subscription and sets up handler to handle events .
startPubSubClient() {
    const pubsub = PubSub();
    this.robot.logger.info(`Connecting to Pub/Sub subscription - ${this.subscriptionName}`);
    const subscription = pubsub.subscription(this.subscriptionName);
    const messageHandler = (pubsubMessage) => {
      this.robot.logger.debug(`Received message ${pubsubMessage.id}:`);
      this.robot.logger.debug(`\tData: ${pubsubMessage.data}`);
      const dataUtf8encoded = Buffer.from(pubsubMessage.data, 'base64').toString('utf8');
      let event;
      try {
        event = JSON.parse(dataUtf8encoded);
      } catch (ex) {
        logging.warn('Bad request');
        pubsubMessage.ack();
        return;
      }
      this.onEventReceived(event, null);
      pubsubMessage.ack();
    };
    subscription.on(`message`, messageHandler);
  }
startPubSubClient ( ) { const pubsub = PubSub ( ) ; this . robot . logger . info ( ` ${ this . subscriptionName } ` ) ; const subscription = pubsub . subscription ( this . subscriptionName ) ; const messageHandler = ( pubsubMessage ) => { this . robot . logger . debug ( ` ${ pubsubMessage . id } ` ) ; this . robot . logger . debug ( ` \t ${ pubsubMessage . data } ` ) ; const dataUtf8encoded = Buffer . from ( pubsubMessage . data , 'base64' ) . toString ( 'utf8' ) ; let event ; try { event = JSON . parse ( dataUtf8encoded ) ; } catch ( ex ) { logging . warn ( 'Bad request' ) ; pubsubMessage . ack ( ) ; return ; } this . onEventReceived ( event , null ) ; pubsubMessage . ack ( ) ; } ; subscription . on ( ` ` , messageHandler ) ; }
Initializes the Cloud Pub/Sub Subscriber,
 establishes connection to the Subscription
 and sets up handler to handle events.
Initializes the Cloud Pub / Sub Subscriber establishes connection to the Subscription and sets up handler to handle events .
// Create an event handler to handle messages. // "Ack" (acknowledge receipt of) the message. // Listen for new messages until timeout is hit.