telegram-test-api icon indicating copy to clipboard operation
telegram-test-api copied to clipboard

send photo instead of text

Open AnthonyMouallem opened this issue 5 years ago • 16 comments

Is there a way to send a photo instead of text

AnthonyMouallem avatar Jan 03 '20 09:01 AnthonyMouallem

You can send your messages with photo as text messages for testing purposes.

I have a simple solution for Telegraf Bot Framework.

const Telegraf = require('telegraf')
const TelegrafContext = require('telegraf/context')

class MyContext extends TelegrafContext {
  replyWithPhoto (photo, ...extra) {
    const { caption, ...rest } = extra[0]
    this.assert(this.chat, 'reply')
    const text = `[${photo}]\n${caption}`
    return this.telegram.sendMessage(this.chat.id, text, rest)
  }
}

const bot = new Telegraf("bot_token", {
  telegram: {
    apiRoot: server.ApiURL,
  },
  contextType: MyContext,
})

aveeday avatar Jan 07 '20 12:01 aveeday

I saw that this package use express, so it will be possible to handle multipart requests but I'm not sure how to code, someone want to discuss some improvements to this library?

b3nab avatar Feb 25 '20 22:02 b3nab

Hello @b3nab . Funny enough, I was just looking at implementing this feature into the library as it is required by me as well. Need to send photos/files rather than text.

tundeOlagunju avatar Sep 29 '20 15:09 tundeOlagunju

Is this repository still being monitored ?

tundeOlagunju avatar Sep 29 '20 15:09 tundeOlagunju

Are pull requests still welcome?

tundeOlagunju avatar Sep 29 '20 15:09 tundeOlagunju

Yup, still monitored but not in development. PRs welcome!

jehy avatar Sep 29 '20 16:09 jehy

You can send your messages with photo as text messages for testing purposes.

I have a simple solution for Telegraf Bot Framework.

const Telegraf = require('telegraf')
const TelegrafContext = require('telegraf/context')

class MyContext extends TelegrafContext {
  replyWithPhoto (photo, ...extra) {
    const { caption, ...rest } = extra[0]
    this.assert(this.chat, 'reply')
    const text = `[${photo}]\n${caption}`
    return this.telegram.sendMessage(this.chat.id, text, rest)
  }
}

const bot = new Telegraf("bot_token", {
  telegram: {
    apiRoot: server.ApiURL,
  },
  contextType: MyContext,
})

This is just for telegraf framework right? Not node-telegram-api

tundeOlagunju avatar Sep 29 '20 16:09 tundeOlagunju

Yup, still monitored but not in development. PRs welcome!

Alright. Cool!

tundeOlagunju avatar Sep 29 '20 16:09 tundeOlagunju

const text = `[${photo}]\n${caption}`

What is photo in this case?

tundeOlagunju avatar Sep 29 '20 16:09 tundeOlagunju

You can send your messages with photo as text messages for testing purposes.

I have a simple solution for Telegraf Bot Framework.

const Telegraf = require('telegraf')
const TelegrafContext = require('telegraf/context')

class MyContext extends TelegrafContext {
  replyWithPhoto (photo, ...extra) {
    const { caption, ...rest } = extra[0]
    this.assert(this.chat, 'reply')
    const text = `[${photo}]\n${caption}`
    return this.telegram.sendMessage(this.chat.id, text, rest)
  }
}

const bot = new Telegraf("bot_token", {
  telegram: {
    apiRoot: server.ApiURL,
  },
  contextType: MyContext,
})

@aveeday Please kindly look at my questions

tundeOlagunju avatar Sep 29 '20 16:09 tundeOlagunju

@aveeday Please kindly look at my questions

This patch is just for telegraf. Photo in my case is a string with url.

It is not necessary to run telegram server for testing locally. I think the best practices are just require bot in test file and call handle update method.

aveeday avatar Sep 29 '20 22:09 aveeday

Yup, still monitored but not in development. PRs welcome!

I just got to know that you built another test library called telegram-test. Does that have the functionality of client being able to send images by any chance? Thanks

tundeOlagunju avatar Sep 30 '20 09:09 tundeOlagunju

@aveeday Please kindly look at my questions

This patch is just for telegraf. Photo in my case is a string with url.

It is not necessary to run telegram server for testing locally. I think the best practices are just require bot in test file and call handle update method.

Thanks for the reply @aveeday . This library starts a server locally, are you saying that is not neccessary for testing locally? Sorry for the questions. Kind of new to this.

Also, can you pls point me to a sample test file/project you have written in the past?

Thanks

tundeOlagunju avatar Sep 30 '20 09:09 tundeOlagunju

Telegram bot can send messages in two ways:

  1. call telegram api
  2. send response to web hook

All this external communications can be mocked and bot answers can be checked in tests. I created a simple testing example to show the proof of this concept. This implementation can be easily extended for your specific needs and rewritten to any language, framework and libs you like. You can integrate this bot with Express or any other http framework/infrastructure with bot.handleUpdate() or just use internal server with bot.launch()

https://github.com/aveeday/telegraf-testing-example

aveeday avatar Oct 02 '20 13:10 aveeday

@jehy After changes are being made to this library which changes some APIs, will a new version be released?

tundeOlagunju avatar Nov 03 '20 10:11 tundeOlagunju

@tundeOlagunju yup, everything will happen according to semantic versioning model.

jehy avatar Nov 12 '20 17:11 jehy