Bot can't send channel photo by file_id - python

I use python and here is a problem. First I use method get_chat to get file_id of photo chat as example.
import telebot
bot = telebot.TeleBot("xxxxxxxxxxxxxxxxxxxxxxxx")
s = bot.get_chat('#codygarbrandt_best')
print(s.photo.big_file_id)
Then I want to send this photo to myself.
bot.send_photo(my_chat_id , s.photo.big_file_id )
But I get the following error.
Bad Request: type of file mismatch
I remember it worked before, how can I achieve this now?

The big_file_id which can be read from the get_chat method can only be used for downloading the photo. You will have to store the file locally yourself and then resend it.
Unique file identifier of big (640x640) chat photo. This file_id can be used only for photo download. [doc]

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.

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!

Python doesn't understand my string variable

I am trying to make a bot for Telegram using pyTelegramBotAPI, it is a store to download Windows Applications, because of the 50MB limit of sending the telegram. I send the file as a user and get the File_ID, which causes the bot to send 2GB files. Each program has a file.txt with your File_ID written.
The bot reads vlc.txt, sets it as a variable and sends the file.
But the program has an error.
"2020-12-19 21:06:09,106 (init.py:489 MainThread) ERROR - TeleBot: "A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: wrong remote file identifier specified: Wrong character in the string""
When I manually put the File_ID in the code, it works perfectly, it can't get the file_id from the variable, and I sent the program to print to make sure the variable contains the File_ID written correctly. What could be causing this?
def selecionou(query):
#pretreatment definition
download = query.data.count('down')
info = query.data.count('info')
prints = query.data.count('prints')
#app name treatment
if download > 0:
name = query.data.replace("down.","")
print(name)
file_id = open(f'/home/flaipy/PycharmProjects/Lucas/apps/{name}.txt','r')
file_id = file_id.read()
print(file_id)
#order type treatment
if download > 0:
print(file_id)
bot.send_document(query.message.chat.id, file_id)
I was reading w3schools from the "read ()" method in python, and saw that there is an argument to limit the number of characters, I put 70 and it worked, even in the txt file having no space. It is a tip for those who go through the same problem.
file_id = file_id.read(70)

How to send python output to telegram CHANNEL not to Group and gmail email group

Hi I have a program in python that generates results every one hour. The result can be of anything.This program will run in local machine or in the virtual private network.
I have two requirements
1. Send this python generated result to one telegram group [Group name "ourworld"](created by me) automatically without user intervention . (I have desktop telegram client running or web.telegram.org running in the same system)
Send this result to gmail group email ID.
what are the methods available to achieve this requirement .Is there any working code available to do this job .Please share the info and details.
Edit:
The Issue that i am facing :
1.created a Bot using BotFather.
2.Adding this Bot to my Group ,Here i get an error could not add an member So added the Bot as admin in the group
3.Token of the BOT noted down.
4. Trying to get ChatId
in this forum (https://web.telegram.org/#/im?p=g154513121) someone says number after p=g is the chartid ,In my case there is no
number it shows #testingname like this.
Using this approach trying to get the Chat ID https://api.telegram.org/bot738909732:AAE-2l7xAAlYHBXprK_a_bex09g9DMZbbDI/getme
so here 738909732 become a chat Id (in this case why we need seperate
call for the chart id)
here it is true as response coming! Here the chat id is the ID of the my "testingname" no chart id generated for the group.
6.Now packing the URL to see the response i am getting this error.
https://api.telegram.org/bot738909732:AAE-2l7xAAlYHBXprK_a_bex09g9DMZbbDI/sendMessage?chat_id=738909732&text=testing
the output if i run this in browser
{"ok":false,"error_code":400,"description":"Bad Request: chat not
found"} {"ok":false,"error_code":403,"description":"Forbidden: bot
can't send messages to bots"}
How to resolve this issue and make the BOT working .why i am not able to add BOT to my group that says error "Cant Add user"
How to make this telegram group working.
Note : Using BotFather BOT created
In case for sending message to telegram group the above method provided by bipin_s works where the chat_id = -xxxxxx is used.This is correct id followed by - symbol to be used.
For Posting message in the "telegram channel " a minor change needs to be done in the URL.The URL must be framed as below .
url = "https://api.telegram.org/botTokenID/sendMessage?chat_id=#yourChannelChatID&text=message"
Replace the TokenID with your BOT tokenID replace the yourChannelChatID with your channel chart id.Please note that the channel id is not negative id.
the code will look like this
import request
url = "https://api.telegram.org/botXyz:wwwwDFSJSJSAX/sendMessage?chat_id=#telechanneltesting&text=message"
requests.post(url)
Here the "message" as in the URL it will be posted in telegram channel.
How to get channel id ?
Go to https://web.telegram.org/#/im in browser ,after login now search your "channel".Now in the browser address bar you will one link like https://web.telegram.org/#/im?p=#llliopppppsssssr
p=#llliopppppsssssr after the equal symbol what comes is channel chat ID.
to send message to the telegram "group" without any user intervention , you require a telegram bot. create one using telegram bot-father. take a look at this link. also note the token while creating the bot. This method will work only for telegram Group .Telegram channel another method to be followed which MarxBabu answered below in his answers post.
import requests
# telegram url
url = "https://api.telegram.org/bot<Token>"
def send_mess(text):
params = {'chat_id':-xxxxxxx, 'text': text}
response = requests.post(url + 'sendMessage', data=params)
return response
send_mess(result_you_want_to_send)
to get the chat_id follow the steps mentioned here. note: group chat id's always starts with '-' . e.g. of group chat_id -356163873. A token and chat_id is only what you require to send message to telegram group.
for sending group emails you have to search more as i do not know much
I have it running on a Raspberry pi. You must look for botfather to get your telegram token.
import telepot
from telepot.loop import MessageLoop
telegram_token = 'xxxx:xxxxxx'
user = 4444444
bot = telepot.Bot(telegram_token)
bot.sendMessage(user, 'Hey!')
For configuring gmail I don't have something here right now...
You can send emails in python through SMTP or using the Mailgun Api
I personally prefer using Mailgun as it is easier to setup and sending an email is as easy as sending a post request to mailgun.
You can get the free version of the api and add your group email id to the sandbox (to avoid spam) and then use requests to post an email with the given api token

How to post formatted message into a channel with Slack API

I have a json formatted message like this, I want to post this message into Slack with Slack postMessage API. Pasting the json string into text section would not work, how/where should I input that?
Thanks
Using the link you posted, you'll need to generate an API token for your slack account. After that, choose a channel to post the message and put
*bold* `code` _italic_ ~strike~
next to the "text" field. The message should be posted in your slack channel.
Since you're using python, you can use the python slack api to do this. The code would then be
from slackclient import SlackClient
slack_client = SlackClient(API TOKEN)
slack_client.api_call("chat.postMessage", channel=CHANNEL ID, text="*bold* `code` _italic_ ~strike~", as_user=True)

Categories

Resources