メニュー

Expand
ページを評価:

Runtimeクライアント

The Twilio Runtime Client provides a simple and intuitive way of orchestrating the various parts of the Twilio Runtime. Using the Runtime Client, developers can reference other Functions to better organize their code, access configuration and files stored in Assets, and shared app state via Twilio Sync.

The Runtime Client is accessed in a Function by referencing Runtime.

Runtime.getAssets()

The getAssets method returns an object that contains the names each private Asset in a Service. Each Asset name serves as the key to an Asset object that contains the path to that Asset, as well as an open method that can be conveniently used to access its contents. These paths can be used to retrieve files served on Twilio Assets.

For example, executing Runtime.getAssets() could return an object with the following private Assets:

{
  '/names.json': {
    path: '/var/task/files/ZNdad14da2e70d2533f640cf362fec0609',
    open: [Function: open]
  },
  '/rickroll.mp3': {
    path: '/var/task/files/ZNdfbfaf15a02e244fa11337548dabd9d0',
    open: [Function: open]
  },
  '/helper-method.js': {
    path: '/var/task/files/ZN5d6d933785a76da25056328a5764d49b',
    open: [Function: open]
  }
}

getAssets() only returns private Assets. Public and protected assets can be accessed via their publicly facing urls without the need for calling getAssets().

Note that an Asset such as names.json will be returned with a key of '/names.json'. To correctly retrieve the Asset and its path, the leading / and extension must be part of the key used to access the object returned by Runtime.getAssets().

たとえば、次のようになります。 Runtime.getAssets()['/names.json'].path

Assetのプロパティー

プロパティ Type 概要
path string String specifying the location of the private Asset
open function Convenience method that returns the contents of the file from path in utf8 encoding

サンプル

