jehy / telegram-test-api

Simple implimentation of telegram API which can be used for testing telegram bots
MIT License
98 stars 24 forks source link

send photo instead of text #30

Open AnthonyMouallem opened 4 years ago

AnthonyMouallem commented 4 years ago

Is there a way to send a photo instead of text

aveeday commented 4 years ago

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,
})
b3nab commented 4 years ago

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?

tundeOlagunju commented 4 years ago

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 commented 4 years ago

Is this repository still being monitored ?

tundeOlagunju commented 4 years ago

Are pull requests still welcome?

jehy commented 4 years ago

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

tundeOlagunju commented 4 years ago

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 commented 4 years ago

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

Alright. Cool!

tundeOlagunju commented 4 years ago
const text = `[${photo}]\n${caption}`

What is photo in this case?

tundeOlagunju commented 4 years ago

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

aveeday commented 4 years ago

@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.

tundeOlagunju commented 4 years ago

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 commented 4 years ago

@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

aveeday commented 4 years ago

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

tundeOlagunju commented 4 years ago

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

jehy commented 4 years ago

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