whatsapp-bot icon indicating copy to clipboard operation
whatsapp-bot copied to clipboard

can you please add unscreen.com api

Open aymenitc opened this issue 4 years ago • 24 comments

Please add command to remove background from Gif for animated stickers using unscreen.com Api

Please Thank you

aymenitc avatar Jan 01 '21 12:01 aymenitc

why don't you use remove.bg?

lxndr-rl avatar Jan 09 '21 06:01 lxndr-rl

remove.bg doesn't work with video or gif

aymenitc avatar Jan 09 '21 12:01 aymenitc

remove.bg doesn't work with video or gif

Good point. Let me see

lxndr-rl avatar Jan 09 '21 13:01 lxndr-rl

image

I'm not sure if the api works for free. Because this is the result it throws at me. If you can test it, please go ahead and send me a screenshot of your console to finish the code or if you can finish it

          if (isMedia) {
                    if (mimetype === 'video/mp4' && message.duration < 10 || mimetype === 'image/gif' && message.duration < 10) {
                        const mediaData = await decryptMedia(message, uaOverride)
                        client.reply(from, mess.wait, id)
                        const filename = `./media/gifnobg.${mimetype.split('/')[1]}`
                        await fs.writeFileSync(filename, mediaData)
                        fetch('https://api.unscreen.com/v1.0/videos', {
                            method: 'POST',
                            body: JSON.stringify({
                                video_file: filename,
                                format: "gif"
                            }),
                            headers: { "X-Api-Key": "API_KEY" }   //API_KEY = YOUR APIKEY
                        })
                            .then(respon => respon.json())
                            .then(result => {
                                console.log(result);

                            })
                            .catch(() => {
                                client.reply(from, 'Error!', id)
                            })

                    } else (
                        client.reply(from, '[❗] *!stickerGif* 10 seconds max!', id)
                    )
                }

lxndr-rl avatar Jan 21 '21 14:01 lxndr-rl

hi, I found that the unscreen api is not free. So, I purchased a small subscription to tryout. this is what I get: unscreen

can you help me please complete the code

aymenitc avatar Jan 21 '21 17:01 aymenitc

If you didn't use my example Can you show your code? (Don't forget to cover your Token)

lxndr-rl avatar Jan 22 '21 12:01 lxndr-rl

I used your exact examble. Didn't change it. Then I tried video_url too, but it gives the same result.

aymenitc avatar Jan 22 '21 12:01 aymenitc

I change fetch with cURL using the api example

Send me your console log

if (isMedia) {
                    if (mimetype === 'video/mp4' && message.duration < 10 || mimetype === 'image/gif' && message.duration < 10) {
                        const mediaData = await decryptMedia(message, uaOverride)
                        client.reply(from, mess.wait, id)
                        const filename = `./media/gifnobg.${mimetype.split('/')[1]}`
                        await fs.writeFileSync(filename, mediaData)
                        await exec(`curl -X POST "https://api.unscreen.com/v1.0/videos" -H "X-Api-Key: YOUR_API_KEY" -F "video_file=${filename}"`, async function (error, stdout, stderr) {
                            console.log(stdout)
                        })

                    } else (
                        client.reply(from, '[❗] *!stickerGif* máximo 10 segundos!', id)
                    )
                }

lxndr-rl avatar Jan 22 '21 14:01 lxndr-rl

screen1

no messages on console

aymenitc avatar Jan 22 '21 15:01 aymenitc

add console.log(error) console.log(stderr)

lxndr-rl avatar Jan 22 '21 15:01 lxndr-rl

screen4 I installed curl

aymenitc avatar Jan 22 '21 15:01 aymenitc

var request = require('request');
var fs = require('fs');

request.post({
  url: 'https://api.remove.bg/v1.0/removebg',
  formData: {
    image_file: fs.createReadStream('/path/to/file.jpg'),
    size: 'auto',
  },
  headers: {
    'X-Api-Key': 'INSERT_YOUR_API_KEY_HERE'
  },
  encoding: null
}, function(error, response, body) {
  if(error) return console.error('Request failed:', error);
  if(response.statusCode != 200) return console.error('Error:', response.statusCode, body.toString('utf8'));
  fs.writeFileSync("no-bg.png", body);
});

this is from remove.bg website. Can we edit it for unscreen.com?

aymenitc avatar Jan 22 '21 16:01 aymenitc

If you send correct parameters you can

lxndr-rl avatar Jan 22 '21 17:01 lxndr-rl

