Problem with posting files to slack channel using Python - 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.

Related

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.

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.

Sending APNs notification returns Success but no notification appears on phone

I'm trying to send a push notification on an iphone using the pushjack library
(https://github.com/joseph-fox/pushjack).
I'm trying to run the provided example using my company's test phone token and pem file.
The code that I'm trying to run is this:
from pushjack_http2 import APNSClient
client = APNSClient(certificate='<mypem>.pem',
default_error_timeout=10,
default_expiration_offset=2592000,
default_batch_size=100,
default_retries=5)
token = '<mytoken>'
alert = 'Hello world.'
res = client.send([token], alert)
resp = {'successes': res.successes, 'failures': res.failures, 'tokens_sent': res.tokens, 'errors': res.errors,
'token_errors': res.token_errors}
return resp
and the response that I'm getting is the following:
{'successes': ['<mytoken>'], 'failures': [], 'tokens_sent': ['<mytoken>'], 'errors': [], 'token_errors': {}}
When using the same .pem and token via pushtry (https://pushtry.com/) I'm having the notification appear correctly on the phone (I wonder if it has to do with the bundle id which I also provided there, but had no option to provide in the sample code).
This is a rather vague question, but, does anybody have any idea why this would happen?
I've tried both the dev and the production .pem file and both return a success while none has a notification correctly pop up. Do you have any idea on what could be wrong or how I could probe around to find what's wrong?
Thanks!
Turns out that I didn't realise that apple changed something in their backend on March31 and the library I was trying to use was deprecated.
Solved it using apns2 instead.

Python TeleBot media editing

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!

How to get user info with bot token

I am using python slack client to connect and send messages with a bot application.
slc = SlackClient("BOT_USER_TOKEN")
out = slc.api_call(method='users.profile.get', user='XXX')
I am getting not_allowed_token_type error in output json. I am able to call:
slc.api_call(method='chat.PostMessage',channel)
Is there a way to get user email, name from slack API. previously I got the username from event messages. but now I am getting only user id so I am looking for a solution to fetch user info using that id.
The reason you get this error message is that users.profile.get does not work with bot tokens.
To mitigate just use the user token that you received along with your bot token when installing your Slack app.

Categories

Resources