如何从电报机器人接收图像

时间:2018-06-30 08:12:00

标签: python python-telegram-bot

尝试以下操作无法从电报机器人接收图像:

import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ServletRegistrations {

    @Bean
    public ServletRegistrationBean fileServlet(){        
        ServletRegistrationBean registration = new ServletRegistrationBean(new FileServlet(), "/files/*");

        // to load add startup uncomment the line below
        //registration.setLoadOnStartup(1);

        // define init param
        registration.addInitParameter("basePath","/WEB-INF/resources");
        return registration;
    }
}

运行时没有任何错误

1 个答案:

答案 0 :(得分:0)

我用它来发送由matplotlib生成的图像。您可以根据需要进行调整。

import telegram
from telegram.bot import Bot
import yachain
import cStringIO
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np

cfg = yachain.Config().load("telegram.cfg")

token = cfg["token"]
chat_id = cfg["chatID"]

bot = Bot(token=token)

y = [2,4,6,8,10,12,14,16,18,20]
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111)
ax.plot(x, y, label='$y = numbers')
plt.title('Legend inside')
ax.legend()
#plt.show()

buffer = cStringIO.StringIO()
fig.savefig(buffer, format='png')

buffer.seek(0)
bot.send_photo(chat_id=chat_id,
               photo=buffer)