Creating a Program to Send Token Information to a Telegram Group Using Solana
In this article, we will walk you through the steps to create a program that retrieves and sends information about specific tokens with specific details to a designated Telegram group on the Solana blockchain.
Prerequisites
- Install Node.js (version 14 or later) if you haven’t already.
- Knowledge of TypeScript, JavaScript, and Python is required. If you are not familiar with these languages, explanations and examples are provided for each language.
- A Solana node (e.g. Solana CLI or Solana Terminal application) set up on your local machine.
Getting Tokens
To retrieve token information from Solana, we will use the « solana-program » library. First install:
npm install @solana @types/solana
Then create a new TypeScript file for the token fetching logic. Here is a sample code snippet to get you started:
token-fetching.ts
import * solana as Program from: '@solana-program';
const TOKEN_INFO_URL = '
const TELEGRAM_BOT_TOKE = '';
export async function fetchTokenInfo(tokenId: string) {
const programId = 'your program ID'; // Replace with Solana's program ID
const tokenProgramAddress = '
try {
// Retrieve market data for the given 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 the Solana program to retrieve token information
const programProgramId = solanaProgram.programId.toString();
const queryArgs = [
market_chart?vs_currency=usd&days=30&interval=daly&market_data=true&sparkline=false
,
];
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(Market Cap: ${info.marketCap} USD
);
console.log(Volume: ${info.volume} USD
);
console.log(Liquidity: ${info.liquidity} USD
);
}
} catch ( error ) {
console.error(error);
}
}
main();
Sending token information to a Telegram group
To send token information to a designated Telegram group, you need to use the @solana/solana-telegram-bot-sdk
library. First install it:
npm install @solana/solana-telegram-bot-sdk
Then 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 async function sendTokenInfoToGroup(tokenId: string) {
const bot = new TelegramBot(
TELEGRAMM_BOT_TOKE,
{
url: «