How to use "cowsay" project in my project?

I’m afraid this is a newbie question, but please let me know.
How can I write the code to use “cowsay canister” in my project file for “Candid UI” ?
I’ve tried everything, but it doesn’t work. If you could write a sample, it would be helpful.Thank you!

2 Likes

: D Great to see the cow’s getting out and about!

You’ll want to follow this syntax for calling to external canisters at the moment: Calling another canister's methods - #2 by Ori

Where you define the canister as an actor with a matching signature to the canister you’re calling (or subtype of). You can find the signature on its listing: https://k7gat-daaaa-aaaae-qaahq-cai.ic0.app/listing/cowsay-10237/7igbu-3qaaa-aaaaa-qaapq-cai

So for cowsay you can build the call as follows:

actor {
    public func main() : async Text {
        let cowsay = actor(“7igbu-3qaaa-aaaaa-qaapq-cai”): actor { cowsay: (Text) -> async Text };
        return await cowsay.cowsay(“hello”);
    };
};

And a Motoko playground example is here for you:
https://m7sm4-2iaaa-aaaab-qabra-cai.raw.ic0.app/?tag=1759972232

7 Likes

The vision was always that you could just write

import CowSay "ic:7igbu-3qaaa-aaaaa-qaapq-cai";

in your Motoko file, and the development tools would do the rest for you (in particular, fetch that canister’s Candid file to find out the type of CowSay), but this feature never had highest priority, would have required cross-component coordination (moc and dfx, maybe replica) and there were opiniated differences on how to best do that under the hood, so it never happened. But the work-around pointed out by Ori is good enough™ for many cases.

(In fact, the Motoko compiler in moc already implements all that’s needed, as specified here. So the Motoko playground and/or dfx could add that feature on their side, and it would “just work”.)

2 Likes

Thank you, Ori.
I didn’t know it was already up. I should have done a little more searching. It worked.

1 Like

I see, it doesn’t work with javascript templates for importing. Thanks a lot.

Is it possible to call a public canister from within the local environment? When I try the example above and run the canister locally, I get the error “Canister 7igbu-3qaaa-aaaaa-qaapq-cai does not exist”. I assume that this is because I’m running on localhost and not on IC?

Not at the moment, see here: