Python TeleBot media editing - python

people!
So, i'm using PyTelegramBotAPI and i need to edit media (photo in my case) in my bot's message, so i tried this thing:
bot.edit_message_media(message_id=M_ID, chat_id=C_ID, media=MY_MEDIA)
If i try to pass message.photo[0].file_id from another message (i mean, photo is already on the telegram server), or if i even try to create InputMedia object from scratch like that:
new = types.InputMedia
new.media = message.photo[0].file_id
In both scenarios, if i print file_id, it will show this long id string, so it's not missing.
But i still get this error:
2020-12-23 08:22:16,889 (__init__.py:489 MainThread) ERROR - TeleBot: "A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: parameter "media" is required"
But i did set the media parametr.... what's the problem?

Alright, i've found the solution.
When you are passing new media, do it like that:
from telebot import types
bot.edit_message_media(message_id=M_ID, chat_id=C_ID, media=types.InputMediaPhoto(file_id))
This works... wow!

Related

Private chats in telegram. Telethon python

I started writing a bot for a private chat in Telegram. I ran into a problem.
url = "here I entered the link for the invitation"
channel = waiting for the client.get_entity(url)
Returned the error `ValueError: cannot find any object matching the 'link'
Nothing came of it.
As a result, I changed the chat type to public, created a link, inserted it into the url = ""
It worked, but how to receive and filter messages from a public chat.
Tried to get a link via
https://t.me/getmyid_bot

Telegram Bot Api / Python: Trying to send voice message via my telegram bot

so im playing a little bit around with my telegram bot, now im already able to send normal messages and pictures. But now i want to be able to send pre recorded voice messages as an answer. In the documentation it says to use send_voice for this matter and it recommends using the file_id of a file thats already on the telegram servers. So thats what i did i send a voice message to the #RawDataBot and he returned to me the file_id of the voice message.
Problem is: When i try to trigger it i get the following error: telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: wrong file identifier/HTTP URL specified
Any ideas here on what i might be doing wrong? Here is the relevant code:
import telebot
API_KEY = <MY_API_KEY>
bot = telebot.TeleBot(API_KEY)
#start
#bot.message_handler(commands=['start'])
def start (message):
bot.send_message(message.chat.id,Textstart)
#bot.message_handler(commands=['pic'])
def start (photo):
bot.send_photo(photo.chat.id, "https://de.wikipedia.org/wiki/Zeus#/media/Datei:Zeus_Otricoli_Pio-Clementino_Inv257.jpg")
#here is the part where he is supposed to send the voice message if someone types in /audio
#bot.message_handler(commands=['audio'])
def start (voice):
bot.send_voice(voice.chat.id,"AwACAgIAAxkBAAEWjl5i5bjyudWAM9IISKWhE1Gjs5ntQgACLx8AApcNKEv97pVasPhBoCkE",)
bot.polling()
There are multiple reasons, but I think it is because of these:
file_id is unique for each individual bot and can't be transferred from one bot to another.
file_id uniquely identifies a file, but a file can have different valid file_ids even for the same bot.
See here.

Problem with posting files to slack channel using Python

So I need to send messages and post files to slack channels using Python.
I am able to send messages but I am getting error uploading files. The relevant piece of code looks likes this:
response = self.client.chat_postMessage(
channel='#notifications',
text=message)
filepath=f'./{file_name}'
response = self.client.files_upload(
channels='#notifications',
file=filepath)
The message gets posted successfully, the file upload results in error:
slack.errors.SlackApiError: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'not_in_channel'}
I have set the following permission in Slack OAuth and permissions screen:
incoming-webhook, chat:write, chat:write.public, files:write
I know that if I dont give chat:write.public I get same error while posting messages as well. However I am not able to find any files:write.public permission.
Has anyone encountered this error? Any insight is appreciated.

A request to the Telegram API was unsuccessful. The server returned HTTP 403 Forbidden

I have the telegram bot written on telebot package on python.
From 03/01/2021 worked without problems. Main task of this bot parse information from documents sent by users. It also handles some commands. All other types of data forward from the user to the developer.
On January 17, 2022, 5 entries with an error appeared in the logs:
A request to the Telegram API was unsuccessful. Error code: 502 Description: Bad Gateway
After about an hour, each request from users returned with an error in the logs:
A request to the Telegram API was unsuccessful. The server returned HTTP 403 Forbidden. Response body:\n[b'<html>\\r\\n<head><title>403 Forbidden</title></head>\\r\\n<body>\\r\\n<center><h1>403 Forbidden</h1></center>\\r\\n<hr><center>nginx/1.18.0</center>\\r\\n</body>\\r\\n</html>\\r\\n']"
I find that a 403 error occurs when bot trying to download the sended document from user with the download_file() method. The part of my code where its breaks:
def handling_documents(message):
def save_file():
downloaded_file = bot.download_file(bot.get_file(message.document.file_id).file_path)
filename = message.document.file_name
The bot runs on CentOS. I tried other workplaces and problem is actual. The problem in https://api.telegram.org/file/bot_token/file_path . Any other types of messages (photos, stickers, text, etc.) the bot handling successfully, as it should.
Couldn't find a good solution on google. About 100 users use the bot daily. I can’t just wait as it is written in Telegram Bot File Download Suddenly response 403 .
I also found a solution with the revoke of the API token https://github.com/eternnoir/pyTelegramBotAPI/issues/581 But after that the problem with the 403 error remained. What could be wrong?
I use PyTelegramBotAPI 4.3.1 version, Linux Mint 20.2, PyCharm.
I didn't find help by russian community stackoverflow.
P.S. sorry for my english if it written not clear
It seems like some kind of telegram api bug. The problem resolved itself. Bot recovered after a 2-day downtime.

Is there another reason for Illegal redirect_URI error when using Spotify API?

I am trying to use the Spotify API (using Spotipy), however I am having issues with the authentication step. I am following along a youtube playlist to learn it. To do that, I am just following along with the code that is shown below. However, when it opens into my web browser for authentication, I get an "Illegal redirect_uri" error.
I tried searching the web and came across this answer that says that it is probably a typo in the redirect_URI on the spotify website or that has been set in my environment variable, however, I have quadruple checked to make sure there was no typo. An image is attached that shows what my environment variable is and what the redirect_URI is set as in spotify.
Is there another reason that I could be getting this error?
Thank you for the help. Spotify Redirect_URI
import os
import sys
import json
import spotipy
import webbrowser
import spotipy.util as util
from json.decoder import JSONDecodeError
#Get the username from terminal
username = sys.argv[1]
# Erase cache and prompt for user permission
try:
token = util.prompt_for_user_token(username)
except:
os.remove(f".cache-{username}")
token = util.prompt_for_user_token(username)
#Create our spotifyObject
spotifyObject = spotipy.Spotify(auth=token)
It is probably a security issue, since google.com will not understand the parameters of the request sent by the Spotify API. It looks like you are not trying to intercept the request (since you are using google.com), so you could try to use https://localhost:8080/ as the redirect url. Since there is (probably) no server running locally, no page will open (and you will get an error), but you only need to copy the url in the address bar ;-)

Categories

Resources