GetMessage

Usage

Import the Any declaration to interact with the GetMessage function to retrieve the specific message of your choice.

import { messages } from 'aleph-sdk-ts';

// Or used optimized import
import { GetMessage } from 'aleph-sdk-ts/dist/messages/any';

Then call the GetMessage function, with the required parameters as follow:

(async () => {
    const res = await GetMessage({
        hash: "87e1e2ee2cbe88fa2923042b84b2f9c69410005ca7dd40193838bf9bad18e12c",
    });
})();

Parameters

ParameterDescription

hash - required string

The hash of your message.

channel - optional string

Channel of the message.

messageType - optional MessageType

The type of message you are waiting for: post, aggregate, forget, store, program.

APIServer - optional string

The APIServer to use to resolve the request. The default one is: https://api2.aleph.im

Result

When you are fetching a message with GetMessage, there are 3 ending scenarios:

A. Keep default

By default, the GetMessage method will return a BaseMessage object. This will allow you to get with auto-completion generic information about the message like the sender, date, chain, ... that does not depend on its type (Post, Store, Aggregate, ...).

(async () => {
    const res = await GetMessage({
        hash: "87e1e2ee2cbe88fa2923042b84b2f9c69410005ca7dd40193838bf9bad18e12c",
    });
    
    res.//sender,chain,signature,size,time,hash_type,item_hash,type,...
})();

B. Use templating

The GetMessage allows you to override its default templating return type. This means if you know the type of message you are looking for, you can add your type to it like this:

import { ForgetMessage } from 'aleph-sdk-ts/dist/messages/message';

(async () => {
    const res = await GetMessage<ForgetMessage>({
        hash: "...",
    });

    // res if now of type ForgetMessage
    res.content.//hashes,reason
})();

This method is perfom an override of the type without further verification. This can lead as unexptected runtime errors if the given type is not the same as the type fetched by the Aleph API.

C. Use Typeguards

This is the safest way to handle messages retrieved by GetMessage or GetMessages. You can find more informaton about them on their dedicated page.

import { any } from 'aleph-sdk-ts/dist/messages';

(async () => {
    const res = await any.GetMessage({
        hash: "...",
    });

    if (any.is.Store(res))
        res.content.//item_hash,... res is now StoreMessage type.
})();

Errors handling

In case the provided hash does not refer to any message stored on the Aleph network, GetMessage will throw an error as the following:

No messages found for: <The Hash you provide>

Last updated