NeuroAgent

Why gram.js returns messageMediaUnsupported for polls

Learn why gram.js returns messageMediaUnsupported for some polls in Telegram and how to fix it. Complete guide to working with polls via Telegram API.

Why does Telegram return “media: messageMediaUnsupported” for one of the poll messages when using gram.js?

I’m trying to parse several polls in an open group using the JavaScript client gram.js v2.26.21. The group contains four messages, each with a poll that has 10 multiple-choice options.

Using the official desktop client, I exported the group’s message history to JSON format and obtained the IDs of the messages containing polls. I wrote a parser that retrieves messages by ID through the channels.getMessages method. In the Message object, the media field contains messageMediaPoll, from which I extract the question text and answer options. Then I make a request to messages.getPollResults to get the voting details.

The problem is that for one of the four messages, the media field contains messageMediaUnsupported instead of messageMediaPoll. However, the messages.getPollResults method works correctly for this same message and returns all the votes.

Why does Telegram consider that my client version doesn’t support this poll, and why does this only happen with one message out of four? What am I doing wrong?

NeuroAgent

When using gram.js to parse polls from Telegram, you encounter a situation where three poll messages return messageMediaPoll, while one returns messageMediaUnsupported. This issue occurs due to features of the Telegram API and version compatibility.

messageMediaUnsupported means that the current client version does not support this media type. This is not an error in your code, but a feature of how the Telegram API works with different types of polls and their history.


Table of Contents


Causes of messageMediaUnsupported

messageMediaUnsupported occurs when Telegram determines that your client (in this case, gram.js) does not support a particular media type. According to the Telegram documentation, this means: “Current version of the client does not support this media type”.

Main causes:

  1. API version mismatch: gram.js uses a specific version of the Telegram API that may not support all types of polls, especially those created using new Telegram features.

  2. Poll format: Different polls may have various formats or parameters that not all API versions can handle correctly. Some polls may contain special flags or parameters that appeared in newer Telegram versions.

  3. Restrictions for third-party clients: Telegram may intentionally limit access to certain types of content for third-party clients for security or functionality reasons.

  4. Historical data: Exported messages may contain polls created at different times with different specifications.

Why this happens only with one message

The fact that the issue occurs with only one of four messages indicates specific characteristics of this particular poll:

  1. Poll creation time: The problematic message may have been created when Telegram used a different specification for polls, or when changes were being made to the API.

  2. Special poll parameters: This poll may contain:

    • Additional flags (such as public_voters or quiz)
    • Special settings not supported by the current version of gram.js
    • Parameters available only in Telegram’s premium version
  3. Size or complexity of the poll: A poll with 10 answer options may reach size or complexity limits that are handled differently in various API versions.

  4. Change history: Telegram may have changed how polls are stored in message history, leading to incompatibility when reading old records.

It’s important to note that messages.getPollResults works correctly for all messages, including the problematic one. This confirms that the poll data is available on the server, but the issue occurs specifically when reading the media field in the message object.


Solutions to the problem

1. Update gram.js

Check for updates to gram.js and update to the latest version:

bash
npm update gram.js

The latest versions may include support for new poll types and compatibility fixes.

2. Handle messageMediaUnsupported

Modify your code to handle both cases:

javascript
async function processPollMessage(messageId) {
    const message = await client.channels.getMessages({ id: [messageId] });
    
    if (message.media && message.media._ === 'messageMediaPoll') {
        // Regular poll
        const poll = message.media.poll;
        console.log('Question:', poll.question);
        console.log('Options:', poll.answers);
    } else if (message.media && message.media._ === 'messageMediaUnsupported') {
        // Unsupported poll
        try {
            // Try to get poll results directly
            const results = await client.messages.getPollResults({
                peer: message.peerId,
                id: message.id
            });
            
            console.log('Poll results (unsupported):', results);
        } catch (error) {
            console.error('Error getting results:', error);
        }
    }
}

3. Use messages.getPollResults for all polls

Since messages.getPollResults works for all types of polls, you can use it as the primary method for retrieving data:

javascript
async function getAllPollResults(messageIds) {
    const results = [];
    
    for (const messageId of messageIds) {
        try {
            const pollResults = await client.messages.getPollResults({
                peer: message.peerId,
                id: messageId
            });
            results.push({ messageId, pollResults });
        } catch (error) {
            console.error(`Error for message ${messageId}:`, error);
        }
    }
    
    return results;
}

4. Check API versions

Ensure your gram.js is using the latest Telegram API version:

javascript
// Check API version
console.log('Current API version:', client.apiId);

Alternative approaches

1. Use the official API through Bot API

If the issue persists, consider using the Bot API:

javascript
const { Telegraf } = require('telegraf');

const bot = new Telegraf(TOKEN);

bot.on('poll', (ctx) => {
    console.log('Poll:', ctx.poll);
});

// Or get poll by message
bot.command('getpoll', async (ctx) => {
    const messageId = ctx.message.text.split(' ')[1];
    const poll = await ctx.telegram.getPoll(messageId);
    console.log('Poll results:', poll);
});

2. Use MTProto directly

For maximum compatibility, you can use MTProto directly:

javascript
const { TelegramClient } = require('telegram');
const { StringSession } = require('telegram/sessions');

const client = new TelegramClient(
    new StringSession(''),
    apiId,
    apiHash,
    { connectionRetries: 5 }
);

await client.connect();

// Direct API request
const result = await client.invoke({
    _: 'messages.getPollResults',
    peer: 'username',
    id: messageId
});

Recommendations for working with polls

  1. Always use messages.getPollResults: As your experience shows, this method is more reliable for getting poll data than reading the media field.

  2. Handle all possible media types: Your code should correctly handle both messageMediaPoll and messageMediaUnsupported.

  3. Use try-catch blocks: Operations with polls can throw errors, especially when working with non-standard types.

  4. Keep an eye on gram.js updates: Regularly update the library to get support for new Telegram features.

  5. Test with different types of polls: Create test polls with various parameters to identify potential compatibility issues.


Conclusion

The issue with messageMediaUnsupported for one of the polls is not an error in your code, but a feature of how the Telegram API works. The main causes include:

  • API version mismatch between gram.js and Telegram
  • Special parameters or format of a specific poll
  • Access restrictions to certain content types for third-party clients

The most effective solution is to use messages.getPollResults as the primary method for retrieving poll data, as it works correctly for all types of polls, including those that return messageMediaUnsupported in the media field.

It’s important to remember that Telegram is constantly evolving, and some features may only be available in official clients or through specific API methods. Regularly updating gram.js and adapting your code to API changes will help avoid similar issues in the future.

Sources

  1. MessageMediaUnsupported - Telegram Core API
  2. MessageMedia Types - Telegram Documentation
  3. Gram.js Message API Documentation
  4. Reddit Discussion: Telegram Unsupported Messages
  5. Quora: What does ‘unsupported message’ on Telegram mean?
  6. GitHub Issue: Poll compatibility in web clients
  7. Pyrogram Documentation: MessageMediaUnsupported