I am trying to make telegram bot using library python-telegram-bot. One of the features of my bot is to send Instagram’s stories to the user. For this feature I am using method sendMediaGroup with several InputMediaVideo or InputMediaPhoto in the input.
For example (Instagram link is available only for 24 hours, perhaps at the time you read this, it is no longer valid):
def input_media_group_test(update: Update, context: CallbackContext):
update.message.bot.sendMediaGroup(
update.message.chat.id,
[
InputMediaVideo('https://scontent-hel3-1.cdninstagram.com/v/t50.2886-16/88545009_217343653444163_3673202947982720032_n.mp4?efg=eyJ2ZW5jb2RlX3RhZyI6InZ0c192b2RfdXJsZ2VuLjcyMC5zdG9yeS5kZWZhdWx0In0&_nc_ht=scontent-hel3-1.cdninstagram.com&_nc_cat=111&_nc_ohc=2Hc1-g9OjDEAX85a-aD&vs=17858017211381000_2586017057&_nc_vs=HBkcFQAYJEdQRVdSd1ZEX2tGRHJNVUFBQ0I4YmFhZTF2a3lidXFIQUFBQRUAAsgBACgAGAAbAYgHdXNlX29pbAExFQAAJpCr%2B6S08bg%2FFQIoAkMzLBdAFAAAAAAAABgSZGFzaF9iYXNlbGluZV8xX3YxEQB16AcA&_nc_rid=ce28126395&oe=60068925&oh=46f6f85c7e36f37b578c7319a70191bc'),
InputMediaVideo('https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4')
]
)
The code above sends two videos (first from the Instagram) to the user. After I send a command calling this code to my bot I get following error: telegram.error.BadRequest: Wrong file identifier/http url specified. What am I doing wrong?
If I use the second url instead of the first one everything works fine, the problem arises only when using Instagram's urls for the videos (photos with InputMediaPhoto works fine).
Also I can send the Instagram video separately, just by calling sendVideo method with the Instagram's video link in the input, but I am trying to send a batch of videos or photos in this bot.
Related
I have been trying to download past broadcasts for a streamer on twitch using python. I found this python code online:
https://gist.github.com/baderj/8340312
However, when I try to call the functions I am getting errors giving me a status 400 message.
Unsure if this is the code I want to download the video (as an mp4) or how to use it properly.
And by video I mean something like this as an example: www(dot)twitch.tv/imaqtpie/v/108909385 //note cant put more than 3 links since I don't have 10 reputation
Any tips on how i should go about doing this?
Here's an example of running it in cmd:
python twitch_past_broadcast_downloader.py 108909385
After running it, it gave me this:
Exception API returned 400
This is where i got the information on running it:
https://www.johannesbader.ch/2014/01/find-video-url-of-twitch-tv-live-streams-or-past-broadcasts/
Huh it's not as easy at it seems ... The code you found on this gist is quite old and Twitch has completely changed its API. Now you will need a Client ID to download videos, in order to limit the amount of video you're downloading.
If you want to correct this gist, here are simple steps you can do :
Register an application : Everything is explained here ! Register you app and keep closely your client id.
Change API route : It is no longer '{base}/api/videos/a{id_}' but {base}/kraken/videos/{id_} (not sure about the last one). You will need to change it inside the python code. The doc is here.
Add the client id to the url : As said in the doc, you need to give a header to the request you make, so add a Client-ID: <client_id> header in the request.
And now I think you will need to start debugging a bit, because it is old code :/
I will try myself to do it and I will edit this answer when I'm finished, but try yourself :)
See ya !
EDIT : Mhhh ... It doesn't seem to be possible anyway to download a video with the API :/ I was thinking only links to API changed, but the chunks section of the response from the video url disappeared and Twitch is no longer giving access to raw videos :/
Really sorry I told you to do that, even with the API I think is no longer possible :/
You can download past broadcasts of Twitch videos with Python library streamlink.
You will need OAuth token which you can generate with the command
streamlink --twitch-oauth-authenticate
Download VODs with:
streamlink --twitch-oauth-token <your-oauth-token> https://www.twitch.tv/videos/<VideoID> best -o <your-output-folder>
I am working on a project that scrapes multiple YouTube channels display name, creation date, subscriber count, and view count. I need help creating this project because I can't seem to find anything about it. I have a YouTube API Key and I just can't seem to find anything about this.
It's a little hard to work with the sample code that the YouTube api shows. Use the curl section. Then send a request to the link that shows and use the information. for example send http request to this:
https://www.googleapis.com/youtube/v3/channels?part=statistics&id=[Channel ID]&key=[Your API]
and this address give you JSON file and you can get what you want!.
for example you can see subscribe count from channel list section in youtube api document. like this image.
you can find other things with this way!. good luck :)
also you can use this.
i'm making sample for you.
def subscribeCount(channel_id):
API = f"https://youtube.googleapis.com/youtube/v3/channels?part=statistics&id={channel_id}&key=[Enter Your Api Key]"
json_data = requests.get(API).json()
subscribeCount = json_data['items'][0]['statistics']['subscriberCount']
return subscribeCount
this sample need channel id(Like all other sections xD). and api key you Which you got from the google developer console.It then shows you the number of subscriptions to a channel. The rest of the sections work the same way.
Read the following article for more information.
How to Use the Python Requests Module With REST APIs
I'm pretty new to making bot thing so I have several questions I want to ask regarding this:
I'm using a bot to send message by
def send_message(chat_id, msg):
url = f"https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text={msg}"
requests.get(url)
send_message(chat_id,msg)
By this, I can send message and I want to beautify the result by using parse_mode but I don't know where to put in in url to make it work.
I'm alternate to using Telethon. I can use it to send message to individual and group by user name and group invite link, but when I try to send it to channel by:
client.send_message(entity='my channel name', message=message)
When I try to run it, it return Cannot find any entity corresponding to "Test channel".
I also try
destination_channel_username='test_ali3'
entity=client.get_entity(destination_channel_username)
client.send_message(entity=entity,message="Hi")
but it require my channel access_hash, how can get it, or are there any other way to send message to channel.
I know Telegram bot API have function like sendMessage or bot.sendMessage that also can do the job, but somehow I can't call it, which packages should I install and/or import.
Thanks a lot!
This should do the trick:
def send_message(chat_id, msg, parse_mode):
url = f"https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text={msg}&parse_mode={parse_mode}"
requests.get(url)
TBH I'm not too familiar with telethon, but that library is mainly intended for so called userbots. that means that it uses the API that also the Telegram app uses and because of that, it's often times more involved than just the plain bot api.
To use the Bot API in Python, I can recommend the package python-telegram-bot, see https://python-telegram-bot.org. Disclaimer: I'm currently the maintainer of that package. There are also other python packages for the bot api, see e.g. https://core.telegram.org/bots/samples#python
I've been trying to use outgoing webhook in Slack to export messages from the channel to my Python program but I can't find the way to do it, so I wonder if even a thing like this exists?
Slack seems to have an API you can use to retreive the messages of a given channel.
Follow this link : https://api.slack.com/messaging/retrieving
You can make a GET request to retreive the full history of the given conversation (so all the messages) :
GET https://slack.com/api/conversations.history?token=YOUR_TOKEN_HERE&channel=CONVERSATION_ID_HERE
Content-type: application/json
You can easily make GET requests wiht python using the library called 'requests'.
If you're able to have a valid API token and the ID of the conversation, you will then be able to get all the messages of the chosen conversation.
First of all, I'm a new guy here and this is my first question, so I'd like to request y'all to ignore any flaws or unexpected details in this question.
So I'm trying to make a screenshot command for my Discord.py bot and currently I'm struck with the following code fragment:
async def ss(ctx, site):
embed=discord.Embed(colour = discord.Colour.orange(), timestamp=ctx.message.created_at)
embed.set_image(url=(f"https://image.thum.io/get/width/1920/crop/675/maxAge/1/noanimate/{site}"))
await ctx.send(embed=embed)
However, the bot just sends an empty embed even for a valid URL. Currently, what seems the most obvious to me is that Discord isn't able to recognize this as a valid image as it doesn't end in a image extension like .png or .jpeg, and hence the empty embed.
TBH I don't know any alternate code for what I'm trying to achieve. I searched a lot and I think it's something to do with BytesIO but I don't have the slightest idea on how to achieve this using the module.
What I'm expecting at this point of time is two things:
Fix the current flaw so that I'm able to send the screenshot of the desired website.
Report to the message author if the website is invalid, in the sense that there's no website on the specified domain, or that the request timed out due to delayed response on the website's end.
Thus, I'd like to request the community to help me out with my goal on this command. I'm not asking to be spoon-fed, but this is the only command in my bot till now, for which I don't have the slightest idea how to fix it. I'd like to thank everyone for their considerate reply in advance.
Hearty regards,
Sayan Bhattacharyya.
I did some research and debugging on my end and found that the thing doesn't work with URLs that don't have HTTPS:// or HTTP:// in front of them. If I use a link that has them, it works fine
So now what you have to do
See if user has used http:// or https:// in the start of the link
if not site.startswith("https://", "http://"):
# site doesn't start with http:// or https://
site = "https://" + site # we add https in front of the URL
See if the link is valid
# add this at the top of your file
import re
# define this variable somewhere outside of the command
URL_REGEX = re.compile(r"http[s]?://(?:[a-zA-Z]|[0-9]|[$-_#.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+")
# then in your command, use
if not re.fullmatch(URL_REGEX, site):
# URL is invalid, send a message here
await ctx.send("Invalid URL")
return
# the `return` here stops the command from running after sending the message
Your site does not redirect to a valid image file and doesn't end with a good image extension.
So this is not a Discord problem.