Creating a program to send token information to a telegram group using Solana
In this article, we will examine the steps of creating a program that seeks and send information about specific tokens with specified details to a telegram group designated at Blockchain Solana.
Prerequisites
- Install Node.js (version 14 or higher), if you have not done so yet.
- Familiarity with TypeScript, JavaScript and Python is required. If you are new in these languages, we will provide explanations and examples for each language.
- A Solana node (for example, Solana Cli or the terminal solana application) configured on your local machine.
FEKEN FEKEN
To look for Solana token information, we will use the Solana-Program 'library. First, install:
Bash
Npm install @solana program @typepes/solana
`
Then create a new TypeScript file for token search logic. Here is an example of code snippet for you to start:
TOKEN-FETCHING.TS
`Different
Import * as solanaprogram of ‘@solana-program’;
CONST TOKEN_INFO_URL = ‘
CONST TELEGRAM_BOT_TOKEN = ‘
Export FetchTokeninfo asynchronous function (tokenid: string) {
CONST PROGRAMID = ‘Your-Program-DID’; // Replace with your Solana Program ID
Const
to try {
// Search market data for the specified token
Const Marketdata Response = awaits fetchtokeninfospones (tokenid, program);
Const Marketdata = Marketdata Response.Data;
// Extract relevant information from market data
Const Marketcap = marketdata.market_cap;
Constt Volume = Marketdata.volume;
Constt Liquidity = Marketdata.Liquidity;
Return {
Marketcap,
volume,
liquidity,
};
} catch (error) {
console.error (error);
return null;
}
}
Fetchtokeninfosponespone asynchized function (tokenid: string, programmid: string) {
// Implement your Solana program to search for token information
CONST PROGRAMPROGRAM = SolanProgram.programid.Tostring ();
CONST QUERYARGS = [
Market_chart? vs_currency = USD & DAYS = 30 & interval = Daily & Market_Data = True & Sparkline = False
,
];
CONST Response = awaits solanaprogram.runprogram (
Programmaker,
Programmid,
Querygs,
‘fetchtokeninfo’,
);
// Analyze the answer as JSON
Return response.
}
// Example of Use
ASSINCRONE FUNCTION MAIN () {
Const Tokenid = ‘Solana’;
to try {
CONST INFO = awaits fetchtokeninfo (tokenid);
if (info) {
console.log (market cap: $ {info.marketcap} USD
);
console.log (Volume: $ {info.volume} USD
);
console.log (liquidity: $ {info.liquidity} USD
);
}
} catch (error) {
console.error (error);
}
}
main();
`
Submitting token information to a telegram group
To send token information to a designated telegram group, you will need to use the @solana/solana-telegram-bot-sdk 'library. First, install:
Bash
Npm install @solana/solana-telegram-bot-sdk
`
Then create another typed text file for the logic of Telegram Bot. Here is an example of code snippet:
TOKEN-FICER.TS
`Different
Import * as solanaprogram of ‘@solana-program’;
Import {telegraph} of ‘@solana/solana-telegram-bot-sdk’;
CONST TELEGRAM_BOT_TOKEN = ‘
CONST TELEGRAM_GROUP_ID = ‘Your-Telemgram-Group-ID’;
Export asynchronous function SendTokenimpotogroup (tokenid: string) {
CONST BOT = NEW TELEGREMBOT (
Telegram_bot_Token,
{
URL: ‘https: //api.telegram.