I used this code:

 if (isMedia) {
                    if (mimetype === 'video/mp4' && message.duration < 10 || mimetype === 'image/gif' && message.duration < 10) {
                        const mediaData = await decryptMedia(message, uaOverride)
                        client.reply(from, mess.wait, id)
                        const filename = `./media/gifnobg.${mimetype.split('/')[1]}`
                        await fs.writeFileSync(filename, mediaData)
                        //
                         request.post({
                             url: 'https://api.unscreen.com/v1.0/videos',
                             formData: {
                                video_file: fs.createReadStream(filename),
                                format: 'gif',
                                       },
                           headers: {
                                 'X-Api-Key': 'YOUR API KEY'
                                    },

                  }, function(error, response, body) {
                       if(error) return console.error('Request failed:', error);
                       if(response.statusCode != 200) return console.error('Error:', response.statusCode, body.toString('utf8'));
                          console.log(response) 
                          fs.writeFileSync("./media/gifnobgR.gif", body);
                    });


                    } else (
                        client.reply(from, '[❗] *!stickerGif* máximo 10 segundos!', id)
                    )
                }

it seems that I sent the video, but I'm stuck here Inkedscreen5_LI

How can wait for status to be 'done' and get video link

aymenitc avatar Jan 22 '21 17:01 aymenitc

You need retry fetch after x time, i think you need https://api.unscreen.com/v1.0/videos/{VIDEO-ID} endpoint and check if the status is done, then use sendFileFromUrl

lxndr-rl avatar Jan 22 '21 22:01 lxndr-rl

Thank you for helping... How can I extract video ID from body ? I couldn't. I think I have to convert it json. But how? I'm not expert.

aymenitc avatar Jan 22 '21 22:01 aymenitc

example

{"target":["all"],"body":"Hello", "data":{"id": "373474747"}}

To get id:


const result = response.json() //response is returned by the api 

VIDEO_ID = result.data.id

console.log(VIDEO_ID)

lxndr-rl avatar Jan 22 '21 23:01 lxndr-rl

Yes I succeeded. I was able to remove bg from gif and send it as sticker.

if (isMedia) {
                    if (mimetype === 'video/mp4' && message.duration < 10 || mimetype === 'image/gif' && message.duration < 10) {
                        const mediaData = await decryptMedia(message, uaOverride)
                        client.reply(from, mess.wait, id)
                        const filename = `./media/gifnobg.${mimetype.split('/')[1]}`
                        await fs.writeFileSync(filename, mediaData)
                        //
                         request.post({
                             url: 'https://api.unscreen.com/v1.0/videos',
                             formData: {
                                video_file: fs.createReadStream(filename),
                                format: 'gif',
                                       },
                           headers: {
                                 'X-Api-Key': 'INSERT_YOUR_API_KEY_HERE'
                                    },

                  }, function(error, response, body) {
                       if(error) return console.error('Request failed:', error);
                       if(response.statusCode != 200) return console.error('Error:', response.statusCode, body.toString('utf8'));
                        const resultScreen = JSON.parse(body)

                       const VIDEO_ID = resultScreen.data.id
                       console.log(VIDEO_ID)
                       // get video
                      const vurl = `https://api.unscreen.com/v1.0/videos/${VIDEO_ID}`
                      function poll(vurl) {
                       axios({
        method: 'get',
        url: vurl,
        headers: { 'X-Api-Key': 'INSERT_YOUR_API_KEY_HERE' },
    })
        .then(function (response) {
            // handle success
            console.log(response.data);

            if (response.data.data.attributes.status != 'done') {
                // poll again
                setTimeout(function () { poll(vurl); }, 3000);
            } else {
                // video processing is finished
                
                console.log(response.data.data.attributes.result_url)
                client.sendStickerfromUrl(from, response.data.data.attributes.result_url, { method: 'get' })
            }
        })
        .catch(function (error) {
            // handle error
            console.log(error);
            res.send(error);
        });
        }
        poll(vurl); 
                      
                    });
                       
                        
                    } else (
                        client.reply(from, '[❗] *!stickerGif* máximo 10 segundos!', id)
                    )
                }

             break

but sendStickerfromUrl function is not sending the gif in right size so, it would not bounce like this:: Screenshot_٢٠٢١٠١٢٣-٠٤٤١٠٤_WhatsApp

Can you clean the code please? and Do you have a better method to send sticker from url in right size?

Thank you for helping

aymenitc avatar Jan 23 '21 01:01 aymenitc

Nice, let me go home and I'll try to clean the code. And maybe you need see the method used in stickerGif to send as gif

lxndr-rl avatar Jan 23 '21 01:01 lxndr-rl

I fixed it by resizing the video file before uploading it to unscreen.com. I used fluent-ffmpeg

aymenitc avatar Jan 23 '21 17:01 aymenitc

Gib mal die Telefonnummer von der Bot danke

Holen Sie sich Outlook für iOShttps://aka.ms/o0ukef