If you would like to include a JavaScript module that isn't available on npm, the best way to do so is to upload the module as a private Asset, then use getAssets in order to require the module as shown in the Load a module from an asset code example.

        
        
        
        Assetのファイルパスの取得方法の例

        Assetのパスを取得する

        Assetのファイルパスの取得方法の例
              
              
              
              Assetに保存されたサードパーティーのライブラリーを読み込む方法についての例

              Assetからモジュールを読み込む

              Assetに保存されたサードパーティーのライブラリーを読み込む方法についての例
                    
                    
                    
                    Leverage the built-in open method for convenience

                    Assetのコンテンツを読み込む

                    Leverage the built-in open method for convenience
                          
                          
                          
                          Directly read the contents of an Asset using filesystem methods

                          Assetのコンテンツを読み込む

                          Directly read the contents of an Asset using filesystem methods

                          Runtime.getFunctions()

                          The getFunctions method returns an object that contains the names of every Function in the Service. Each Function name serves as the key to a Function object that contains the path to that Function. These paths can be used to import code from other Functions and to compose code hosted on Twilio Functions.

                          For example, executing Runtime.getFunctions() could return an object with the following Functions:

                          {
                            'sms/reply': {
                              path: '/var/task/handlers/ZNdad14da2e70d2533f640cf362fec0609.js',
                            },
                            'helper': {
                              path: '/var/task/handlers/ZNdfbfaf15a02e244fa11337548dabd9d0.js',
                            },
                            'example-function': {
                              path: '/var/task/handlers/ZN5d6d933785a76da25056328a5764d49b.js',
                            },
                          }

                          Note that, unlike an Asset, a Function such as sms/reply.js will be returned with a key of "sms/reply". To correctly retrieve the Function and its path, do not include characters such as a leading slash or the .js extension in the key used to access the object returned by Runtime.getFunctions().

                          たとえば、次のようになります。 Runtime.getFunctions()["sms/reply"].path

                          Function Properties

                          プロパティ Type 概要
                          path string Functionの場所を指定する文字列です

                          サンプル

                                
                                
                                
                                Functionのファイルパスの取得方法の例

                                Functionのパスを取得する

                                Functionのファイルパスの取得方法の例

                                Include a private Function in another Function

                                Private Functions are a great way to store methods that may be resused between your other Functions. For example, lets say we have a private Function called Zoltar that exports a fortune-generating method, ask:

                                      
                                      
                                      
                                      Implement Zoltar and define an ask method

                                      Define a private helper Function

                                      Implement Zoltar and define an ask method

                                      You could then access this private method by using Runtime.getFunctions() to get the path for the Zoltar Function, import Zoltar as a JavaScript module using require, and then access the ask method as in the following code sample:

                                            
                                            
                                            
                                            Functionからのコードのインクルード方法の例

                                            Functionからのコードのインクルード

                                            Functionからのコードのインクルード方法の例

                                            Runtime.getSync(options?)

                                            We’ve made it convenient for you to access the Sync REST API from Functions. Use the Runtime Client to access any of Sync’s real-time data primitives and store information between Function invocations. The same data can be accessed using the Sync API library, making Sync from Functions the perfect way to update your real-time apps and dashboards.

                                            The Runtime Client provides a wrapper around the Twilio REST API Helper for Twilio Sync. By default, calling Runtime.getSync() will return a Sync Service object that has been configured to work with your default Sync Instance.

                                            For added convenience and less typing, the following methods returned from getSync are renamed from their usual name in the Node.js SDK, as you will see in the examples.

                                            Default method name Method name in Runtime
                                            syncMaps maps
                                            syncLists lists

                                            Arguments

                                            getSync optionally accepts a configuration object with the following properties.

                                            パラメーター Type 概要
                                            serviceName string String specifying either the serviceSid or uniqueName of the Sync Service to connect to. Defaults to default.

                                            サンプル

                                                  
                                                  
                                                  
                                                  既定のSync Serviceインスタンスを取得する方法の例

                                                  既定のSync Serviceインスタンスを取得する

                                                  既定のSync Serviceインスタンスを取得する方法の例
                                                        
                                                        
                                                        
                                                        SIDで指定されたSync Serviceインスタンスを取得するためRuntimeクライアントを使用する方法の例

                                                        既存のSync Serviceインスタンスを取得する

                                                        SIDで指定されたSync Serviceインスタンスを取得するためRuntimeクライアントを使用する方法の例
                                                              
                                                              
                                                              
                                                              RuntimeクライアントでSync内のMapを作成する方法の例

                                                              Runtimeクライアントを使用してSync Mapを作成する

                                                              RuntimeクライアントでSync内のMapを作成する方法の例
                                                                    
                                                                    
                                                                    
                                                                    RuntimeクライアントでSync Mapにエントリーを追加する方法の例

                                                                    RuntimeクライアントでSync Mapにエントリーを追加する

                                                                    RuntimeクライアントでSync Mapにエントリーを追加する方法の例
                                                                          
                                                                          
                                                                          
                                                                          RuntimeクライアントでSync Listを作成する方法の例

                                                                          RuntimeクライアントでSync Listを作成する

                                                                          RuntimeクライアントでSync Listを作成する方法の例
                                                                                
                                                                                
                                                                                
                                                                                RuntimeクライアントでSync Listに追加(アペンド)する方法の例

                                                                                RuntimeクライアントでSync Listに追加(アペンド)する

                                                                                RuntimeクライアントでSync Listに追加(アペンド)する方法の例
                                                                                      
                                                                                      
                                                                                      
                                                                                      RuntimeクライアントでSync Documentを作成する方法の例

                                                                                      RuntimeクライアントでSync Documentを作成する

                                                                                      RuntimeクライアントでSync Documentを作成する方法の例
                                                                                            
                                                                                            
                                                                                            
                                                                                            Example of creating a Sync Map and adding data to it in the same Function execution

                                                                                            Perform multiple Sync actions in a Function

                                                                                            Example of creating a Sync Map and adding data to it in the same Function execution
                                                                                            ページを評価:

                                                                                            ヘルプが必要ですか?

                                                                                            We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Community Forums or browsing the Twilio tag on Stack Overflow.

                                                                                                  
                                                                                                  
                                                                                                  

                                                                                                  フィードバックくださりありがとうございます!

                                                                                                  We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

                                                                                                  Sending your feedback...
                                                                                                  🎉 Thank you for your feedback!
                                                                                                  Something went wrong. Please try again.

                                                                                                  Thanks for your feedback!

                                                                                                  Refer us and get $10 in 3 simple steps!

                                                                                                  ステップ1

                                                                                                  Get link

                                                                                                  Get a free personal referral link here

                                                                                                  ステップ2:

                                                                                                  Give $10

                                                                                                  Your user signs up and upgrade using link

                                                                                                  ステップ3

                                                                                                  Get $10

                                                                                                  1,250 free SMSes
                                                                                                  OR 1,000 free voice mins
                                                                                                  OR 12,000 chats
                                                                                                  OR more