grammyjs / grammY

The Telegram Bot Framework.
https://grammy.dev
MIT License
2.04k stars 106 forks source link

HttpError: Network request for 'getMe' failed! #579

Closed wudb closed 2 months ago

wudb commented 2 months ago

Hi, I used grammy,but report error: HttpError: Network request for 'getMe' failed! my source code:

// main.ts
import { config } from "dotenv";
config(); 

import { Bot, GrammyError, HttpError } from "grammy";
import { SocksProxyAgent } from "socks-proxy-agent";

const socksAgent = new SocksProxyAgent("socks5://127.0.0.1:7890")
const bot = new Bot(process.env.TELEGRAM_BOT_TOKEN!, {
    client: {
        baseFetchConfig: {
            agent: socksAgent,
            compress: true
        },
        timeoutSeconds: 30
    }
});

// start
bot.command("start", (ctx) => ctx.reply("Hello!"));

bot.catch((err) => {
    const ctx = err.ctx;
    console.error(`Error while handling update ${ctx.update.update_id}:`);
    const e = err.error;
    if (e instanceof GrammyError) {
      console.error("Error in request:", e.description);
    } else if (e instanceof HttpError) {
      console.error("Could not contact Telegram:", e);
    } else {
      console.error("Unknown error:", e);
    }
});

bot.start();
console.log("Bot started!");

node version: v20.3.1

// package.json 
{
  "name": "flash_bot",
  "version": "1.0.0",
  "description": "bot of flash",
  "main": "main.ts",
  "scripts": {
    "start": "npm run build:live",
    "build:live": "nodemon --exec ./node_modules/.bin/ts-node -- ./main.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/node": "^20.12.11",
    "nodemon": "^3.1.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.4.5"
  },
  "dependencies": {
    "dotenv": "^16.4.5",
    "grammy": "^1.23.0",
    "socks-proxy-agent": "^8.0.3"
  }
}

The same error is reported whether the proxy is set or not.

KnorpelSenf commented 2 months ago

Can you share the code with the other framework that works? You mentioned this in https://t.me/grammyjs/236806

wudb commented 2 months ago

const TelegramBot = require('node-telegram-bot-api'); const agent = require('socks5-https-client/lib/Agent');

const bot = new TelegramBot(process.env.TELEGRAM_BOT_TOKEN, { polling: true, request: { agentClass: agent, agentOptions: { socksHost: process.env.PROXY_SOCKS5_HOST, socksPort: process.env.PROXY_SOCKS5_PORT }, // timeout: 300000 } });

The framework used here is node-telegram bot-api and socks5-https-client, it work.

wudb commented 2 months ago

Can you share the code with the other framework that works? You mentioned this in https://t.me/grammyjs/236806

See the code above, the development environment and agent are the same

wudb commented 2 months ago

I solved this problem myself, in the following code: const socksAgent = new SocksProxyAgent("socks5://127.0.0.1:7890"), just change socks5 to socks.

KnorpelSenf commented 2 months ago

Oh wow, good job, i wouldn't have guessed that. Thanks for sharing the solution, I'll point future people here /cc @KnightNiwrem