Ada - OpenAI and ScriptRunner Connect powered Apple shortcut


Get Started

Not the template you're looking for? Browse more.

About the template


This template accepts a question submitted by an Apple shortcut, forwards it from ChatGPT and relies back the answer. Get started to learn more.

About ScriptRunner Connect


What is ScriptRunner Connect?

ScriptRunner Connect is an AI assisted code-first (JavaScript/TypeScript) integration platform (iPaaS) for building complex integrations and automations.

Can I try it out for free?

Yes. ScriptRunner Connect comes with a forever free tier.

Can I customize the integration logic?

Absolutely. The main value proposition of ScriptRunner Connect is that you'll get full access to the code that is powering the integration, which means you can make any changes to the the integration logic yourself.

Can I change the integration to communicate with additional apps?

Yes. Since ScriptRunner Connect specializes in enabling complex integrations, you can easily change the integration logic to connect to as many additional apps as you need, no limitations.

What if I don't feel comfortable making changes to the code?

First you can try out our AI assistant which can help you understand what the code does, and also help you make changes to the code. Alternatively you can hire our professionals to make the changes you need or build new integrations from scratch.

Do I have to host it myself?

No. ScriptRunner Connect is a fully managed SaaS (Software-as-a-Service) product.

What about security?

ScriptRunner Connect is ISO 27001 and SOC 2 certified. Learn more about our security.

Template Content


README

Scripts

TypeScriptProcessAdaRequest
Sync HTTP Event

README


๐Ÿ–Š๏ธ Setup

  1. Set up the generic connector for OpenAI API:
    • Go to OpenAI's API keys page, create a new secret key, and copy the generated API key.
    • In your generic connector, add a custom header with the following details:
      • URL: https://api.openai.com
      • Header name: Authorization
      • Header value: Bearer YOUR_OPENAI_API_KEY
  2. Save the Sync HTTP event listener, then copy the URL provided.

๐Ÿš€ Using the template

  1. Download the Ada shortcut.
  2. Set up the Ada shortcut:
    • In the first text box, paste the Sync HTTP Event listener URL.
    • In the second text box, paste the AUTH_KEY configured via Parameters. (Update the key as desired).
  3. Run the shortcut and ask a question. If everything is set up correctly, you'll get a response.

API Connections


TypeScriptProcessAdaRequest

import { HttpEventRequest, HttpEventResponse, buildJSONResponse } from '@sr-connect/generic-app/events/http';
import OpenAI from './api/openai';

/**
 * When invoked, this function accepts the question, forwards it to OpenAI and replies with the answer.
 */
export default async function (event: HttpEventRequest, context: Context): Promise<HttpEventResponse> {
    try {
        if (context.triggerType === 'MANUAL') {
            console.error('This script is designed to be triggered externally or manually from the Event Listener. Please consider using Event Listener Test Event Payload if you need to trigger this script manually.');
            return;
        }

        const { API_KEY } = context.environment.vars as EnvVars;

        // Check if the incoming request has approriate API key
        if (event.body.auth !== API_KEY) {
            return buildJSONResponse({
                response: "Sorry, your API key isn't working. Might want to double check that."
            });
        }

        // Extract the question
        const question = event.body.prompt;
        // Log out the question
        console.log(`Question: ${event.body.prompt}`);

        // Forward the question to ChatGPT
        const response = await OpenAI.fetch('/chat/completions', {
            method: "POST",
            body: JSON.stringify({
                "model": "gpt-4-1106-preview",
                "messages": [
                    {
                        "role": "system",
                        "content": `You are a helpful assistant. Be kind and courteous and respond respectfully. Be brief, as your response will be broadcast on a speaker.`
                    },
                    {
                        "role": "user",
                        "content": question
                    }
                ]
            })
        });

        // Extract the response message
        const responseMessage = await response.json<OpenAIResponse>();

        // Check if the message contains an error
        if (responseMessage.error) {
            // If so, then throw an error
            throw new Error(responseMessage.error.message);
        }

        // Extract the answer
        const answer = responseMessage.choices.map(ch => ch.message.content).join(' ');

        // Finally return the answer
        return buildJSONResponse({
            response: answer
        })
    } catch (e) {
        // If error was thrown, log it out
        console.log('Error while processing request', e);

        // And respond appropriately
        return buildJSONResponse({
            response: `Something went wrong while asking your question`
        });
    }
}

interface EnvVars {
    readonly API_KEY: string;
}

interface OpenAIResponse {
    choices: {
        message: Message;
    }[];
    error?: {
        message: string;
    }
}

interface Message {
    role: string;
    content: string;
}
Documentation ยท Support ยท Suggestions & feature requests