Ejemplo n.º 1
0
 async def _propose(self):
     self._count += 1
     sent = await self.sender.sendMessage('%d. Would you marry me?' %
                                          self._count,
                                          reply_markup=self.keyboard)
     self._editor = amanobot.aio.helper.Editor(self.bot, sent)
     self._edit_msg_ident = message_identifier(sent)
Ejemplo n.º 2
0
async def on_callback_query(msg):
    query_id, from_id, data = amanobot.glance(msg, flavor='callback_query')
    print('Callback query:', query_id, from_id, data)

    if data == 'notification':
        await bot.answerCallbackQuery(query_id, text='Notification at top of screen')
    elif data == 'alert':
        await bot.answerCallbackQuery(query_id, text='Alert!', show_alert=True)
    elif data == 'edit':
        global message_with_inline_keyboard

        if message_with_inline_keyboard:
            msg_idf = amanobot.message_identifier(message_with_inline_keyboard)
            await bot.editMessageText(msg_idf, 'NEW MESSAGE HERE!!!!!')
        else:
            await bot.answerCallbackQuery(query_id, text='No previous message to edit')
Ejemplo n.º 3
0
    def _init_ballot(self):
        keyboard = InlineKeyboardMarkup(inline_keyboard=[[
            InlineKeyboardButton(text='Yes', callback_data='yes'),
            InlineKeyboardButton(text='Nah!!!!', callback_data='no'),
        ]])
        sent = self.sender.sendMessage("Let's Vote ...", reply_markup=keyboard)

        self._ballot_box = {}
        self._keyboard_msg_ident = amanobot.message_identifier(sent)
        self._editor = amanobot.helper.Editor(self.bot,
                                              self._keyboard_msg_ident)

        # Generate an expiry event 30 seconds later
        self._expired_event = self.scheduler.event_later(
            30, ('_vote_expired', {
                'seconds': 30
            }))
Ejemplo n.º 4
0
    async def _init_ballot(self):
        keyboard = InlineKeyboardMarkup(inline_keyboard=[[
            InlineKeyboardButton(text='Yes', callback_data='yes'),
            InlineKeyboardButton(text='Nah!!!!', callback_data='no'),
        ]])
        sent = await self.sender.sendMessage("Let's Vote ...",
                                             reply_markup=keyboard)

        self._member_count = await self.administrator.getChatMembersCount(
        ) - 1  # exclude myself, the bot

        self._ballot_box = {}
        self._keyboard_msg_ident = message_identifier(sent)
        self._editor = amanobot.aio.helper.Editor(self.bot,
                                                  self._keyboard_msg_ident)

        # Generate an expiry event 30 seconds later
        self._expired_event = self.scheduler.event_later(
            30, ('_vote_expired', {
                'seconds': 30
            }))
