1

I'm a noob at this, so excuse me if this is a ludicrous question..

I have a ReactJS app that interacts with a smart contract via web3. At a certain point, some data gets stored to the contract, by calling a function in the contract. Just after this data is stored, I would like to be able to call a js script, which uses Twilio to send an SMS. This script only seems to work locally when called with 'node send_sms.js' in the terminal. The script works when called like this. But cannot be imported into the React Component file for some reason. I think it's because it cannot be run from the client.

I had been attempting for some time to call this script from within the React component(inside an onSubmit function which is executed due to a Button press), however, it seems that I cannot even import anything to do with this script into the Components file.

Is there a way to call it from solidity, or to trigger its execution with node?

2

So one way to do it is with event. You can reaf more about it at here: https://solidity.readthedocs.io/en/v0.5.3/contracts.html#events.

So basically you just have to emit an event with the value, address and then you listen to that event on backend server and then your javascript file will execute upon that event. You can use this web3: https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#contract-events

P/S: Correct me if i'm wrong but if you want to send sms to a customer you will need a database that store your customer phone number then what is the point of using decentralized at the first place you can easily do it with centralized

  • I am storing user info on the blockchain. I simply want to send an sms to the user after this occurs. – Totem Mar 8 at 16:01
  • @Totem if you are storing user info like phone number on blockchain then i dont think blockchain technology is good for you, centralized can do even better because decentralized is for application that's public and share with everyone, then nobody want to public their phone number so that everybody know that's why they trust centralized to do what they promise and you can't send sms from blockchain just like Lauri's answer. TL;DR dont use blockchain to send sms – haxerl Mar 8 at 16:35
1

Just to make sure: no, you can't use a JS file in a smart contract. Ethereum blockchain is deterministic so all information from outside has to come through transactions. But yes, you can use events to trigger something when your backend is monitoring for those events. After that it has nothing to do with Ethereum.

In theory if you add the contents of the JS file into your transaction you can use the data with Solidity in the smart contract but that's probably not what you want - it couldn't contact external services to send SMS for example.

Your question seems to be mostly about React and problems with loading a JS file in React. I suggest you ask the React parts of your problem in a more proper forum (for example the main StackOverflow).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.