Creating a program for sending information about tokens to a Telegram group using Solana
In this article, we will look at the steps to create a program that extracts and sends information about specific tokens with specified data to a designated Telegram group on the Solana blockchain.
Prerequisites
- Install Node.js (version 14 or higher) if you haven’t already.
- Familiarity with TypeScript, JavaScript, and Python is required. If you are new to these languages, we will provide explanations and examples for each language.
- A Solana node (eg Solana CLI or Solana Terminal application) configured on your local computer.
Token Extraction
To extract token information from Solana, we will use the solana-program' library. Install it first:
npm install @solana program @types/solana
Then create a new TypeScript file for the token extraction logic. Here's an example code snippet to get you started:
token-fetching.ts
market_chart?vs_currency=usd&days=30&interval=daily&market_data=true&sparkline=false
import * as solanaProgram from '@solana-program';
const TOKEN_INFO_URL = '
const TELEGRAM_BOT_TOKE = '
'; export async function fetchTokenInfo(tokenId: string) {
const programId = 'your-program-id'; // Replace with your Solana program ID
const tokenProgramAddress = '
try {
// Retrieve market data for the specified token
const marketDataResponse = await fetchTokenInfoResponse(tokenId, programId);
const marketData = marketDataResponse.data;
// Extract relevant information from market data
const marketCap = marketData.market_cap;
const volume = marketData.volume;
const liquidity = marketData.liquidity;
return {
marketCap,
volume,
liquidity,
};
} catch ( error ) {
console.error(error);
return null;
}
}
async function fetchTokenInfoResponse(tokenId: string, programId: string) {
// Implement your Solana program to retrieve information about the token
const programProgramId = solanaProgram.programId.toString();
const queryArgs = [
,
Market Cap: ${info.marketCap} USD];
const response = await solanaProgram.runProgram(
programProgramId,
programId,
queryArgs,
fetchTokenInfo,
);
// Parse the response as JSON
return response.data;
}
// Usage example
async function main() {
const tokenId = 'solana';
try {
const info = await fetchTokenInfo(tokenId);
if (info) {
console.log(
);
Volume: ${info.volume} USDconsole.log(
);
Liquidity: ${info.liquidity} USDconsole.log(
);
}
} catch ( error ) {
console.error(error);
}
}
main();
Sending information about the token to the Telegram group
To send information about the token to the specified Telegram group, you will need to use the @solana/solana-telegram-bot-sdklibrary. Install it first:
npm install @solana/solana-telegram-bot-sdk
Next, create another TypeScript file for the Telegram bot logic. Here is an example code snippet:
token-fixer.ts
“typescript
import * as solanaProgram from ‘@solana-program’;
import { TelegramBot } from ‘@solana/solana-telegram-bot-sdk’;
const TELEGRAM_BOT_TOKE = ‘
const TELEGRAM_GROUP_ID = ‘your-telegram-group-id’;
export asynchronous function sendTokenInfoToGroup(tokenId: string) {
const bot = new TelegramBot(
TELEGRAMM_BOT_TOKE,
{
url: ‘
Leave a Reply