Ejemplo n.º 5
0
def send_everything_on_contact(msg):
    content_type, chat_type, chat_id, msg_date, msg_id = amanobot.glance(
        msg, long=True)

    if chat_id != USER_ID:
        print('Unauthorized user:'******'from']['id'])
        exit(1)

    print('Received message from ID: %d' % chat_id)
    print('Start sending various messages ...')

    ##### forwardMessage

    r = bot.forwardMessage(chat_id, chat_id, msg_id)
    examine(r, amanobot.namedtuple.Message)

    ##### sendMessage

    r = bot.sendMessage(chat_id,
                        'Hello, I am going to send you a lot of things.',
                        reply_to_message_id=msg_id)
    examine(r, amanobot.namedtuple.Message)
    time.sleep(0.5)

    r = bot.sendMessage(chat_id, '中文')
    examine(r, amanobot.namedtuple.Message)
    time.sleep(0.5)

    r = bot.sendMessage(
        chat_id,
        '*bold text*\n_italic text_\n[link](http://www.google.com)',
        parse_mode='Markdown')
    examine(r, amanobot.namedtuple.Message)
    time.sleep(0.5)

    bot.sendMessage(chat_id, 'http://www.yahoo.com\nwith web page preview')
    time.sleep(0.5)

    bot.sendMessage(chat_id,
                    'http://www.yahoo.com\nno web page preview',
                    disable_web_page_preview=True)
    time.sleep(0.5)

    show_keyboard = {'keyboard': [['Yes', 'No'], ['Maybe', 'Maybe not']]}
    remove_keyboard = {'remove_keyboard': True}
    force_reply = {'force_reply': True}

    nt_show_keyboard = amanobot.namedtuple.ReplyKeyboardMarkup(**show_keyboard)
    nt_remove_keyboard = amanobot.namedtuple.ReplyKeyboardRemove(
        **remove_keyboard)
    nt_force_reply = amanobot.namedtuple.ForceReply(**force_reply)

    bot.sendMessage(chat_id,
                    'Here is a custom keyboard',
                    reply_markup=show_keyboard)
    time.sleep(0.5)

    bot.sendMessage(chat_id, 'Hiding it now.', reply_markup=nt_remove_keyboard)
    time.sleep(0.5)

    bot.sendMessage(chat_id, 'Force reply', reply_markup=nt_force_reply)
    time.sleep(0.5)

    ##### sendPhoto

    bot.sendChatAction(chat_id, 'upload_photo')
    r = bot.sendPhoto(chat_id, open('lighthouse.jpg', 'rb'))
    examine(r, amanobot.namedtuple.Message)
    time.sleep(0.5)

    file_id = r['photo'][0]['file_id']

    bot.sendPhoto(chat_id,
                  file_id,
                  caption='Show original message and keyboard',
                  reply_to_message_id=msg_id,
                  reply_markup=nt_show_keyboard)
    time.sleep(0.5)

    bot.sendPhoto(chat_id,
                  file_id,
                  caption='_Hide keyboard_',
                  parse_mode='Markdown',
                  reply_markup=remove_keyboard)
    time.sleep(0.5)

    furl = urllib.request.urlopen('http://i.imgur.com/35HSRQ6.png')
    bot.sendPhoto(chat_id, ('abc.jpg', furl))
    time.sleep(0.5)

    bot.sendPhoto(chat_id, ('中文照片.jpg', open('lighthouse.jpg', 'rb')),
                  caption='中文照片')
    time.sleep(0.5)

    ##### getFile

    f = bot.getFile(file_id)
    examine(f, amanobot.namedtuple.File)

    ##### download_file, smaller than one chunk (65K)

    try:
        print('Downloading file to non-existent directory ...')
        bot.download_file(file_id, 'non-existent-dir/file')
    except:
        print('Error: as expected')

    print('Downloading file to down.1 ...')
    bot.download_file(file_id, 'down.1')

    print('Open down.2 and download to it ...')
    with open('down.2', 'wb') as down:
        bot.download_file(file_id, down)

    ##### sendAudio
    # Need one of `performer` or `title' for server to regard it as audio. Otherwise, server treats it as voice.

    bot.sendChatAction(chat_id, 'upload_audio')
    r = bot.sendAudio(chat_id, open('dgdg.mp3', 'rb'), title='Ringtone')
    examine(r, amanobot.namedtuple.Message)
    time.sleep(0.5)

    file_id = r['audio']['file_id']

    bot.sendAudio(chat_id,
                  file_id,
                  duration=6,
                  performer='Ding Dong',
                  title='Ringtone',
                  reply_to_message_id=msg_id,
                  reply_markup=show_keyboard)
    time.sleep(0.5)

    bot.sendAudio(chat_id,
                  file_id,
                  performer='Ding Dong',
                  reply_markup=nt_remove_keyboard)
    time.sleep(0.5)

    bot.sendAudio(chat_id, ('中文歌.mp3', open('dgdg.mp3', 'rb')), title='中文歌')
    time.sleep(0.5)

    ##### sendDocument

    bot.sendChatAction(chat_id, 'upload_document')
    r = bot.sendDocument(chat_id, open('document.txt', 'rb'))
    examine(r, amanobot.namedtuple.Message)
    time.sleep(0.5)

    file_id = r['document']['file_id']

    bot.sendDocument(chat_id,
                     file_id,
                     reply_to_message_id=msg_id,
                     reply_markup=nt_show_keyboard)
    time.sleep(0.5)

    bot.sendDocument(chat_id, file_id, reply_markup=remove_keyboard)
    time.sleep(0.5)

    bot.sendDocument(chat_id, ('中文文件.txt', open('document.txt', 'rb')))
    time.sleep(0.5)

    ##### sendSticker

    r = bot.sendSticker(chat_id, open('gandhi.png', 'rb'))
    examine(r, amanobot.namedtuple.Message)
    time.sleep(0.5)

    file_id = r['sticker']['file_id']

    bot.sendSticker(chat_id,
                    file_id,
                    reply_to_message_id=msg_id,
                    reply_markup=show_keyboard)
    time.sleep(0.5)

    bot.sendSticker(chat_id, file_id, reply_markup=nt_remove_keyboard)
    time.sleep(0.5)

    ##### sendVideo

    bot.sendChatAction(chat_id, 'upload_video')
    r = bot.sendVideo(chat_id, open('hktraffic.mp4', 'rb'))
    examine(r, amanobot.namedtuple.Message)
    time.sleep(0.5)

    try:
        file_id = r['video']['file_id']

        bot.sendVideo(chat_id,
                      file_id,
                      duration=5,
                      caption='Hong Kong traffic',
                      reply_to_message_id=msg_id,
                      reply_markup=nt_show_keyboard)
        time.sleep(0.5)
        bot.sendVideo(chat_id, file_id, reply_markup=remove_keyboard)
        time.sleep(0.5)

    except KeyError:
        # For some reason, Telegram servers may return a document.
        print('****** sendVideo returns a DOCUMENT !!!!!')

        file_id = r['document']['file_id']

        bot.sendDocument(chat_id,
                         file_id,
                         reply_to_message_id=msg_id,
                         reply_markup=nt_show_keyboard)
        time.sleep(0.5)
        bot.sendDocument(chat_id, file_id, reply_markup=remove_keyboard)
        time.sleep(0.5)

    ##### download_file, multiple chunks

    print('Downloading file to down.3 ...')
    bot.download_file(file_id, 'down.3')

    ##### sendVoice

    r = bot.sendVoice(chat_id, open('example.ogg', 'rb'))
    examine(r, amanobot.namedtuple.Message)
    time.sleep(0.5)

    file_id = r['voice']['file_id']

    bot.sendVoice(chat_id,
                  file_id,
                  duration=6,
                  reply_to_message_id=msg_id,
                  reply_markup=show_keyboard)
    time.sleep(0.5)

    bot.sendVoice(chat_id, file_id, reply_markup=nt_remove_keyboard)
    time.sleep(0.5)

    ##### sendVideoNote

    bot.sendVideoNote(chat_id, open('hktraffic.mp4', 'rb'), length=2)

    ##### sendMediaGroup

    with open('lighthouse.jpg',
              'rb') as f1, open('gandhi.png', 'rb') as f2, open(
                  'bookshelf.jpg', 'rb') as f3, open('saturn.jpg', 'rb') as f4:
        ms = [
            amanobot.namedtuple.InputMediaPhoto(media=f1),
            amanobot.namedtuple.InputMediaPhoto(media=('media2', f2)),
            amanobot.namedtuple.InputMediaPhoto(
                media=
                'https://telegram.org/file/811140935/175c/FSf2aidnuaY.21715.gif/31dc2dbb6902dcef78'
            ),
            {
                'type': 'photo',
                'media': ('media3', ('books.jpg', f3))
            },
            {
                'type': 'photo',
                'media': f4
            },
        ]
        bot.sendMediaGroup(chat_id, ms)

    ##### sendLocation

    bot.sendChatAction(chat_id, 'find_location')
    r = bot.sendLocation(chat_id, 22.33, 114.18)  # Hong Kong
    examine(r, amanobot.namedtuple.Message)
    time.sleep(0.5)

    bot.sendLocation(chat_id,
                     49.25,
                     -123.1,
                     reply_to_message_id=msg_id,
                     reply_markup=nt_show_keyboard)  # Vancouver
    time.sleep(0.5)

    bot.sendLocation(chat_id, -37.82, 144.97,
                     reply_markup=remove_keyboard)  # Melbourne
    time.sleep(0.5)

    r = bot.sendLocation(chat_id, -37.82, 144.97, live_period=60)  # Melbourne
    time.sleep(3)

    mif = amanobot.message_identifier(r)
    bot.editMessageLiveLocation(mif, -37.819, 144.97)
    time.sleep(1)

    bot.editMessageLiveLocation(mif, -37.818, 144.97)
    time.sleep(1)

    bot.stopMessageLiveLocation(mif)

    ##### sendGame

    bot.sendGame(chat_id, 'sunchaser')
    time.sleep(0.5)

    game_keyboard = amanobot.namedtuple.InlineKeyboardMarkup(inline_keyboard=[[
        amanobot.namedtuple.InlineKeyboardButton(text='Play now',
                                                 callback_game=True),
        amanobot.namedtuple.InlineKeyboardButton(
            text='How to play?', url='https://mygame.com/howto'),
    ]])
    bot.sendGame(chat_id, 'sunchaser', reply_markup=game_keyboard)
    time.sleep(0.5)

    ##### Done sending messages

    bot.sendMessage(chat_id, 'I am done.')