Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support image generation #75

Merged
merged 1 commit into from
Feb 21, 2024

Conversation

RidiculousDoge
Copy link
Contributor

使用openai的nodejs库,添加图片生成功能
使用方式:
/image 提示词

prompt = cmdParams.action.substring(7,len);
logger(prompt)
url = await getOpenaiImageUrl(prompt);
await reply(cmdParams.messageId,url);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是不是可以考虑将其变成飞书自己的图片?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

飞书bot根据url生成图片的接口似乎不是很好用,目测需要下载成二进制然后上传图片,我调用飞书的上传图片接口的时候没拿到飞书的响应

async function getImageData(url) {
  return new Promise((resolve, reject) => {
    https.get(url, (response) => {
      const { statusCode } = response;
      const contentType = response.headers["content-type"];

      if (statusCode !== 200) {
        reject(new Error(`请求失败了: 状态码: ${statusCode}`));
        response.resume();
        return;
      }

      if (!/^image\//.test(contentType)) {
        reject(new Error(`无效的内容类型: ${contentType}`));
        response.resume();
        return;
      }

      let imageData = Buffer.from([]);

      response
        .on("data", (chunk) => {
          imageData = Buffer.concat([imageData, chunk]);
        })
        .on("end", () => {
          resolve(imageData);
        })
        .on("error", (error) => {
          reject(error);
        });
    });
  });
}


async function uploadImage(imageData,url){
  logger("upload image!");
  let resp = await client.im.image.create({
    data:{
      image_type:"message",
      image: imageData
    }
  });
  logger(console.dir(resp));
  if(resp.code!=0){
    return;
  }
  await ImageTable.save({url:url,image_key:resp.data.image_key});
  return resp.data.image_key;
}

@bestony bestony merged commit b962b98 into bestony:master Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants