python telebot got unexpected response

2024/10/8 22:58:53

I have been using my Telegram bot for sending me different notifications from my desktop computer using python's telebot library. Everything was working properly for quite a long time, but one day it stopped working.

Here's the code (Python 2.7):

import telebot
import socketTELEBOT_TOKEN = '<token>'
CHAT_ID = <chat id>bot = telebot.TeleBot(TELEBOT_TOKEN)def notify(message):bot.send_message(CHAT_ID, 'Notification from ' + socket.gethostname() + ':\n' + message)notify('Hello world!')

When I try doing this in the interpreter, I get this:

Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import telebot
>>> TELEBOT_TOKEN = '<token>'
>>> CHAT_ID = <chat id>
>>> bot = telebot.TeleBot(TELEBOT_TOKEN)
>>> bot.send_message(CHAT_ID, 'Hello world!')
{'ok': False, 'error': 'Got unexpected response. (404) - {"ok":false,"error_code":404,"description":"Not Found"}'}

Seems that I will get this error on any request

>>> bot.get_me()
{'ok': False, 'error': 'Got unexpected response. (404) - {"ok":false,"error_code":404,"description":"Not Found"}'}

I also tried using direct HTTPS Telegram bot API in the browser - typed this

https://api.telegram.org/bot<token>/sendMessage?chat_id=<chat id>&text=Test

in the address line and it did the thing!

It also works with Python's requests library

>>> import requests
>>> res = requests.get('https://api.telegram.org/bot<token>/sendMessage?chat_id=<chat id>&text=Test')

And finally, it works with the very same code on my two servers (VDS) without any troubles.

I recently installed scapy, if it has anything to do with this (maybe it caused the problem?)

EDIT: uninstalling scapy didn't help.

I tried rebooting the computer, and the router, but nothing changed.

What can be wrong with my computer?

Answer
bot = telebot.TeleBot(TOKEN)
bot.config['api_key'] = TOKEN
https://en.xdnf.cn/q/70087.html

Related Q&A

How to set correct value for Django ROOT_URLCONF setting in different branches

Ive put site directory created by django-admin startproject under version control (Mercurial). Lets say, the site is called frobnicator.Now I want to make some serious refactoring, so I clone the site …

How do I improve scrapys download speed?

Im using scrapy to download pages from many different domains in parallel. I have hundreds of thousands of pages to download, so performance is important.Unfortunately, as Ive profiled scrapys speed, …

Convert numpy, list or float to string in python

Im writing a python function to append data to text file, as shown in the following,The problem is the variable, var, could be a 1D numpy array, a 1D list, or just a float number, I know how to convert…

Shared XMPP connection between Celery workers

My web app needs to be able to send XMPP messages (Facebook Chat), and I thought Celery might be a good solution for this. A task would consist of querying the database and sending the XMPP message to …

List of installed fonts OS X / C

Im trying to programatically get a list of installed fonts in C or Python. I need to be able to do this on OS X, does anyone know how?

How to detect changed and new items in an RSS feed?

Using feedparser or some other Python library to download and parse RSS feeds; how can I reliably detect new items and modified items?So far I have seen new items in feeds with publication dates earli…

python SharedMemory persistence between processes

Is there any way to make SharedMemory object created in Python persist between processes? If the following code is invoked in interactive python session: >>> from multiprocessing import share…

What is the difference between syntax error and runtime error?

For example:def tofloat(i): return flt(i)def addnums(numlist):total = 0for i in numlist:total += tofloat(i)return totalnums = [1 ,2 ,3] addnums(nums)The flt is supposed to be float, but Im confused whe…

Printing a line at the bottom of the console/terminal

Using Python, I would like to print a line that will appear on the last visible line on the console the script is being ran from. For example, something like this:Would this be able to be done?

Comparing first element of the consecutive lists of tuples in Python

I have a list of tuples, each containing two elements. The first element of few sublists is common. I want to compare the first element of these sublists and append the second element in one lists. Her…