Von: aymenitc [email protected] Gesendet: Saturday, January 23, 2021 6:36:11 PM An: MhankBarBar/whatsapp-bot [email protected] Cc: Subscribed [email protected] Betreff: Re: [MhankBarBar/whatsapp-bot] can you please add unscreen.com api (#173)

I fixed it by resizing the video file before uploading it to unscreen.com. I used fluent-ffmpeg

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/MhankBarBar/whatsapp-bot/issues/173#issuecomment-766146534, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ASOEUMRHEABJUMRLACDE5LLS3MCIXANCNFSM4VQH2GRQ.

reymundo1 avatar Jan 27 '21 18:01 reymundo1

Ok but whatsapp bot 2 que error define the que.

On Wed, 27 Jan 2021, 23:51 reymundo1, [email protected] wrote:

Gib mal die Telefonnummer von der Bot danke

Holen Sie sich Outlook für iOShttps://aka.ms/o0ukef


Von: aymenitc [email protected] Gesendet: Saturday, January 23, 2021 6:36:11 PM An: MhankBarBar/whatsapp-bot [email protected] Cc: Subscribed [email protected] Betreff: Re: [MhankBarBar/whatsapp-bot] can you please add unscreen.com api (#173)

I fixed it by resizing the video file before uploading it to unscreen.com. I used fluent-ffmpeg

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub< https://github.com/MhankBarBar/whatsapp-bot/issues/173#issuecomment-766146534>, or unsubscribe< https://github.com/notifications/unsubscribe-auth/ASOEUMRHEABJUMRLACDE5LLS3MCIXANCNFSM4VQH2GRQ

.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/MhankBarBar/whatsapp-bot/issues/173#issuecomment-768479910, or unsubscribe https://github.com/notifications/unsubscribe-auth/AR5ZDX2AJMICGVDKPVLNVS3S4BKUHANCNFSM4VQH2GRQ .

raghavtrivedi-23 avatar Jan 28 '21 04:01 raghavtrivedi-23

Ok but whatsapp bot 2 que error define the que. On Wed, 27 Jan 2021, 23:51 reymundo1, @.> wrote: Gib mal die Telefonnummer von der Bot danke Holen Sie sich Outlook für iOShttps://aka.ms/o0ukef ________________________________ Von: aymenitc @.> Gesendet: Saturday, January 23, 2021 6:36:11 PM An: MhankBarBar/whatsapp-bot @.> Cc: Subscribed @.> Betreff: Re: [MhankBarBar/whatsapp-bot] can you please add unscreen.com api (#173) I fixed it by resizing the video file before uploading it to unscreen.com. I used fluent-ffmpeg — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub< #173 (comment)>, or unsubscribe< https://github.com/notifications/unsubscribe-auth/ASOEUMRHEABJUMRLACDE5LLS3MCIXANCNFSM4VQH2GRQ >. — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#173 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AR5ZDX2AJMICGVDKPVLNVS3S4BKUHANCNFSM4VQH2GRQ .

I'm not expert in programming. if you can contribute by modifing the code and make it better, that would be very nice

aymenitc avatar Jan 28 '21 15:01 aymenitc

give me the number to see everything the bot can do

Holen Sie sich Outlook für iOShttps://aka.ms/o0ukef


Von: aymenitc [email protected] Gesendet: Thursday, January 28, 2021 4:12:26 PM An: MhankBarBar/whatsapp-bot [email protected] Cc: reymundo1 [email protected]; Comment [email protected] Betreff: Re: [MhankBarBar/whatsapp-bot] can you please add unscreen.com api (#173)

Ok but whatsapp bot 2 que error define the que. … On Wed, 27 Jan 2021, 23:51 reymundo1, @.> wrote: Gib mal die Telefonnummer von der Bot danke Holen Sie sich Outlook für iOShttps://aka.ms/o0ukef ________________________________ Von: aymenitc @.> Gesendet: Saturday, January 23, 2021 6:36:11 PM An: MhankBarBar/whatsapp-bot @.> Cc: Subscribed @.> Betreff: Re: [MhankBarBar/whatsapp-bot] can you please add unscreen.com api (#173https://github.com/MhankBarBar/whatsapp-bot/issues/173) I fixed it by resizing the video file before uploading it to unscreen.com. I used fluent-ffmpeg — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub< #173 (comment)https://github.com/MhankBarBar/whatsapp-bot/issues/173#issuecomment-766146534>, or unsubscribe< https://github.com/notifications/unsubscribe-auth/ASOEUMRHEABJUMRLACDE5LLS3MCIXANCNFSM4VQH2GRQ >. — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#173 (comment)https://github.com/MhankBarBar/whatsapp-bot/issues/173#issuecomment-768479910>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AR5ZDX2AJMICGVDKPVLNVS3S4BKUHANCNFSM4VQH2GRQ .

I'm not expert in programming. if you can contribute by modifing the code and make it better, that would be very nice

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/MhankBarBar/whatsapp-bot/issues/173#issuecomment-769151787, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ASOEUMQJD4LQX7WDSIGJ4TLS4F5FVANCNFSM4VQH2GRQ.

reymundo1 avatar Jan 28 '21 18:01 reymundo1